com.alibaba.fastjson.serializer.ObjectSerializer Java Examples

The following examples show how to use com.alibaba.fastjson.serializer.ObjectSerializer. 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 handleForSerializer(final ObjectSerializer serializer, Class targetType) {
    ClassNode node = repo.node(targetType.getName());
    if (null == node) {
        warn("Unknown target type: " + targetType.getName());
        return;
    }
    final SerializeConfig config = SerializeConfig.getGlobalInstance();
    node.visitSubTree(new Lang.Visitor<ClassNode>() {
        @Override
        public void visit(ClassNode classNode) throws Lang.Break {
            Class type = app.classForName(classNode.name());
            config.put(type, serializer);
        }
    });
    config.put(targetType, serializer);
}
 
Example #3
Source File: FastJsonSerializeConfig.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public void setSerializers(Map<Type, ObjectSerializer> dest){
    this.put(BigInteger.class, ToStringSerializer.instance);
    this.put(Long.class, ToStringSerializer.instance);
    this.put(Long.TYPE, ToStringSerializer.instance);
    if (CollectionUtil.isNotEmpty(dest)){
        for (Entry<Type, ObjectSerializer> entry :dest.entrySet()){
            this.put(entry.getKey(), entry.getValue());
        }
    }
}
 
Example #4
Source File: MyListSerializer.java    From dpCms with Apache License 2.0 5 votes vote down vote up
private void handle(JSONSerializer serializer, ObjectSerializer itemSerializer, SerialContext context,
		Object object, Object fieldName, Type elementType,  Object item) throws IOException {
	serializer.println();
	if (item != null) {
		itemSerializer = serializer.getObjectWriter(item.getClass());
		SerialContext itemContext = new SerialContext(context, object, fieldName, 0, 0);
		serializer.setContext(itemContext);
		itemSerializer.write(serializer, item, fieldName, elementType, 0);
	} 
	else {
		serializer.getWriter().writeNull();
	}
}
 
Example #5
Source File: SerializeConfig.java    From dpCms with Apache License 2.0 5 votes vote down vote up
public ObjectSerializer createJavaBeanSerializer(Class<?> clazz) {
	if (!Modifier.isPublic(clazz.getModifiers())) {
		MyJavaBeanSerializer bean = new MyJavaBeanSerializer(clazz);
		return bean;
	}
	return new MyJavaBeanSerializer(clazz);
}
 
Example #6
Source File: EnhancedSerializeConfig.java    From java-platform with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectSerializer getObjectWriter(Class<?> clazz) {
	ObjectSerializer writer = get(clazz);
	if (writer == null) {
		for (java.util.Map.Entry<Class<?>, ObjectSerializer> assignableSerializerEntry : assignableSerializerMap
				.entrySet()) {
			if (assignableSerializerEntry.getKey().isAssignableFrom(clazz)) {
				put(clazz, assignableSerializerEntry.getValue());
			}
		}
	}
	return super.getObjectWriter(clazz);
}
 
Example #7
Source File: FastJson.java    From actframework with Apache License 2.0 5 votes vote down vote up
@AnnotatedClassFinder(Serializer.class)
public void foundSerializer(Class<?> targetType) {
    Serializer serializerAnno = targetType.getAnnotation(Serializer.class);
    Class<? extends ObjectSerializer> serializerType = serializerAnno.value();
    ObjectSerializer serializer = app.getInstance(serializerType);
    handleForSerializer(serializer, targetType);
}
 
Example #8
Source File: Json.java    From t-io with Apache License 2.0 4 votes vote down vote up
public static SerializeConfig put(Class<?> clazz, ObjectSerializer serializer) {
	mapping.put(clazz, serializer);
	return mapping;
}
 
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 addJsonSerializer(Type type, ObjectSerializer serializer) {
    Jsoner.addSerializer(type, serializer);
}
 
Example #11
Source File: MyObjectFieldSerializer.java    From dpCms with Apache License 2.0 4 votes vote down vote up
public RuntimeSerializerInfo(ObjectSerializer fieldSerializer, Class<?> runtimeFieldClass){
    super();
    this.fieldSerializer = fieldSerializer;
    this.runtimeFieldClass = runtimeFieldClass;
}
 
Example #12
Source File: MyCollectionSerializer.java    From dpCms with Apache License 2.0 4 votes vote down vote up
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    SerializeWriter out = serializer.getWriter();
    if (object == null) {
        if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
            out.write("[]");
        } 
        else {
            out.writeNull();
        }
        return;
    }

    Type elementType = null;
    if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
        if (fieldType instanceof ParameterizedType) {
            ParameterizedType param = (ParameterizedType) fieldType;
            elementType = param.getActualTypeArguments()[0];
        }
    }

    Collection<?> collection = (Collection<?>) object;

    SerialContext context = serializer.getContext();
    serializer.setContext(context, object, fieldName, 0);

    if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
        if (HashSet.class == collection.getClass()) {
            out.append("Set");
        } 
        else if (TreeSet.class == collection.getClass()) {
            out.append("TreeSet");
        }
    }

    try {
        int i = 0;
        out.append('[');
        
        for (Object item : collection) {

            if (i++ != 0) {
                out.append(',');
            }

            if (item == null) {
                out.writeNull();
                continue;
            }

            Class<?> clazz = item.getClass();

            if (clazz == Integer.class) {
                out.writeInt(((Integer) item).intValue());
                continue;
            }

            if (clazz == Long.class) {
                out.writeLong(((Long) item).longValue());

                if (out.isEnabled(SerializerFeature.WriteClassName)) {
                    out.write('L');
                }
                continue;
            }

            ObjectSerializer itemSerializer = serializer.getObjectWriter(clazz);
            itemSerializer.write(serializer, item, i - 1, elementType, 0);
        }
        out.append(']');
    } 
    finally {
        serializer.setContext(context);
    }
}
 
Example #13
Source File: EnhancedSerializeConfig.java    From java-platform with Apache License 2.0 4 votes vote down vote up
public void putAssignableTo(Class<?> clazz, ObjectSerializer objectSerializer) {
	assignableSerializerMap.put(clazz, objectSerializer);
}
 
Example #14
Source File: FastJson.java    From actframework with Apache License 2.0 4 votes vote down vote up
private void handleForSerializer(ObjectSerializer serializer, Class[] targetTypes) {
    for (Class type : targetTypes) {
        handleForSerializer(serializer, type);
    }
}