/** * Creates a {@link JsonEncoder} using the OutputStream provided for writing * data conforming to the Schema provided with optional pretty printing. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying OutputStream until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param out * The OutputStream to write to. Cannot be null. * @param pretty * Pretty print encoding. * @return A JsonEncoder configured with <i>out</i>, <i>schema</i> and <i>pretty</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException { return new JsonEncoder(schema, out, pretty); }
/** * Creates a {@link JsonEncoder} using the OutputStream provided for writing * data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying OutputStream until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param out * The OutputStream to write to. Cannot be null. * @return A JsonEncoder configured with <i>out</i> and <i>schema</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, OutputStream out) throws IOException { return new JsonEncoder(schema, out); }
/** * Creates a {@link JsonEncoder} using the {@link JsonGenerator} provided for * output of data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying output until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param gen * The JsonGenerator to write with. Cannot be null. * @return A JsonEncoder configured with <i>gen</i> and <i>schema</i> * @throws IOException */ JsonEncoder jsonEncoder(Schema schema, JsonGenerator gen) throws IOException { return new JsonEncoder(schema, gen); }
/** * Creates a {@link JsonEncoder} using the OutputStream provided for writing * data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying OutputStream until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param out * The OutputStream to write to. Cannot be null. * @return A JsonEncoder configured with <i>out</i> and <i>schema</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, OutputStream out) throws IOException { return new JsonEncoder(schema, out); }
/** * Creates a {@link JsonEncoder} using the OutputStream provided for writing * data conforming to the Schema provided with optional pretty printing. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying OutputStream until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param out * The OutputStream to write to. Cannot be null. * @param pretty * Pretty print encoding. * @return A JsonEncoder configured with <i>out</i>, <i>schema</i> and <i>pretty</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException { return new JsonEncoder(schema, out, pretty); }
/** * Creates a {@link JsonEncoder} using the {@link JsonGenerator} provided for * output of data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying output until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param gen * The JsonGenerator to write with. Cannot be null. * @return A JsonEncoder configured with <i>gen</i> and <i>schema</i> * @throws IOException * @deprecated internal method */ @Deprecated public JsonEncoder jsonEncoder(Schema schema, JsonGenerator gen) throws IOException { return new JsonEncoder(schema, gen); }
public static String jsonFromGenericRecord(GenericRecord record) { ByteArrayOutputStream out = new ByteArrayOutputStream(); GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(DefaultTopicSchema.MESSAGE_V0); try { Encoder encoder = new JsonEncoder(DefaultTopicSchema.MESSAGE_V0, out); writer.write(record, encoder); encoder.flush(); } catch (IOException e) { LOG.error("Unable to serialize avro record due to error " + e); } return out.toString(); }
@Override public Encoder createJsonEncoder(Schema schema, OutputStream outputStream) throws IOException { Encoder jsonEncoder = new JsonEncoder(schema, outputStream); return jsonEncoder; } }
/** * Creates a {@link JsonEncoder} using the OutputStream provided for writing * data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying OutputStream until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param out * The OutputStream to write to. Cannot be null. * @return A JsonEncoder configured with <i>out</i> and <i>schema</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, OutputStream out) throws IOException { return new JsonEncoder(schema, out); }
/** * Creates a {@link JsonEncoder} using the {@link JsonGenerator} provided for * output of data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying output until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param gen * The JsonGenerator to write with. Cannot be null. * @return A JsonEncoder configured with <i>gen</i> and <i>schema</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, JsonGenerator gen) throws IOException { return new JsonEncoder(schema, gen); }
/** * Creates a {@link JsonEncoder} using the OutputStream provided for writing * data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying OutputStream until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param out * The OutputStream to write to. Cannot be null. * @return A JsonEncoder configured with <i>out</i> and <i>schema</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, OutputStream out) throws IOException { return new JsonEncoder(schema, out); }
use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; $encoders = array(new JsonEncoder()); $normalizers = array(new GetSetMethodNormalizer()); $serializer = new Serializer($normalizers, $encoders); $jsonContent = $serializer->serialize($object, 'json');
/** * Creates a {@link JsonEncoder} using the OutputStream provided for writing * data conforming to the Schema provided with optional pretty printing. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying OutputStream until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param out * The OutputStream to write to. Cannot be null. * @param pretty * Pretty print encoding. * @return A JsonEncoder configured with <i>out</i>, <i>schema</i> and <i>pretty</i> * @throws IOException */ public JsonEncoder jsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException { return new JsonEncoder(schema, out, pretty); }
/** * Creates a {@link JsonEncoder} using the {@link JsonGenerator} provided for * output of data conforming to the Schema provided. * <p/> * {@link JsonEncoder} buffers its output. Data may not appear on the * underlying output until {@link Encoder#flush()} is called. * <p/> * {@link JsonEncoder} is not thread-safe. * * @param schema * The Schema for data written to this JsonEncoder. Cannot be null. * @param gen * The JsonGenerator to write with. Cannot be null. * @return A JsonEncoder configured with <i>gen</i> and <i>schema</i> * @throws IOException * @deprecated internal method */ @Deprecated public JsonEncoder jsonEncoder(Schema schema, JsonGenerator gen) throws IOException { return new JsonEncoder(schema, gen); }
$encoders = array(new JsonEncoder()); $normalizers = array(new GetSetMethodNormalizer());
private void dumpJson(PrintStream out, Schema schema, Object datum) throws IOException { DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema); JsonGenerator g = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8); g.useDefaultPrettyPrinter(); writer.write(datum, new JsonEncoder(schema, g)); g.flush(); out.println(); out.flush(); }
private void dumpJson(PrintStream out, Schema schema, Object datum) throws IOException { DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema); JsonGenerator g = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8); g.useDefaultPrettyPrinter(); writer.write(datum, new JsonEncoder(schema, g)); g.flush(); out.println(); out.flush(); }
@Override public int run(InputStream stdin, PrintStream out, PrintStream err, List<String> args) throws Exception { if (args.size() != 1) { // Unlike other commands, "-" can't be used for stdin, because // we can only use seekable files. err.println("Expected 1 argument: input_file."); return 1; } GenericDatumReader<Object> reader = new GenericDatumReader<Object>(); DataFileReader<Object> fileReader = new DataFileReader<Object>(new File(args.get(0)), reader); try { Schema schema = fileReader.getSchema(); DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema); Encoder encoder = new JsonEncoder(schema, (JsonGenerator)null); for (Object datum : fileReader) { // init() recreates the internal Jackson JsonGenerator encoder.init(out); writer.write(datum, encoder); encoder.flush(); out.println(); } out.flush(); } finally { fileReader.close(); } return 0; }
@Override public int run(InputStream stdin, PrintStream out, PrintStream err, List<String> args) throws Exception { if (args.size() != 1) { // Unlike other commands, "-" can't be used for stdin, because // we can only use seekable files. err.println("Expected 1 argument: input_file."); return 1; } GenericDatumReader<Object> reader = new GenericDatumReader<Object>(); DataFileReader<Object> fileReader = new DataFileReader<Object>(new File(args.get(0)), reader); try { Schema schema = fileReader.getSchema(); DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema); Encoder encoder = new JsonEncoder(schema, (JsonGenerator)null); for (Object datum : fileReader) { // init() recreates the internal Jackson JsonGenerator encoder.init(out); writer.write(datum, encoder); encoder.flush(); out.println(); } out.flush(); } finally { fileReader.close(); } return 0; }
JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator( out, JsonEncoding.UTF8); JsonEncoder jsonEncoder = new JsonEncoder(message.getRequest(), jsonGenerator);