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

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#useDefaultPrettyPrinter() . 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: JsonCasSerializer.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
public static void serialize(Writer writer, JCas jCas) throws IOException {

        JsonFactory jsonFactory = new JsonFactory();
        JsonGenerator jg = jsonFactory.createGenerator(writer);
        jg.useDefaultPrettyPrinter();
        jg.writeStartObject();
        jg.writeFieldName(F_SDI);
        writeSDI(jg, jCas);
        jg.writeFieldName(F_WORD_ANNOTATIONS);
        writeWordAnnotations(jg, jCas);
        jg.writeFieldName(F_TERM_OCC_ANNOTATIONS);
        writeTermOccAnnotations(jg, jCas);
        jg.writeFieldName(F_FIXED_EXPRESSIONS);
        writeFixedExpressions(jg, jCas);
        writeCoveredText(jg, jCas);
        jg.writeEndObject();
        jg.flush();
        writer.close();
    }
 
Example 2
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 3
Source File: PartitionStrategyParser.java    From kite with Apache License 2.0 6 votes vote down vote up
public static String toString(PartitionStrategy strategy, 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(strategy));
    gen.close();
  } catch (IOException e) {
    throw new DatasetIOException("Cannot write to JSON generator", e);
  }
  return writer.toString();
}
 
Example 4
Source File: ExportUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void exportFederatedUsersToStream(KeycloakSession session, RealmModel realm, List<String> usersToExport, ObjectMapper mapper, OutputStream os, ExportOptions options) throws IOException {
    JsonFactory factory = mapper.getFactory();
    JsonGenerator generator = factory.createGenerator(os, JsonEncoding.UTF8);
    try {
        if (mapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            generator.useDefaultPrettyPrinter();
        }
        generator.writeStartObject();
        generator.writeStringField("realm", realm.getName());
        // generator.writeStringField("strategy", strategy.toString());
        generator.writeFieldName("federatedUsers");
        generator.writeStartArray();

        for (String userId : usersToExport) {
            UserRepresentation userRep = ExportUtils.exportFederatedUser(session, realm, userId, options);
            generator.writeObject(userRep);
        }

        generator.writeEndArray();
        generator.writeEndObject();
    } finally {
        generator.close();
    }
}
 
Example 5
Source File: JsonWriter.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
public final String writeToString(T value, boolean indent)
{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        JsonGenerator g = JsonReader.jsonFactory.createGenerator(out);
        if (indent) {
            g = g.useDefaultPrettyPrinter();
        }
        try {
            write(value, g);
        }
        finally {
            g.flush();
        }
        return new String(out.toByteArray(), "UTF-8");

    }
    catch (IOException ex) {
        // We don't want to see exceptions.
        throw LangUtil.mkAssert("Impossible", ex);
    }
}
 
Example 6
Source File: PrettyPrintingDecorator.java    From grpc-proxy with Apache License 2.0 5 votes vote down vote up
@Override
public JsonGenerator decorate(JsonGenerator generator) {
  if (Boolean.valueOf(System.getenv("DENSE_LOGS"))) {
    return generator;
  }
  return generator.useDefaultPrettyPrinter();
}
 
Example 7
Source File: ExtractorConfigIO.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
public static void toJson(ExtractorOptions options, Path path) throws IOException {
	try (FileWriter writer = new FileWriter(path.toFile())) {
		JsonGenerator jg = new JsonFactory().createGenerator(writer);
		jg.useDefaultPrettyPrinter();
		new ObjectMapper().writeValue(jg, options);
	}
}
 
Example 8
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 9
Source File: JSONPrettyPrintFilter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectWriter modify(EndpointConfigBase<?> endpoint, MultivaluedMap<String, Object> responseHeaders,
  Object valueToWrite, ObjectWriter w, JsonGenerator g) throws IOException {
  ObjectWriter writer = super.modify(endpoint, responseHeaders, valueToWrite, w, g);
  g.useDefaultPrettyPrinter();

  return writer;
}
 
Example 10
Source File: JsonWriter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public JsonWriter(OutputStream out, boolean pretty, boolean useExtendedOutput) throws IOException{
  JsonGenerator writer = factory.createJsonGenerator(out);
  if(pretty){
    writer = writer.useDefaultPrettyPrinter();
  }
  if(useExtendedOutput){
    gen = new ExtendedJsonOutput(writer);
  }else{
    gen = new BasicJsonOutput(writer);
  }

}
 
Example 11
Source File: PartitionSpecParser.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static String toJson(PartitionSpec spec, boolean pretty) {
  try {
    StringWriter writer = new StringWriter();
    JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
    if (pretty) {
      generator.useDefaultPrettyPrinter();
    }
    toJson(spec, generator);
    generator.flush();
    return writer.toString();

  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }
}
 
Example 12
Source File: SnapshotParser.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static String toJson(Snapshot snapshot) {
  try {
    StringWriter writer = new StringWriter();
    JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
    generator.useDefaultPrettyPrinter();
    toJson(snapshot, generator);
    generator.flush();
    return writer.toString();
  } catch (IOException e) {
    throw new RuntimeIOException(e, "Failed to write json for: %s", snapshot);
  }
}
 
Example 13
Source File: JsonWriter.java    From Bats with Apache License 2.0 5 votes vote down vote up
public JsonWriter(OutputStream out, boolean pretty, boolean useExtendedOutput) throws IOException{
  JsonGenerator writer = factory.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, false).createJsonGenerator(out);
  if(pretty){
    writer = writer.useDefaultPrettyPrinter();
  }
  if(useExtendedOutput){
    gen = new ExtendedJsonOutput(writer);
  }else{
    gen = new BasicJsonOutput(writer);
  }

}
 
Example 14
Source File: ExtractorConfigIO.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
public static String toJson(ExtractorOptions options) {
	try {
		StringWriter writer = new StringWriter();
		JsonGenerator jg = new JsonFactory().createGenerator(writer);
		jg.useDefaultPrettyPrinter();
		new ObjectMapper().writeValue(jg, options);
		return writer.getBuffer().toString();
	} catch (IOException e) {
		throw new TermSuiteException(e);
	}
}
 
Example 15
Source File: SchemaParser.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static String toJson(Schema schema, boolean pretty) {
  try {
    StringWriter writer = new StringWriter();
    JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
    if (pretty) {
      generator.useDefaultPrettyPrinter();
    }
    toJson(schema.asStruct(), generator);
    generator.flush();
    return writer.toString();

  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }
}
 
Example 16
Source File: JacksonUtils.java    From ameba with MIT License 5 votes vote down vote up
/**
 * <p>configureGenerator.</p>
 *
 * @param uriInfo   a {@link javax.ws.rs.core.UriInfo} object.
 * @param generator a {@link com.fasterxml.jackson.core.JsonGenerator} object.
 * @param isDev     a boolean.
 */
public static void configureGenerator(UriInfo uriInfo, JsonGenerator generator, boolean isDev) {
    MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
    String pretty = params.getFirst("pretty");
    if ("false".equalsIgnoreCase(pretty)) {
        generator.setPrettyPrinter(null);
    } else if (pretty != null && !"false".equalsIgnoreCase(pretty) || isDev) {
        generator.useDefaultPrettyPrinter();
    }
    String unicode = params.getFirst("unicode");
    if (unicode != null && !"false".equalsIgnoreCase(unicode)) {
        generator.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
    }
}
 
Example 17
Source File: JsonConfigObject.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
	try {
		StringWriter writer = new StringWriter();
		JsonGenerator jg = new JsonFactory().createGenerator(writer);
		jg.useDefaultPrettyPrinter();
		new ObjectMapper().writeValue(jg, this);
		return writer.getBuffer().toString();
	} catch (IOException e) {
		throw new TermSuiteException(e);
	}
}
 
Example 18
Source File: NameMappingParser.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static String toJson(NameMapping mapping) {
  try {
    StringWriter writer = new StringWriter();
    JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
    generator.useDefaultPrettyPrinter();
    toJson(mapping, generator);
    generator.flush();
    return writer.toString();
  } catch (IOException e) {
    throw new RuntimeIOException(e, "Failed to write json for: %s", mapping);
  }
}
 
Example 19
Source File: DefaultPrettyPrinterDecorator.java    From logbook with MIT License 4 votes vote down vote up
@Override
public JsonGenerator decorate(final JsonGenerator generator) {
    return generator.useDefaultPrettyPrinter();
}
 
Example 20
Source File: GraphJsonWriter.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * Serialise a graph in JSON format to an OutputStream.
 * <p>
 * The graph elements are written in the order GRAPH, VERTEX, TRANSACTION,
 * META. Each element type will be written, even if there are no elements of
 * that type.
 * <p>
 * Ancillary files are not written: only the JSON is done here.
 * <p>
 * Originally, the vertices were written to JSON using the position as the
 * id, so vx_id_ = 0, 1, 2, ... . This required everything else (in
 * particular the transaction writing code, but also implementers of
 * AbstractGraphIOProvider.writeObject()) to know about the mapping from
 * graph vertex id to JSON vertex id. Then I realised that is was much
 * easier to write the actual vertex id to JSON, because it doesn't matter
 * what the numbers are in the file, and since the file and JSON ids are the
 * same, there's no need to keep a mapping.
 *
 * @param graph The graph to serialise.
 * @param out The OutputStream to write to.
 * @param verbose Determines whether to write default values of attributes
 * or not.
 * @param elementTypes The GraphElementTypes to serialise.
 *
 * @return True if the user cancelled the write, false otherwise.
 *
 * @throws IOException If an I/O error occurs.
 */
public boolean writeGraphToStream(final GraphReadMethods graph, final OutputStream out, final boolean verbose, final List<GraphElementType> elementTypes) throws IOException {
    // Get a new JSON writer.
    // Don't close the underlying zip stream automatically.
    final JsonGenerator jg = new JsonFactory().createGenerator(out, JsonEncoding.UTF8);
    jg.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    jg.useDefaultPrettyPrinter();

    counter = 0;
    isCancelled = false;

    try {
        final int total = graph.getVertexCount() + graph.getTransactionCount();
        if (progress != null) {
            progress.start(total);
        }

        jg.writeStartArray();

        jg.writeStartObject();

        //write version number
        jg.writeNumberField("version", VERSION);

        //write versioned items
        jg.writeObjectFieldStart("versionedItems");
        for (Entry<String, Integer> itemVersion : UpdateProviderManager.getLatestVersions().entrySet()) {
            jg.writeNumberField(itemVersion.getKey(), itemVersion.getValue());
        }
        jg.writeEndObject();

        Schema schema = graph.getSchema();

        //write schema
        jg.writeStringField("schema", schema == null ? new BareSchemaFactory().getName() : schema.getFactory().getName());

        //write global modCounts
        final long globalModCount = graph.getGlobalModificationCounter();
        final long structModCount = graph.getStructureModificationCounter();
        final long attrModCount = graph.getStructureModificationCounter();
        jg.writeNumberField("global_mod_count", globalModCount);
        jg.writeNumberField("structure_mod_count", structModCount);
        jg.writeNumberField("attribute_mod_count", attrModCount);
        jg.writeEndObject();
        for (GraphElementType elementType : ELEMENT_TYPES_FILE_ORDER) {
            if (!isCancelled) {
                writeElements(jg, graph, elementType, verbose, elementTypes.contains(elementType));
            }
        }

        jg.writeEndArray();
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        jg.close();

        if (progress != null) {
            progress.finish();
        }
    }

    return isCancelled;
}