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

项目:ef-orm    文件:JsonTypeSerializer.java   
@SuppressWarnings("unchecked")
public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,int features) throws IOException {
    SerializeWriter out=serializer.getWriter();
    if (object == null) {
        out.writeNull();
        return;
    }

    if (serializer.containsReference(object)) {
        serializer.writeReference(object);
        return;
    }

       SerialContext parent = serializer.getContext();
       serializer.setContext(parent, object, fieldName,0);
       serializer.write(processToJson((T)object));
}
项目:GitHub    文件:PropertyPathTest3.java   
public boolean apply(JSONSerializer serializer, Object source, String name) {
    SerialContext nowContext = new SerialContext(serializer.getContext(), source, name, 0, 0);
    String nowPath = getLinkedPath(nowContext);
    System.out.println("path->" + nowPath);
    //只输出children.id
    return containInclude(onlyProperties, nowPath);
}
项目:GitHub    文件:PropertyPathTest3.java   
/** 输出结果 类似a.b.c.d等格式,忽略[] */
private static String getLinkedPath(SerialContext serialContext) {
    //这里有点bad smell,即要考虑parent为null,又要考虑fieldName为null,且对collection判断只能从fieldName,而不能从object入手
    boolean isCollection = serialContext.fieldName instanceof Integer;
    boolean isFieldNameNull = serialContext.fieldName == null;
    if(serialContext.parent == null)
        return isCollection ? "" : isFieldNameNull ? "" : String.valueOf(serialContext.fieldName);
    String parentLinkedPath = getLinkedPath(serialContext.parent);
    if(isCollection || isFieldNameNull)
        return parentLinkedPath;
    return
        parentLinkedPath.length() == 0 ? String.valueOf(serialContext.fieldName) :
            parentLinkedPath + "." + serialContext.fieldName;
}
项目:dpCms    文件:MyJavaBeanSerializer.java   
@Override
public boolean writeReference(JSONSerializer serializer, Object object,
        int fieldFeatures) {

    SerialContext context = serializer.getContext();
    {

        if (context != null
                && SerializerFeature.isEnabled(context.getFeatures(),
                        fieldFeatures,
                        SerializerFeature.DisableCircularReferenceDetect)) {
            return false;
        }
    }

    if (!serializer.containsReference(object)) {
        return false;
    }

    SerialContext parentCtx = context;
    while ((parentCtx = parentCtx.getParent()) != null) {
        Object parentObj = parentCtx.getObject();
        if (object.equals(parentObj)) {
            serializer.writeNull();
            return true;
        }
    }
    return false;
}
项目: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();
    }
}
项目:GitHub    文件:SerialContextTest.java   
public void test_context() throws Exception {
    SerialContext root = new SerialContext(null, null, null, 0, 0);
    SerialContext context = new SerialContext(root, null, "x", 0, 0);
    Assert.assertEquals("x", context.fieldName);
    Assert.assertEquals("$.x", context.toString());
}
项目: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);
    }
}