mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-28 05:08:06 +08:00
refactor, add ExtraTypeProvider
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.json.bvt.parser;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -9,12 +10,13 @@ import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
|
||||
import com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider;
|
||||
|
||||
public class RedundantTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
public void test_extra() throws Exception {
|
||||
ExtraProcessor processor = new ExtraProcessor() {
|
||||
public void process(Object object, String key, Object value) {
|
||||
public void processExtra(Object object, String key, Object value) {
|
||||
VO vo = (VO) object;
|
||||
vo.getAttributes().put(key, value);
|
||||
}
|
||||
@@ -24,6 +26,28 @@ public class RedundantTest extends TestCase {
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
Assert.assertEquals("abc", vo.getAttributes().get("name"));
|
||||
}
|
||||
|
||||
public void test_extraWithType() throws Exception {
|
||||
class MyExtraProcessor implements ExtraProcessor, ExtraTypeProvider {
|
||||
public void processExtra(Object object, String key, Object value) {
|
||||
VO vo = (VO) object;
|
||||
vo.getAttributes().put(key, value);
|
||||
}
|
||||
|
||||
public Type getExtraType(Object object, String key) {
|
||||
if ("value".equals(key)) {
|
||||
return int.class;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
ExtraProcessor processor = new MyExtraProcessor();
|
||||
|
||||
VO vo = JSON.parseObject("{\"id\":123,\"value\":\"123456\"}", VO.class, processor);
|
||||
Assert.assertEquals(123, vo.getId());
|
||||
Assert.assertEquals(123456, vo.getAttributes().get("value"));
|
||||
}
|
||||
|
||||
|
||||
public static class VO {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user