下面列出了com.fasterxml.jackson.core.JsonGenerator#useDefaultPrettyPrinter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
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();
}
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();
}
}
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);
}
}
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();
}
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);
}
}
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);
}
}
@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);
}
}
/**
* <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);
}
}
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);
}
}
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);
}
}
@Override
public JsonGenerator decorate(JsonGenerator generator) {
if (Boolean.valueOf(System.getenv("DENSE_LOGS"))) {
return generator;
}
return generator.useDefaultPrettyPrinter();
}
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);
}
}
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);
}
}
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);
}
}
@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;
}
/**
* 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();
}
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);
}
}
/**
* 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;
}
@Override
public JsonGenerator decorate(final JsonGenerator generator) {
return generator.useDefaultPrettyPrinter();
}