Search in sources :

Example 1 with ExtraProcessor

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

the class JSON method parseObject.

@SuppressWarnings("unchecked")
public static <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
    if (input == null) {
        return null;
    }
    if (features != null) {
        for (Feature feature : features) {
            featureValues |= feature.mask;
        }
    }
    DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);
    if (processor != null) {
        if (processor instanceof ExtraTypeProvider) {
            parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
        }
        if (processor instanceof ExtraProcessor) {
            parser.getExtraProcessors().add((ExtraProcessor) processor);
        }
        if (processor instanceof FieldTypeResolver) {
            parser.setFieldTypeResolver((FieldTypeResolver) processor);
        }
    }
    T value = (T) parser.parseObject(clazz, null);
    parser.handleResovleTask(value);
    parser.close();
    return (T) value;
}
Also used : FieldTypeResolver(com.alibaba.fastjson.parser.deserializer.FieldTypeResolver) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) ExtraTypeProvider(com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider) ExtraProcessor(com.alibaba.fastjson.parser.deserializer.ExtraProcessor) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) Feature(com.alibaba.fastjson.parser.Feature)

Example 2 with ExtraProcessor

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

the class JSON method parseObject.

@SuppressWarnings("unchecked")
public static final <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
    if (input == null) {
        return null;
    }
    for (Feature featrue : features) {
        featureValues = Feature.config(featureValues, featrue, true);
    }
    DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);
    if (processor instanceof ExtraTypeProvider) {
        parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
    }
    if (processor instanceof ExtraProcessor) {
        parser.getExtraProcessors().add((ExtraProcessor) processor);
    }
    T value = (T) parser.parseObject(clazz);
    parser.handleResovleTask(value);
    parser.close();
    return (T) value;
}
Also used : DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) ExtraTypeProvider(com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider) ExtraProcessor(com.alibaba.fastjson.parser.deserializer.ExtraProcessor) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) Feature(com.alibaba.fastjson.parser.Feature)

Example 3 with ExtraProcessor

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

the class JSONTest3 method test_json.

public void test_json() throws Exception {
    ExtraProcessor extraProcessor = new ExtraProcessor() {

        public void processExtra(Object object, String key, Object value) {
            Model model = (Model) object;
            model.attributes.put(key, value);
        }
    };
    Model model = JSON.parseObject("{\"id\":1001}", (Type) Model.class, extraProcessor);
    Assert.assertEquals(1, model.attributes.size());
    Assert.assertEquals(1001, model.attributes.get("id"));
}
Also used : ExtraProcessor(com.alibaba.fastjson.parser.deserializer.ExtraProcessor)

Example 4 with ExtraProcessor

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

the class RedundantTest method test_extra.

public void test_extra() throws Exception {
    ExtraProcessor processor = new ExtraProcessor() {

        public void processExtra(Object object, String key, Object value) {
            VO vo = (VO) object;
            vo.getAttributes().put(key, value);
        }
    };
    VO vo = JSON.parseObject("{\"id\":123,\"name\":\"abc\"}", VO.class, processor);
    Assert.assertEquals(123, vo.getId());
    Assert.assertEquals("abc", vo.getAttributes().get("name"));
}
Also used : ExtraProcessor(com.alibaba.fastjson.parser.deserializer.ExtraProcessor)

Example 5 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 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 FieldTypeResolver (com.alibaba.fastjson.parser.deserializer.FieldTypeResolver)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1