/** * Convenience method for constructing generator that uses default * encoding of the format (UTF-8 for JSON and most other data formats). *<p> * Note: there are formats that use fixed encoding (like most binary data formats). * * @since 1.8 */ public JsonGenerator createJsonGenerator(OutputStream out) throws IOException { return createJsonGenerator(out, JsonEncoding.UTF8); }
@Override public StreamingRepresentationFormat writeTo( OutputStream output ) { try { JsonGenerator g = factory.createJsonGenerator( output ); return new StreamingRepresentationFormat( g, this ); } catch ( IOException e ) { throw new WebApplicationException( e ); } }
/** * Method that can be used to serialize any Java value as * JSON output, written to File provided. */ public void writeValue(File resultFile, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configAndWriteValue(_jsonFactory.createJsonGenerator(resultFile, JsonEncoding.UTF8), value); }
/** * Method that can be used to serialize any Java value as * JSON output, written to File provided. */ public void writeValue(File resultFile, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configAndWriteValue(_jsonFactory.createJsonGenerator(resultFile, JsonEncoding.UTF8), value); }
public JacksonJsonGenerator(OutputStream out) { try { this.out = out; // use dedicated method to lower Jackson requirement this.generator = JSON_FACTORY.createJsonGenerator(out, JsonEncoding.UTF8); } catch (IOException ex) { throw new EsHadoopSerializationException(ex); } }
JsonRPFormatter(DataOutputStream out) throws IOException { generator = new ObjectMapper().getJsonFactory().createJsonGenerator(out); }
/** * Method that can be used to serialize any Java value as * JSON output, using Writer provided. *<p> * Note: method does not close the underlying stream explicitly * here; however, {@link JsonFactory} this mapper uses may choose * to close the stream depending on its settings (by default, * it will try to close it when {@link JsonGenerator} we construct * is closed). */ public void writeValue(Writer w, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configAndWriteValue(_jsonFactory.createJsonGenerator(w), value); }
public JSONMeasurementsExporter(OutputStream os) throws IOException { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); g = factory.createJsonGenerator(bw); g.setPrettyPrinter(new DefaultPrettyPrinter()); }
/** * Serialize as a String */ public String toJson() throws IOException { StringWriter writer = new StringWriter(); try (JsonGenerator generator = jsonFactory.createJsonGenerator(writer) .setCodec(objectMapper)) { toJsonUtf8(generator); } return writer.toString(); }
public JSONArrayMeasurementsExporter(OutputStream os) throws IOException { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); g = factory.createJsonGenerator(bw); g.setPrettyPrinter(new DefaultPrettyPrinter()); g.writeStartArray(); }
private String readBody( JsonParser jp ) throws IOException { JsonNode node = mapper.readTree( jp ); StringWriter out = new StringWriter(); JsonGenerator gen = jsonFactory .createJsonGenerator(out); mapper.writeTree( gen, node ); gen.flush(); gen.close(); return out.toString(); }
public JsonWriter(FileSystem fs, Path writePath) throws IOException { OutputStream out = fs.create(writePath); jsonGenerator = new JsonFactory().createJsonGenerator(out); jsonGenerator.writeStartObject(); jsonGenerator.writeStringField("version", METADATA_FORMAT_VERSION); }
protected static String toJson(Map<String, ByteIterator> values) throws IOException { ObjectNode node = MAPPER.createObjectNode(); Map<String, String> stringMap = StringByteIterator.getStringMap(values); for (Map.Entry<String, String> pair : stringMap.entrySet()) { node.put(pair.getKey(), pair.getValue()); } JsonFactory jsonFactory = new JsonFactory(); Writer writer = new StringWriter(); JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer); MAPPER.writeTree(jsonGenerator, node); return writer.toString(); } }
@Override public void writeTo(BufferedWriter out) throws IOException { while(keyIterator.hasNext()) { final JsonGenerator generator = new JsonFactory(new ObjectMapper()).createJsonGenerator(out); byte[] keyBytes = keyIterator.next().get(); Object keyObject = serializer.toObject((null == keysCompressionStrategy) ? keyBytes : keysCompressionStrategy.inflate(keyBytes)); SubCommandStreamFetchEntries.writeObjectAsJson(out, keyObject, generator); out.write('\n'); } } });
public ExecutionResultSerializer( OutputStream output, URI baseUri, LogProvider logProvider, TransitionalPeriodTransactionMessContainer container ) { this.baseUri = baseUri; this.log = logProvider.getLog( getClass() ); this.container = container; JSON_FACTORY.setCodec( new Neo4jJsonCodec( container ) ); JsonGenerator generator = null; try { generator = JSON_FACTORY.createJsonGenerator( output ); } catch ( IOException e ) { loggedIOException( e ); } this.out = generator; }
public void readAndExecuteOperations( UriInfo uriInfo, HttpHeaders httpHeaders, HttpServletRequest req, InputStream body, ServletOutputStream output ) throws IOException, ServletException { results = new StreamingBatchOperationResults( jsonFactory.createJsonGenerator( output ), output ); Map<Integer,String> locations = results.getLocations(); parseAndPerform( uriInfo, httpHeaders, req, body, locations ); results.close(); }
@Test public void shouldWriteNestedMaps() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = new JsonFactory( new Neo4jJsonCodec() ).createJsonGenerator( out ); JsonNode rest = serialize( out, json, new RestRepresentationWriter( URI.create( "localhost" ) ) ); MatcherAssert.assertThat( rest.size(), equalTo( 1 ) ); JsonNode firstCell = rest.get( 0 ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); }
@Test public void shouldWriteNestedMaps() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = new JsonFactory( new Neo4jJsonCodec() ).createJsonGenerator( out ); JsonNode row = serialize( out, json, new RowWriter( ) ); MatcherAssert.assertThat( row.size(), equalTo( 1 ) ); JsonNode firstCell = row.get( 0 ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); }
/** * Method that can be used to serialize any Java value as * a String. Functionally equivalent to calling * {@link #writeValue(Writer,Object)} with {@link java.io.StringWriter} * and constructing String, but more efficient. */ public String writeValueAsString(Object value) throws IOException, JsonGenerationException, JsonMappingException { // alas, we have to pull the recycler directly here... SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler()); _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value); return sw.getAndClear(); }
private JsonNode write( Map<String, Object> row ) throws IOException, JsonParseException { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = jsonFactory.createJsonGenerator( out ); json.writeStartObject(); try { new GraphExtractionWriter().write( json, row.keySet(), new MapRow( row ), checker ); } finally { json.writeEndObject(); json.flush(); } return JsonHelper.jsonNode( out.toString( UTF_8.name() ) ); }