Search in sources :

Example 6 with ExtraProcessor

use of com.alibaba.fastjson.parser.deserializer.ExtraProcessor in project fastjson by alibaba.

the class RedundantTest method test_extraWithType.

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"));
}
Also used : ExtraTypeProvider(com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider) ExtraProcessor(com.alibaba.fastjson.parser.deserializer.ExtraProcessor)

Aggregations

ExtraProcessor (com.alibaba.fastjson.parser.deserializer.ExtraProcessor)6 ExtraTypeProvider (com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider)3 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)2 Feature (com.alibaba.fastjson.parser.Feature)2 FieldTypeResolver (com.alibaba.fastjson.parser.deserializer.FieldTypeResolver)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1