com.fasterxml.jackson.core.JsonGenerator#getOutputContext ( )源码实例Demo

下面列出了com.fasterxml.jackson.core.JsonGenerator#getOutputContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jkes   文件: JkesJsonSerializer.java
@Override
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException {

    JsonStreamContext context = gen.getOutputContext();
    gen.setCurrentValue(value); // must set manually, or else the result of context.getCurrentValue() will be null。this method will call context.setCurrentValue(value);

    gen.writeStartObject(); // writeStartObject(value) will call setCurrentValue(value) well
    Class<?> entityClass = value.getClass();
    Method[] methods = entityClass.getMethods(); // include method inherited from super classes
    for(Method method : methods) {
        // check whether serialization will be recursive
        if (willRecursive(gen, context, method)) {
            gen.writeEndObject();
            return;
        }

        // method annotated with @Field
        Field fieldAnnotation = method.getAnnotation(Field.class);
        if(fieldAnnotation != null) {
            serializeField(value, gen, entityClass, method, fieldAnnotation);
        }else {
            MultiFields multiFields = method.getAnnotation(MultiFields.class);
            if(multiFields != null) {
                Field mainFieldAnnotation = multiFields.mainField();
                serializeField(value, gen, entityClass, method, mainFieldAnnotation);
            }
        }
    }

    // gen.writeObjectField("extra_field", "whatever_value");
    gen.writeEndObject();

}
 
@Override
public void serializeValue(JsonGenerator jgen, Object value) throws IOException {
    JsonStreamContext ctxt = jgen.getOutputContext();
    try {
        super.serializeValue(jgen, value);
    } catch (Exception e) {
        onSerializationException(ctxt, jgen, value, e);
    }
}
 
@Override
public void serializeValue(JsonGenerator jgen, Object value, JavaType rootType) throws IOException {
    JsonStreamContext ctxt = jgen.getOutputContext();
    try {
        super.serializeValue(jgen, value, rootType);
    } catch (Exception e) {
        onSerializationException(ctxt, jgen, value, e);
    }
}
 
源代码4 项目: ameba   文件: CommonBeanSerializer.java
/**
 * only first call return FetchPath
 * <p>
 * and then call is bean sub-path (property)
 *
 * @return fetch path or null
 */
private FetchPath getPathProperties(JsonGenerator jsonGenerator) {
    FetchPath fetchPath = EbeanUtils.getRequestFetchPath();
    if (fetchPath != null) {
        JsonStreamContext context = jsonGenerator.getOutputContext();
        JsonStreamContext parent = context.getParent();
        if (parent == null) {
            return fetchPath;
        }
        StringBuilder path = new StringBuilder();
        while (parent != null && !parent.inRoot()) {
            if (parent != context.getParent()) {
                path.insert(0, '.');
            }
            path.insert(0, parent.getCurrentName());
            parent = parent.getParent();
        }
        String fp = path.toString();
        PathProperties fetch = new PathProperties();
        EbeanPathProps src = (EbeanPathProps) fetchPath;
        String cp = fp + ".";
        for (BeanPathProperties.Props prop : src.getPathProps()) {
            String pp = prop.getPath();
            if (pp != null) {
                if (pp.equals(fp)) {
                    addToFetchPath(fetch, null, prop);
                } else if (pp.startsWith(cp)) {
                    addToFetchPath(fetch, pp.substring(cp.length()), prop);
                }
            }
        }

        return fetch;
    }
    return null;
}
 
public void serializeFromError(JsonStreamContext ctxt, @Nullable Exception error, Object value, JsonGenerator jgen, SerializerProvider configurableSerializerProvider) throws IOException {
    if (log.isDebugEnabled())
        log.debug("Recovering from json serialization error, serializing "+value+": "+error);

    if (BidiSerialization.isStrictSerialization())
        throw new JsonMappingException("Cannot serialize "
            + (ctxt!=null && !ctxt.inRoot() ? "object containing " : "")
            + value.getClass().getName()+" when strict serialization requested");

    if (WARNED_CLASSES.add(value.getClass().getCanonicalName())) {
        log.warn("Standard serialization not possible for "+value.getClass()+" ("+value+")", error);
    }
    JsonStreamContext newCtxt = jgen.getOutputContext();

    // very odd, but flush seems necessary when working with large objects; presumably a buffer which is allowed to clear itself?
    // without this, when serializing the large (1.5M) Server json object from BrooklynJacksonSerializerTest creates invalid json,
    // containing:  "foo":false,"{"error":true,...
    jgen.flush();

    boolean createObject = !newCtxt.inObject() || newCtxt.getCurrentName()!=null;
    if (createObject) {
        jgen.writeStartObject();
    }

    if (allowEmpty(value.getClass())) {
        // write nothing
    } else {

        jgen.writeFieldName("error");
        jgen.writeBoolean(true);

        jgen.writeFieldName("errorType");
        jgen.writeString(NotSerializableException.class.getCanonicalName());

        jgen.writeFieldName("type");
        jgen.writeString(value.getClass().getCanonicalName());

        jgen.writeFieldName("toString");
        jgen.writeString(value.toString());

        if (error!=null) {
            jgen.writeFieldName("causedByError");
            jgen.writeString(error.toString());
        }

    }

    if (createObject) {
        jgen.writeEndObject();
    }

    while (newCtxt!=null && !newCtxt.equals(ctxt)) {
        if (jgen.getOutputContext().inArray()) { jgen.writeEndArray(); continue; }
        if (jgen.getOutputContext().inObject()) { jgen.writeEndObject(); continue; }
        break;
    }

}