Java 类com.alibaba.fastjson.serializer.ObjectSerializer 实例源码

项目:ef-orm    文件:SerializeConfigEx.java   
@Override
public ObjectSerializer getObjectWriter(Class<?> clazz) {
    ObjectSerializer writer = get(clazz);
    if(writer==null){
        JSONCustom annotation = clazz.getAnnotation(JSONCustom.class);
        if (annotation != null && annotation.serializer() != Void.class) {
            ObjectSerializer os= createCustomSerializer(annotation.serializer());
            put(clazz,os);
            return os;
        }
    }
    if (writer == null) {
        if (typeCodecs != null) {
            for (Map.Entry<Class<?>, ObjectSerializer> e : typeCodecs.entrySet()) {
                if (e.getKey().isAssignableFrom(clazz)) {
                    writer = e.getValue();
                    put(clazz, writer);
                }
            }
        }
    }
    return super.getObjectWriter(clazz);
}
项目:GitHub    文件:ObjectWriteTest.java   
public void test_objectWriteTest() throws Exception {
    ObjectSerializer serializer = SerializeConfig.getGlobalInstance().getObjectWriter(Model.class);

    JSONSerializer jsonSerializer = new JSONSerializer();
    serializer.write(jsonSerializer, null, "a", Model.class, 0);

    String text = jsonSerializer.out.toString();
    assertEquals("null", text);
}
项目:datatree-adapters    文件:JsonFast.java   
public static final <T> void addSerializer(SerializeConfig config, Class<T> type,
        CheckedBiConsumer<T, JSONSerializer> consumer) {
    config.put(type, new ObjectSerializer() {

        @SuppressWarnings("unchecked")
        @Override
        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
                throws IOException {
            consumer.accept((T) object, serializer);
        }

    });
}
项目:dpCms    文件:MyListSerializer.java   
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();
    }
}
项目:dpCms    文件:SerializeConfig.java   
public ObjectSerializer createJavaBeanSerializer(Class<?> clazz) {
    if (!Modifier.isPublic(clazz.getModifiers())) {
        MyJavaBeanSerializer bean = new MyJavaBeanSerializer(clazz);
        return bean;
    }
    return new MyJavaBeanSerializer(clazz);
}
项目:java-platform    文件:EnhancedSerializeConfig.java   
@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);
}
项目:ef-orm    文件:SerializeConfigEx.java   
/**
 * 对传入类型和子类都生效的序列化器
 * @param class1
 * @param nodeSer
 */
public void putHierarchy(Class<?> class1, ObjectSerializer nodeSer) {
    if (typeCodecs == null) {
        typeCodecs = new HashMap<Class<?>, ObjectSerializer>();
    }
    typeCodecs.put(class1, nodeSer);
}
项目:dpCms    文件:MyObjectFieldSerializer.java   
public RuntimeSerializerInfo(ObjectSerializer fieldSerializer, Class<?> runtimeFieldClass){
    super();
    this.fieldSerializer = fieldSerializer;
    this.runtimeFieldClass = runtimeFieldClass;
}
项目:dpCms    文件:MyCollectionSerializer.java   
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);
    }
}
项目:java-platform    文件:EnhancedSerializeConfig.java   
public void putAssignableTo(Class<?> clazz, ObjectSerializer objectSerializer) {
    assignableSerializerMap.put(clazz, objectSerializer);
}
项目:ef-orm    文件:PartitionKeyImpl.java   
public static ObjectSerializer getSerializer() {
    return new Ser();
}
项目:ef-orm    文件:PartitionTableImpl.java   
public static ObjectSerializer getSerializer(){
    return new Ser();
}
项目:treeleaf    文件:Jsoner.java   
/**
 * 添加类型转换
 *
 * @param classz     要转换的类型
 * @param serializer 转换器
 */
public static void addSerializeConfig(Class<?> classz, ObjectSerializer serializer) {
    SerializeConfig.getGlobalInstance().put(classz, serializer);
}