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

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#writeTree() . 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: MutableProfileTest.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static void prettyPrint(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(json);

    CustomPrettyPrinter prettyPrinter = new CustomPrettyPrinter();
    prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb))
            .setPrettyPrinter(prettyPrinter);
    jg.writeTree(node);
    jg.close();

    System.out.println(sb.toString().replace("\"", "\\\"").replace(DefaultIndenter.SYS_LF,
            "\"" + DefaultIndenter.SYS_LF + " + \""));
}
 
Example 2
Source File: PluginDescriptor.java    From glowroot with Apache License 2.0 6 votes vote down vote up
public static String writeValue(List<PluginDescriptor> pluginDescriptors) throws IOException {
    ObjectMapper mapper = ObjectMappers.create();
    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb));
    try {
        jg.setPrettyPrinter(ObjectMappers.getPrettyPrinter());
        jg.writeStartArray();
        for (PluginDescriptor pluginDescriptor : pluginDescriptors) {
            ObjectNode objectNode = mapper.valueToTree(pluginDescriptor);
            ObjectMappers.stripEmptyContainerNodes(objectNode);
            jg.writeTree(objectNode);
        }
        jg.writeEndArray();
    } finally {
        jg.close();
    }
    // newline is not required, just a personal preference
    sb.append(ObjectMappers.NEWLINE);
    return sb.toString();
}
 
Example 3
Source File: Session.java    From jlibs with Apache License 2.0 6 votes vote down vote up
protected WAMPOutputStream errorMessage(int requestType, long requestID, ErrorCode errorCode) throws Throwable{
    if(ROUTER)
        Debugger.temp("<- ErrorMessage: [%d, %d, %d, {}, \"%s\", %s, %s]", ErrorMessage.ID, requestType, requestID, errorCode.uri, errorCode.arguments, errorCode.argumentsKw);
    WAMPOutputStream out = router.server.createOutputStream();
    try{
        JsonGenerator json = serialization.mapper().getFactory().createGenerator(out);
        json.writeStartArray();
        json.writeNumber(ErrorMessage.ID);
        json.writeNumber(requestType);
        json.writeNumber(requestID);
        json.writeStartObject();
        json.writeEndObject();
        json.writeString(errorCode.uri);
        json.writeTree(errorCode.arguments);
        json.writeTree(errorCode.argumentsKw);
        json.writeEndArray();
        json.close();
        return out;
    }catch(Throwable thr){
        out.release();
        throw thr;
    }
}
 
Example 4
Source File: Session.java    From jlibs with Apache License 2.0 6 votes vote down vote up
protected WAMPOutputStream invocationMessage(long requestID, long registrationID, ObjectNode details, JsonParser call) throws Throwable{
    if(ROUTER)
        Debugger.temp("<- InvocationMessage: [%d, %d, %s, ...]", InvocationMessage.ID, requestID, registrationID, details);
    WAMPOutputStream out = router.server.createOutputStream();
    try{
        JsonGenerator json = serialization.mapper().getFactory().createGenerator(out);
        json.writeStartArray();
        json.writeNumber(InvocationMessage.ID);
        json.writeNumber(requestID);
        json.writeNumber(registrationID);
        if(details==null){
            json.writeStartObject();
            json.writeEndObject();
        }else
            json.writeTree(details);
        while(call.nextToken()!=null)
            json.copyCurrentEvent(call);
        json.close();
        return out;
    }catch(Throwable thr){
        out.release();
        throw thr;
    }
}
 
Example 5
Source File: Session.java    From jlibs with Apache License 2.0 6 votes vote down vote up
protected WAMPOutputStream welcomeMessage() throws Throwable{
    if(ROUTER)
        Debugger.temp("<- WelcomeMessage: [%d, %d, %s]", WelcomeMessage.ID, sessionID, Peer.router.details);
    WAMPOutputStream out = router.server.createOutputStream();
    try{
        JsonGenerator json = serialization.mapper().getFactory().createGenerator(out);
        json.writeStartArray();
        json.writeNumber(WelcomeMessage.ID);
        json.writeNumber(sessionID);
        json.writeTree(Peer.router.details);
        json.writeEndArray();
        json.close();
        return out;
    }catch(Throwable thr){
        out.release();
        throw thr;
    }
}
 
Example 6
Source File: ColumnMappingParser.java    From kite with Apache License 2.0 6 votes vote down vote up
public static String toString(ColumnMapping mapping, boolean pretty) {
  StringWriter writer = new StringWriter();
  JsonGenerator gen;
  try {
    gen = new JsonFactory().createGenerator(writer);
    if (pretty) {
      gen.useDefaultPrettyPrinter();
    }
    gen.setCodec(new ObjectMapper());
    gen.writeTree(toJson(mapping));
    gen.close();
  } catch (IOException e) {
    throw new DatasetIOException("Cannot write to JSON generator", e);
  }
  return writer.toString();
}
 
Example 7
Source File: PathValueOperation.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Override
public final void serialize(final JsonGenerator jgen,
                            final SerializerProvider provider) throws IOException {
    jgen.writeStartObject();
    jgen.writeStringField("op", op);
    jgen.writeStringField("path", path.toString());
    jgen.writeFieldName("value");
    jgen.writeTree(value);
    jgen.writeEndObject();
}
 
Example 8
Source File: DebugTabUri.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void appendExpressionJson(final JsonGenerator gen, final Expression expression) throws IOException {
  if (expression == null) {
    gen.writeNull();
  } else {
    try {
      final JsonNode tree = expression.accept(new ExpressionJsonVisitor());
      gen.writeTree(tree);
    } catch (final ODataException e) {
      gen.writeString("Exception in Debug Expression visitor occurred: " + e.getMessage());
    }
  }
}
 
Example 9
Source File: ConfigFileUtil.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static String writeConfigAsString(ObjectNode rootObjectNode, List<String> keyOrder)
        throws IOException {
    ObjectNode orderedRootObjectNode = getOrderedObjectNode(rootObjectNode, keyOrder);
    ObjectMappers.stripEmptyContainerNodes(orderedRootObjectNode);
    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb));
    try {
        jg.setPrettyPrinter(ObjectMappers.getPrettyPrinter());
        jg.writeTree(orderedRootObjectNode);
    } finally {
        jg.close();
    }
    // newline is not required, just a personal preference
    return sb.toString() + ObjectMappers.NEWLINE;
}
 
Example 10
Source File: GeneralUtils.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Formats the {@code node} as a string using the pretty printer.
 */
public static String jsonPretty(JsonNode node) throws IOException {
  StringBuilder buf = new StringBuilder();
  JsonGenerator gen = JSON_FACTORY.createGenerator(new StringBuilderWriter(buf));
  gen.useDefaultPrettyPrinter();
  gen.setCodec(JsonUtils.getMapper());
  gen.writeTree(node);
  return buf.toString();
}
 
Example 11
Source File: JSONOptions.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(JSONOptions value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
    JsonGenerationException {
  if (value.opaque != null) {
    jgen.writeObject(value.opaque);
  } else {
    jgen.writeTree(value.root);
  }

}
 
Example 12
Source File: SafeReplaceOperation.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartObject();
    gen.writeStringField("op", op);
    gen.writeStringField("path", path.toString());
    gen.writeFieldName("oldValue");
    gen.writeTree(oldValue);
    gen.writeFieldName("value");
    gen.writeTree(newValue);
    gen.writeEndObject();
}
 
Example 13
Source File: ColumnMappingParser.java    From kite with Apache License 2.0 5 votes vote down vote up
public static String toString(FieldMapping mapping) {
  StringWriter writer = new StringWriter();
  JsonGenerator gen;
  try {
    gen = new JsonFactory().createGenerator(writer);
    gen.setCodec(new ObjectMapper());
    gen.writeTree(toJson(mapping));
    gen.close();
  } catch (IOException e) {
    throw new DatasetIOException("Cannot write to JSON generator", e);
  }
  return writer.toString();
}
 
Example 14
Source File: SchemaMapper.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    Schema schema,
    JsonGenerator jsonGenerator,
    SerializerProvider serializerProvider
) throws IOException {
  jsonGenerator.writeTree(jsonConverter.asJsonSchema(schema));
}
 
Example 15
Source File: RequestAspect.java    From milkman with MIT License 4 votes vote down vote up
@Override
public void serializeWithType(UnknownRequestAspect value, JsonGenerator gen, SerializerProvider serializers,
		TypeSerializer typeSer) throws IOException {
	gen.writeTree(value.content);
}
 
Example 16
Source File: RequestAspect.java    From milkman with MIT License 4 votes vote down vote up
@Override
public void serialize(UnknownRequestAspect value, JsonGenerator gen, SerializerProvider serializers)
		throws IOException {
	gen.writeTree(value.content);
}
 
Example 17
Source File: RequestContainer.java    From milkman with MIT License 4 votes vote down vote up
@Override
public void serializeWithType(UnknownRequestContainer value, JsonGenerator gen, SerializerProvider serializers,
		TypeSerializer typeSer) throws IOException {
	gen.writeTree(value.content);
}
 
Example 18
Source File: RequestContainer.java    From milkman with MIT License 4 votes vote down vote up
@Override
public void serialize(UnknownRequestContainer value, JsonGenerator gen, SerializerProvider serializers)
		throws IOException {
	gen.writeTree(value.content);
}
 
Example 19
Source File: OptionsObject.java    From milkman with MIT License 4 votes vote down vote up
@Override
public void serializeWithType(UnknownOptionsObject value, JsonGenerator gen, SerializerProvider serializers,
		TypeSerializer typeSer) throws IOException {
	gen.writeTree(value.content);
}
 
Example 20
Source File: OptionsObject.java    From milkman with MIT License 4 votes vote down vote up
@Override
public void serialize(UnknownOptionsObject value, JsonGenerator gen, SerializerProvider serializers)
		throws IOException {
	gen.writeTree(value.content);
}