Java Code Examples for com.fasterxml.jackson.core.JsonGenerator#getOutputContext()

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#getOutputContext() . 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: JkesJsonSerializer.java    From jkes with Apache License 2.0 5 votes vote down vote up
@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();

}
 
Example 2
Source File: ConfigurableSerializerProvider.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 3
Source File: ConfigurableSerializerProvider.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: CommonBeanSerializer.java    From ameba with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: ErrorAndToStringUnknownTypeSerializer.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
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;
    }

}