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

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#configure() . 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: ShardWriter.java    From emodb with Apache License 2.0 5 votes vote down vote up
private JsonGenerator createGenerator()
        throws IOException {
    JsonGenerator generator = _mapper.getFactory().createGenerator(_out);
    // Disable closing the output stream on completion
    generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    return generator;
}
 
Example 2
Source File: LocalRangeScanUploader.java    From emodb with Apache License 2.0 5 votes vote down vote up
private JsonGenerator createGenerator(OutputStream out)
        throws IOException {
    JsonGenerator generator = _mapper.getFactory().createGenerator(out);
    // Disable closing the output stream on completion
    generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    return generator;
}
 
Example 3
Source File: StringsXML.java    From appium_apk_tools with Apache License 2.0 5 votes vote down vote up
public static void toJSON(final ResValuesFile input,
    final File outputDirectory) throws Exception {
  String[] paths = input.getPath().split("/"); // always "/" even on Windows
  final String outName = paths[paths.length - 1].replaceFirst("\\.xml$",
      ".json");
  final File outFile = new File(outputDirectory, outName);
  p("Saving to: " + outFile);
  JsonGenerator generator = json.createGenerator(
      new FileOutputStream(outFile), JsonEncoding.UTF8);

  // Ensure output stream is auto closed when generator.close() is called.
  generator.configure(Feature.AUTO_CLOSE_TARGET, true);
  generator.configure(Feature.AUTO_CLOSE_JSON_CONTENT, true);
  generator.configure(Feature.FLUSH_PASSED_TO_STREAM, true);
  generator.configure(Feature.QUOTE_NON_NUMERIC_NUMBERS, true);
  generator.configure(Feature.WRITE_NUMBERS_AS_STRINGS, true);
  generator.configure(Feature.QUOTE_FIELD_NAMES, true);
  // generator.configure(Feature.ESCAPE_NON_ASCII, true); // don't escape non
  // ascii
  generator.useDefaultPrettyPrinter();

  // ResStringValue extends ResScalarValue which has field mRawValue
  final Field valueField = ResScalarValue.class.getDeclaredField("mRawValue");
  valueField.setAccessible(true);

  generator.writeStartObject();
  for (ResResource resource : input.listResources()) {
    if (input.isSynthesized(resource)) {
      continue;
    }

    final String name = resource.getResSpec().getName();
    // Get the value field from the ResStringValue object.
    final String value = (String) valueField.get(resource.getValue());
    generator.writeStringField(name, value);
  }
  generator.writeEndObject();
  generator.flush();
  generator.close();
}
 
Example 4
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;
}