Refine search
public void createGenerator() throws IOException { if ( gen != null ) gen.close(); gen = fac.createGenerator(out); }
public JsonBenchmarkResultWriter(OutputStream outputStream) { requireNonNull(outputStream, "outputStream is null"); try { jsonGenerator = new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8); jsonGenerator.writeStartObject(); jsonGenerator.writeArrayFieldStart("samples"); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Example to writeJson using StreamingAPI */ private static void writeJson() throws IOException { JsonFactory jsonFactory = new JsonFactory(); OutputStream outputStream = new ByteArrayOutputStream(); JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream, JsonEncoding.UTF8); // or Stream, Reader jsonGenerator.writeStartObject(); jsonGenerator.writeStringField("message", "Hi"); jsonGenerator.writeFieldName("place"); jsonGenerator.writeStartObject(); jsonGenerator.writeStringField("name", "World!"); jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject(); jsonGenerator.close(); System.out.println(outputStream.toString()); // print "{"message":"Hi","place":{"name":"World!"}}" } }
public void createGenerator() throws IOException { if ( gen != null ) gen.close(); gen = fac.createGenerator(out); }
public static String mapToJson(Map<String, String> m) { JsonFactory jsonFactory = new JsonFactory(); StringWriter sw = new StringWriter(); try { JsonGenerator jg = jsonFactory.createGenerator(sw); jg.writeStartObject(); for (Map.Entry<String, String> entry : m.entrySet()) { jg.writeStringField(entry.getKey(), entry.getValue()); } jg.writeEndObject(); jg.close(); } catch (IOException e) { e.printStackTrace(); } return sw.toString(); }
public static JsonGenerator jsonGeneratorOf(Writer writer) throws IOException { return new JsonFactory().createGenerator(writer); }
/** * Serialize a list of objects to a JSON String. * * @param list The list of objects to serialize. */ public String serialize(List<T> list) throws IOException { StringWriter sw = new StringWriter(); JsonGenerator jsonGenerator = LoganSquare.JSON_FACTORY.createGenerator(sw); serialize(list, jsonGenerator); jsonGenerator.close(); return sw.toString(); }
public static String getJsonFromActionData(ActionData actionData) { JsonFactory jsonFactory = new JsonFactory(); StringWriter sw = new StringWriter(); try { JsonGenerator jg = jsonFactory.createGenerator(sw); jg.writeStartObject(); jg.writeStringField("timestamp_utc", actionData.timestamp_utc); jg.close(); } catch (IOException e) { e.printStackTrace();
public OdsBenchmarkResultWriter(String entity, OutputStream outputStream) { requireNonNull(entity, "entity is null"); requireNonNull(outputStream, "outputStream is null"); this.entity = entity; try { jsonGenerator = new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8); jsonGenerator.writeStartArray(); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Serialize a list of objects to an OutputStream. * * @param map The map of objects to serialize. * @param os The OutputStream to which the list should be serialized */ public void serialize(Map<String, T> map, OutputStream os) throws IOException { JsonGenerator jsonGenerator = LoganSquare.JSON_FACTORY.createGenerator(os); serialize(map, jsonGenerator); jsonGenerator.close(); }
/** * Use the Jackson streaming API to output this as GeoJSON without creating * another object. The Indicator is a column store, and is transposed WRT * the JSON representation. */ public void writeJson(OutputStream out, Boolean forcePoints) { try { JsonFactory jsonFactory = new JsonFactory(); // ObjectMapper.getJsonFactory() is better JsonGenerator jgen = jsonFactory.createGenerator(out); jgen.setCodec(new ObjectMapper()); jgen.writeStartObject(); { jgen.writeStringField("type", "FeatureCollection"); writeJsonProperties(jgen); jgen.writeArrayFieldStart("features"); { for (int f = 0; f < capacity; f++) { writeFeature(f, jgen, forcePoints); } } jgen.writeEndArray(); } jgen.writeEndObject(); jgen.close(); } catch (IOException ioex) { LOG.info("IOException, connection may have been closed while streaming JSON."); } }
@Override public void serialize(final Object entity, final OutputStream out) throws IOException { final JsonFactory factory = new JsonFactory(); final JsonGenerator generator = factory.createGenerator(out); generator.setCodec(jsonCodec); generator.writeObject(entity); generator.flush(); } }
/** * Serialize a list of objects to an OutputStream. * * @param list The list of objects to serialize. * @param os The OutputStream to which the list should be serialized */ public void serialize(List<T> list, OutputStream os) throws IOException { JsonGenerator jsonGenerator = LoganSquare.JSON_FACTORY.createGenerator(os); serialize(list, jsonGenerator); jsonGenerator.close(); }
JsonFactory jsonFactory = new JsonFactory(); JsonGenerator symJson = jsonFactory.createGenerator(symOut); JsonGenerator refJson = jsonFactory.createGenerator(refOut); JsonGenerator docJson = jsonFactory.createGenerator(docOut); JsonGenerator[] allJson = {symJson, refJson, docJson}; for (JsonGenerator json : allJson) { json.close();
/** * Encode the object for couchbase storage. * * @param source the source value. * @return the storable object. */ private Object encode(final Map<String, ByteIterator> source) { Map<String, String> stringMap = StringByteIterator.getStringMap(source); if (!useJson) { return stringMap; } ObjectNode node = JSON_MAPPER.createObjectNode(); for (Map.Entry<String, String> pair : stringMap.entrySet()) { node.put(pair.getKey(), pair.getValue()); } JsonFactory jsonFactory = new JsonFactory(); Writer writer = new StringWriter(); try { JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer); JSON_MAPPER.writeTree(jsonGenerator, node); } catch (Exception e) { throw new RuntimeException("Could not encode JSON value"); } return writer.toString(); } }
/** * Serialize an object to an OutputStream. * * @param object The object to serialize. * @param os The OutputStream being written to. */ public void serialize(T object, OutputStream os) throws IOException { JsonGenerator jsonGenerator = LoganSquare.JSON_FACTORY.createGenerator(os); serialize(object, jsonGenerator, true); jsonGenerator.close(); }
JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jgen = jsonFactory.createGenerator(output); jgen.setCodec(new ObjectMapper()); jgen.close(); } catch (IOException ioex) { LOG.info("IOException, connection may have been closed while streaming JSON.");
/** * @see <a href="https://github.com/logstash-plugins/logstash-input-beats/blob/master/PROTOCOL.md#data-frame-type">'data' frame type</a> */ private Collection<ByteBuf> parseDataFrame(Channel channel, ByteBuf channelBuffer) throws IOException { sequenceNum = channelBuffer.readUnsignedInt(); LOG.trace("Received sequence number {}", sequenceNum); final int pairs = Ints.saturatedCast(channelBuffer.readUnsignedInt()); final JsonFactory jsonFactory = new JsonFactory(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try (final JsonGenerator jg = jsonFactory.createGenerator(outputStream)) { jg.writeStartObject(); for (int i = 0; i < pairs; i++) { final String key = parseDataItem(channelBuffer); final String value = parseDataItem(channelBuffer); jg.writeStringField(key, value); } jg.writeEndObject(); } final ByteBuf buffer = Unpooled.wrappedBuffer(outputStream.toByteArray()); sendACK(channel); return Collections.singleton(buffer); }
/** * Serialize a list of objects to a JSON String. * * @param map The map of objects to serialize. */ public String serialize(Map<String, T> map) throws IOException { StringWriter sw = new StringWriter(); JsonGenerator jsonGenerator = LoganSquare.JSON_FACTORY.createGenerator(sw); serialize(map, jsonGenerator); jsonGenerator.close(); return sw.toString(); }
@Test public void testFormatting() throws IOException, ParseException { Date now = new Date(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); JsonGenerator gen = new JsonFactory().createGenerator(bos); new JsonDateSerializer().serialize(now, gen, null); gen.close(); Assert.assertEquals(String.format("\"%s\"", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(now)), bos.toString()); }