Java Code Examples for com.fasterxml.jackson.core.JsonStreamContext#inRoot()

The following examples show how to use com.fasterxml.jackson.core.JsonStreamContext#inRoot() . 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: JsonXContentGenerator.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws IOException {
    if (generator.isClosed()) {
        return;
    }
    JsonStreamContext context = generator.getOutputContext();
    if ((context != null) && (context.inRoot() == false)) {
        throw new IOException("Unclosed object or array found");
    }
    if (writeLineFeedAtEnd) {
        flush();
        // Bypass generator to always write the line feed
        getLowLevelGenerator().writeRaw(LF);
    }
    generator.close();
}
 
Example 2
Source File: JsonXContentGenerator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected boolean inRoot() {
    if (isFiltered()) {
        JsonStreamContext context = filter.getFilterContext();
        return ((context != null) && (context.inRoot() && context.getCurrentName() == null));
    }
    return false;
}
 
Example 3
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 4
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;
    }

}
 
Example 5
Source File: JsonXContentGenerator.java    From crate with Apache License 2.0 4 votes vote down vote up
private boolean inRoot() {
    JsonStreamContext context = generator.getOutputContext();
    return ((context != null) && (context.inRoot() && context.getCurrentName() == null));
}