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; }
JsonGenerator jg = new JsonFactory().createJsonGenerator(System.out); jg.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true); jg.writeStartArray(); int i = 0; while (i < 6) { jg.writeStartArray(); jg.writeObject(i++); jg.writeObject(i++); jg.writeEndArray(); } jg.writeEndArray(); jg.flush();
private static void configure(JsonGenerator generator, JsonGenerator.Feature feature, boolean enabled) { if (feature.enabledByDefault() == enabled) { return; } generator.configure(feature, enabled); }
protected JsonGenerator configure(JsonGenerator generator, boolean pretty) { generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); if (pretty) { generator.useDefaultPrettyPrinter(); } return generator; }
@Override public void beginShapeTraversal() { try { fieldCount = 0; memory = new HashSet<>(); dataFile.getParentFile().mkdirs(); writer = new FileWriter(dataFile); JsonFactory factory = new JsonFactory(); json = factory.createGenerator(writer); json.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true); } catch (IOException e) { throw new KonigException(e); } }
@Override public void write(final OutputStream output) throws IOException, WebApplicationException { final JsonGenerator generator = mapper.getFactory().createJsonGenerator(output); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.writeStartArray(); for (final E entity : entities) { generator.writeObject(toJson.apply(entity)); } generator.writeEndArray(); generator.close(); } };
public JsonWriter(OutputStream os, Charset charset, ObjectMapper mapper) throws IOException { generator = mapper.getFactory() .createGenerator(new OutputStreamWriter(os, charset)) .configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false) .setPrettyPrinter(new MinimalPrettyPrinter("")); }
public JsonWriter(OutputStream os, Charset charset, ObjectMapper mapper) throws IOException { generator = mapper.getFactory() .createGenerator(new OutputStreamWriter(os, charset)) .configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false) .setPrettyPrinter(new MinimalPrettyPrinter("")); }
@Override public void write(final OutputStream output) throws IOException, WebApplicationException { final Iterator<E> iterator = entities.iterator(); try { final JsonGenerator generator = mapper.getFactory().createGenerator(output); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.writeStartArray(); while (iterator.hasNext()) { final E entity = iterator.next(); final J asJson = toJson.apply(entity); if (asJson != null) { generator.writeObject(asJson); } } generator.writeEndArray(); generator.close(); } finally { // In case the client goes away (IOException), make sure to close the underlying DB connection entities.close(); } } };
private void initialize() throws IOException { this.generator = new JsonFactory().createGenerator(outputStream).configure(Feature.AUTO_CLOSE_TARGET, false); initializer.execute(this.generator); this.writer = new ObjectMapper() .writerFor(clazz) .with(SerializationFeature.FLUSH_AFTER_WRITE_VALUE) .writeValuesAsArray(this.generator); } };
generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
@VisibleForTesting public void jsonSerializeTree(final ObjectMapper mapper, final OutputStream output) throws IOException { final JsonGenerator generator = mapper.getFactory().createGenerator(output); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); walkTree(new WalkCallback() { private int curDepth = 0; @Override public void onCurrentNode(final int depth, final NodeInterval curNode, final NodeInterval parent) { final ItemsNodeInterval node = (ItemsNodeInterval) curNode; if (node.isRoot()) { return; } try { if (curDepth < depth) { generator.writeStartArray(); curDepth = depth; } else if (curDepth > depth) { generator.writeEndArray(); curDepth = depth; } generator.writeObject(node); } catch (IOException e) { throw new RuntimeException("Failed to deserialize tree", e); } } }); generator.close(); }
@Override public void serialize(RestEntity entity, OutputStream outputStream) throws RestException { if(entity instanceof StreamedEntity) { try { StreamedEntity streamedEntity = (StreamedEntity) entity; JsonGenerator generator = factory.createGenerator(outputStream); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false); try { JsonEntitySerializer entitySerializer = new JsonEntitySerializer(); entitySerializer.serializeEntity(streamedEntity.getEntity(), generator); } finally { generator.close(); } } catch(IOException | MetadataParseException e) { throw new RestException("Unable to serialize entity", e); } } else { throw new RestException("Unable to serialize entity not of type Entity"); } }
.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, !Boolean.parseBoolean(writerOptions.get("enableNanInf"))); if (uglify) {
public void outputGraph(final OutputStream jsonOutputStream, final Set<String> vertexPropertyKeys, final Set<String> edgePropertyKeys, final GraphSONMode mode, final boolean normalize) throws IOException { final JsonGenerator jg = jsonFactory.createGenerator(jsonOutputStream); // don't let the JsonGenerator close the underlying stream...leave that to the client passing in the stream jg.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); final GraphSONUtility graphson = new GraphSONUtility(mode, null, ElementPropertyConfig.includeProperties(vertexPropertyKeys, edgePropertyKeys, normalize)); jg.writeStartObject(); jg.writeStringField(GraphSONTokens.MODE, mode.toString()); jg.writeArrayFieldStart(GraphSONTokens.VERTICES); final Iterable<Vertex> vertices = vertices(normalize); for (Vertex v : vertices) { jg.writeTree(graphson.objectNodeFromElement(v)); } jg.writeEndArray(); jg.writeArrayFieldStart(GraphSONTokens.EDGES); final Iterable<Edge> edges = edges(normalize); for (Edge e : edges) { jg.writeTree(graphson.objectNodeFromElement(e)); } jg.writeEndArray(); jg.writeEndObject(); jg.flush(); jg.close(); }
@Override public void write(final OutputStream output) throws IOException, WebApplicationException { try { final JsonGenerator generator = mapper.getFactory().createGenerator(output); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.writeStartObject(); while (iterator.hasNext()) { final Tag tag = iterator.next(); final UUID accountId = tag.getObjectId(); try { invoiceUserApi.triggerInvoiceGeneration(accountId, clock.getUTCToday(), callContext); generator.writeStringField(accountId.toString(), OK); } catch (final InvoiceApiException e) { if (e.getCode() != ErrorCode.INVOICE_NOTHING_TO_DO.getCode()) { log.warn("Unable to trigger invoice generation for accountId='{}'", accountId); } generator.writeStringField(accountId.toString(), ErrorCode.fromCode(e.getCode()).toString()); } } generator.writeEndObject(); generator.close(); } finally { // In case the client goes away (IOException), make sure to close the underlying DB connection tags.close(); } } };
public JsonFileWriter(File outputFile, JSONWriteConfig config) throws IOException { MappingJsonFactory jsonFactory = new MappingJsonFactory(); this.generator = jsonFactory.createGenerator(outputFile, JsonEncoding.UTF8); if (config.pretty) { DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentArraysWith(NopIndenter.instance); this.generator.setPrettyPrinter(prettyPrinter); } // Allow writing of floating point NaN values not as strings this.generator.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, false); }