com.alibaba.fastjson.parser.deserializer.ObjectDeserializer Java Examples

The following examples show how to use com.alibaba.fastjson.parser.deserializer.ObjectDeserializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastJson.java    From actframework with Apache License 2.0 6 votes vote down vote up
@AnnotatedClassFinder(For.class)
public void foundFor(Class<?> type) {
    For forAnno = type.getAnnotation(For.class);
    Class[] targetTypes = forAnno.value();
    if (0 == targetTypes.length) {
        warn("@For annotation on [%s] must have target type specified", type.getName());
        return;
    }
    if (ObjectSerializer.class.isAssignableFrom(type)) {
        ObjectSerializer serializer = (ObjectSerializer) app.getInstance(type);
        handleForSerializer(serializer, targetTypes);
    }
    if (ObjectDeserializer.class.isAssignableFrom(type)) {
        ObjectDeserializer deserializer = (ObjectDeserializer) app.getInstance(type);
        handleForDeserializer(deserializer, targetTypes);
    }
}
 
Example #2
Source File: FastJson.java    From actframework with Apache License 2.0 6 votes vote down vote up
private void handleForDeserializer(final ObjectDeserializer deserializer, Class targetType) {
    ClassNode node = repo.node(targetType.getName());
    if (null == node) {
        warn("Unknown target type: " + targetType.getName());
        return;
    }
    final ParserConfig config = ParserConfig.getGlobalInstance();
    node.visitSubTree(new Lang.Visitor<ClassNode>() {
        @Override
        public void visit(ClassNode classNode) throws Lang.Break {
            Class type = app.classForName(classNode.name());
            config.putDeserializer(type, deserializer);
        }
    });
    config.putDeserializer(targetType, deserializer);
}
 
Example #3
Source File: JsonConfig.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
public ObjectDeserializer getDeserializer(final Class<?> clazz, final Type type) {

        if (blackList != null && blackList.isBlack(clazz.getName())) {
            throw new JSONException("Failed to decode class " + type + " by json serialization, it is in blacklist");
        }
        ObjectDeserializer deserializer = super.getDeserializer(clazz, type);
        if (deserializer instanceof ThrowableDeserializer) {
            //覆盖掉默认的异常解析器
            deserializer = new JsonThrowableDeserializer(this, clazz);
            //内部支持并发
            putDeserializer(type, deserializer);
        }
        return deserializer;
    }
 
Example #4
Source File: FastJsonParserConfig.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public void setDeserializers(Map<Type, ObjectDeserializer> dest){
    if (CollectionUtil.isNotEmpty(dest)){
        for (Entry<Type, ObjectDeserializer> entry :dest.entrySet()){
            this.putDeserializer(entry.getKey(), entry.getValue());
        }
    }
}
 
Example #5
Source File: FastJson.java    From actframework with Apache License 2.0 5 votes vote down vote up
@AnnotatedClassFinder(Deserializer.class)
public void foundDeserializer(Class<?> targetType) {
    Deserializer deserializerAnno = targetType.getAnnotation(Deserializer.class);
    Class<? extends ObjectDeserializer> deserializerType = deserializerAnno.value();
    ObjectDeserializer deserializer = app.getInstance(deserializerType);
    handleForDeserializer(deserializer, targetType);
}
 
Example #6
Source File: HswebAutoConfiguration.java    From hsweb-framework with Apache License 2.0 4 votes vote down vote up
@Bean
@Primary
@ConfigurationProperties(prefix = "fastjson")
public FastJsonGenericHttpMessageConverter fastJsonGenericHttpMessageConverter(EntityFactory entityFactory) {
    JSON.DEFAULT_PARSER_FEATURE |= Feature.DisableFieldSmartMatch.getMask();
    FastJsonGenericHttpMessageConverter converter = new FastJsonGenericHttpMessageConverter();
    converter.setFeatures(
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullBooleanAsFalse
    );
    converter.setConverters(converters);
    ParserConfig.global = new ParserConfig() {
        @Override
        public ObjectDeserializer getDeserializer(Type type) {
            ObjectDeserializer derializer = getDeserializers().get(type);
            if (derializer != null) {
                return derializer;
            }
            if (type instanceof Class) {
                Class classType = ((Class) type);
                if (classType.isEnum()) {
                    return super.getDeserializer(type);
                }
                checkAutoType(type.getTypeName(), ((Class) type));
                if (Modifier.isAbstract(classType.getModifiers()) || Modifier.isInterface(classType.getModifiers())) {
                    Class realType;
                    if (entityFactory != null && (realType = entityFactory.getInstanceType(classType)) != null) {
                        return new JavaBeanDeserializer(this, realType, type);
                    }
                } else {
                    return new JavaBeanDeserializer(this, classType);
                }
            }

            return super.getDeserializer(type);
        }
    };

    //fastjson.parser.autoTypeAccept
    ParserConfig.global.addAccept("org.hswebframework.web.entity.");
    ParserConfig.global.addDeny("org.hswebframework.ezorm.core.param.SqlTerm");
    return converter;
}
 
Example #7
Source File: Jsoner.java    From ICERest with Apache License 2.0 4 votes vote down vote up
public static void addConfig(Type type, ObjectSerializer serializer, ObjectDeserializer deserializer) {
    addSerializer(type, serializer);
    addDeserializer(type, deserializer);
}
 
Example #8
Source File: Jsoner.java    From ICERest with Apache License 2.0 4 votes vote down vote up
public static void addDeserializer(Type type, ObjectDeserializer deserializer) {
    parserConfig.putDeserializer(type, deserializer);
}
 
Example #9
Source File: ConstantLoader.java    From ICERest with Apache License 2.0 4 votes vote down vote up
public void addJsonConfig(Type type, ObjectSerializer serializer, ObjectDeserializer deserializer) {
    addJsonSerializer(type, serializer);
    addJsonDeserializer(type, deserializer);
}
 
Example #10
Source File: ConstantLoader.java    From ICERest with Apache License 2.0 4 votes vote down vote up
public void addJsonDeserializer(Type type, ObjectDeserializer deserializer) {
    Jsoner.addDeserializer(type, deserializer);
}
 
Example #11
Source File: FastJson.java    From actframework with Apache License 2.0 4 votes vote down vote up
private void handleForDeserializer(ObjectDeserializer serializer, Class[] targetTypes) {
    for (Class type : targetTypes) {
        handleForDeserializer(serializer, type);
    }
}