Example usage for com.fasterxml.jackson.core JsonGenerator copyCurrentEvent

List of usage examples for com.fasterxml.jackson.core JsonGenerator copyCurrentEvent

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator copyCurrentEvent.

Prototype

public abstract void copyCurrentEvent(JsonParser jp) throws IOException, JsonProcessingException;

Source Link

Document

Method for copying contents of the current event that the given parser instance points to.

Usage

From source file:com.teamlazerbeez.crm.sf.rest.HttpApiClientTest.java

private static String reformatJson(String input) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();

    JsonParser parser = jsonFactory.createJsonParser(input);
    StringWriter writer = new StringWriter();
    JsonGenerator generator = jsonFactory.createJsonGenerator(writer);
    generator.useDefaultPrettyPrinter();

    while (parser.nextToken() != null) {
        generator.copyCurrentEvent(parser);
    }/*from  www .  j  ava2  s.  c o m*/

    generator.close();

    return writer.toString();
}

From source file:com.palominolabs.crm.sf.rest.HttpApiClientTest.java

private static String reformatJson(@Nullable String input) throws IOException {
    checkNotNull(input);//from  w ww  .  ja  va 2 s. com

    JsonFactory jsonFactory = new JsonFactory();

    JsonParser parser = jsonFactory.createParser(input);
    StringWriter writer = new StringWriter();
    JsonGenerator generator = jsonFactory.createGenerator(writer);
    generator.useDefaultPrettyPrinter();

    while (parser.nextToken() != null) {
        generator.copyCurrentEvent(parser);
    }

    generator.close();

    return writer.toString();
}

From source file:org.zalando.logbook.JsonHttpLogFormatter.java

private String compactJson(final String json) throws IOException {
    if (isAlreadyCompacted(json)) {
        return json;
    }//  w  ww.  ja  v  a2s .c o m

    final StringWriter output = new StringWriter();
    final JsonFactory factory = mapper.getFactory();
    final JsonParser parser = factory.createParser(json);

    final JsonGenerator generator = factory.createGenerator(output);

    // https://github.com/jacoco/jacoco/wiki/FilteringOptions
    //noinspection TryFinallyCanBeTryWithResources - jacoco can't handle try-with correctly
    try {
        while (parser.nextToken() != null) {
            generator.copyCurrentEvent(parser);
        }
    } finally {
        generator.close();
    }

    return output.toString();
}

From source file:com.bazaarvoice.jsonpps.PrettyPrintJson.java

private void copyCurrentStructure(JsonParser parser, ObjectMapper mapper, int depth, JsonGenerator generator)
        throws IOException {
    // Avoid using the mapper to parse the entire input until we absolutely must.  This allows pretty
    // printing huge top-level arrays (that wouldn't fit in memory) containing smaller objects (that
    // individually do fit in memory) where the objects are printed with sorted keys.
    JsonToken t = parser.getCurrentToken();
    if (t == null) {
        generator.copyCurrentStructure(parser); // Will report the error of a null token.
        return;/*from  w w  w .  ja  v a2s  .  c om*/
    }
    int id = t.id();
    if (id == ID_FIELD_NAME) {
        if (depth > flatten) {
            generator.writeFieldName(parser.getCurrentName());
        }
        t = parser.nextToken();
        id = t.id();
    }
    switch (id) {
    case ID_START_OBJECT:
        if (sortKeys && depth >= flatten) {
            // Load the entire object in memory so we can sort its keys and serialize it back out.
            mapper.writeValue(generator, parser.readValueAs(Map.class));
        } else {
            // Don't load the whole object into memory.  Copy it in a memory-efficient streaming fashion.
            if (depth >= flatten) {
                generator.writeStartObject();
            }
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                copyCurrentStructure(parser, mapper, depth + 1, generator);
            }
            if (depth >= flatten) {
                generator.writeEndObject();
            }
        }
        break;
    case ID_START_ARRAY:
        // Don't load the whole array into memory.  Copy it in a memory-efficient streaming fashion.
        if (depth >= flatten) {
            generator.writeStartArray();
        }
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            copyCurrentStructure(parser, mapper, depth + 1, generator);
        }
        if (depth >= flatten) {
            generator.writeEndArray();
        }
        break;
    default:
        generator.copyCurrentEvent(parser);
        break;
    }
}