Search in sources :

Example 1 with ExtraTypeProvider

use of com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider 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 ExtraTypeProvider

use of com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider 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)

Example 3 with ExtraTypeProvider

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

the class DefaultJSONParser method parseExtra.

public void parseExtra(Object object, String key) {
    // xxx
    final JSONLexer lexer = this.lexer;
    lexer.nextTokenWithColon();
    Type type = null;
    if (extraTypeProviders != null) {
        for (ExtraTypeProvider extraProvider : extraTypeProviders) {
            type = extraProvider.getExtraType(object, key);
        }
    }
    Object value = //
    type == null ? // skip
    parse() : parseObject(type);
    if (object instanceof ExtraProcessable) {
        ExtraProcessable extraProcessable = ((ExtraProcessable) object);
        extraProcessable.processExtra(key, value);
        return;
    }
    if (extraProcessors != null) {
        for (ExtraProcessor process : extraProcessors) {
            process.processExtra(object, key, value);
        }
    }
    if (resolveStatus == NeedToResolve) {
        resolveStatus = NONE;
    }
}
Also used : WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ExtraTypeProvider(com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider) ExtraProcessor(com.alibaba.fastjson.parser.deserializer.ExtraProcessor) ExtraProcessable(com.alibaba.fastjson.parser.deserializer.ExtraProcessable)

Aggregations

ExtraProcessor (com.alibaba.fastjson.parser.deserializer.ExtraProcessor)3 ExtraTypeProvider (com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider)3 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)1 Feature (com.alibaba.fastjson.parser.Feature)1 ExtraProcessable (com.alibaba.fastjson.parser.deserializer.ExtraProcessable)1 FieldTypeResolver (com.alibaba.fastjson.parser.deserializer.FieldTypeResolver)1 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 WildcardType (java.lang.reflect.WildcardType)1