/** * Reconfigures this JsonEncoder to output to the JsonGenerator provided. * <p/> * If the JsonGenerator provided is null, a NullPointerException is thrown. * <p/> * Otherwise, this JsonEncoder will flush its current output and then * reconfigure its output to use the provided JsonGenerator. * * @param generator * The JsonGenerator to direct output to. Cannot be null. * @throws IOException * @return this JsonEncoder */ private JsonEncoder configure(JsonGenerator generator) throws IOException { if (null == generator) throw new NullPointerException("JsonGenerator cannot be null"); if (null != parser) { flush(); } this.out = generator; return this; }
private void dumpJson(PrintStream out, Schema schema, Object datum) throws IOException { DatumWriter<Object> writer = new GenericDatumWriter<>(schema); JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, out, true); writer.write(datum, jsonEncoder); jsonEncoder.flush(); out.println(); out.flush(); }
/** * Reconfigures this JsonEncoder to output to the JsonGenerator provided. * <p/> * If the JsonGenerator provided is null, a NullPointerException is thrown. * <p/> * Otherwise, this JsonEncoder will flush its current output and then * reconfigure its output to use the provided JsonGenerator. * * @param generator * The JsonGenerator to direct output to. Cannot be null. * @throws IOException * @return this JsonEncoder * @deprecated internal method */ @Deprecated public JsonEncoder configure(JsonGenerator generator) throws IOException { if (null == generator) throw new NullPointerException("JsonGenerator cannot be null"); if (null != parser) { flush(); } this.out = generator; return this; }
/** * Encode record to Json and then convert to byte array. * * @param record the object to encode * @return the byte[] * @throws IOException Signals that an I/O exception has occurred. */ public byte[] encodeToJsonBytes(T record) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); jsonEncoder = EncoderFactory.get().jsonEncoder(this.schema, baos, true); datumWriter.write(record, jsonEncoder); jsonEncoder.flush(); baos.flush(); return baos.toByteArray(); }
/** * Encode record to Json and then convert to byte array. * * @param record the object to encode * @return the byte[] * @throws IOException Signals that an I/O exception has occurred. */ public byte[] encodeToJsonBytes(T record) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); jsonEncoder = EncoderFactory.get().jsonEncoder(this.schema, baos, true); avroWriter.write(record, jsonEncoder); jsonEncoder.flush(); baos.flush(); return baos.toByteArray(); }
/** * Read a single byte from the stream. */ @Override public int read() throws IOException { if (pos < buffer.length) { return buffer[pos++]; } if (!fileReader.hasNext()) { return -1; } writer.write(fileReader.next(), encoder); encoder.flush(); if (!fileReader.hasNext()) { // Write a new line after the last Avro record. output.write(System.getProperty("line.separator") .getBytes(StandardCharsets.UTF_8)); output.flush(); } pos = 0; buffer = output.toByteArray(); output.reset(); return read(); }
message.getRequest()); writer.write(request, jsonEncoder); jsonEncoder.flush(); out.flush(); } catch (IOException e) {
datum = reader.read(datum, binaryDecoder); writer.write(datum, jsonEncoder); jsonEncoder.flush();
writer.write(datum, encoder); encoder.flush(); out.println(); out.flush();
@Test public void testResolveUnion() throws IOException { final SpecificDatumWriter<TestRecordWithUnion> writer = new SpecificDatumWriter<>(); Schema schema = TestRecordWithUnion.SCHEMA$; ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, out); writer.setSchema(schema); TestRecordWithUnion c = TestRecordWithUnion.newBuilder(). setKind(Kind.BAR).setValue("rab").build(); writer.write(c, encoder); encoder.flush(); out.close(); String expectedJson = String.format( "{'kind':{'org.apache.avro.test.Kind':'%s'},'value':{'string':'%s'}}", c.getKind().toString(), c.getValue()).replace('\'', '"'); assertEquals(expectedJson, out.toString("UTF-8")); }
public static <D> String avroToJson(Schema schema, GenericData.Record datum) { DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, bos); writer.write(datum, encoder); encoder.flush(); return new String(bos.toByteArray()); } catch (IOException ex) { throw new RuntimeException(ex); } }
@Override public void init(OutputStream out) throws IOException { flush(); this.out = getJsonGenerator(out); }
@Override public void init(OutputStream out) throws IOException { flush(); this.out = getJsonGenerator(out); }
@Override public byte[] encode(T object) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, baos); writer.write(object, jsonEncoder); jsonEncoder.flush(); return baos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException(e); } }
/** * Convert AvroHttpRequest to JSON String. */ public String toString(final AvroHttpRequest request) { final DatumWriter<AvroHttpRequest> configurationWriter = new SpecificDatumWriter<>(AvroHttpRequest.class); try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.SCHEMA$, out); configurationWriter.write(request, encoder); encoder.flush(); return out.toString(JSON_CHARSET); } catch (final IOException e) { throw new RuntimeException(e); } }
/** * Convert AvroHttpRequest to JSON String. */ public String toString(final AvroHttpRequest request) { final DatumWriter<AvroHttpRequest> configurationWriter = new SpecificDatumWriter<>(AvroHttpRequest.class); try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.SCHEMA$, out); configurationWriter.write(request, encoder); encoder.flush(); return out.toString(JSON_CHARSET); } catch (final IOException e) { throw new RuntimeException(e); } }
@Override public String toString(final ClassHierarchy classHierarchy) throws IOException { final DatumWriter<AvroNode> classHierarchyWriter = new SpecificDatumWriter<>(AvroNode.class); try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroNode.SCHEMA$, out); classHierarchyWriter.write(toAvro(classHierarchy), encoder); encoder.flush(); out.flush(); return out.toString(JSON_CHARSET); } }
public static byte[] convertData(@NonNull final AvroPayload avroPayload) throws IOException { // We use JsonEncoder explicitly to guarantee if ever needed we can convert back to GenericRecord // There are cases where calling toString() on GenericRecord doesn't allow conversion back try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { final GenericRecord record = avroPayload.getData(); final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(record.getSchema(), out, false); final DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(record.getSchema()); writer.write(record, encoder); encoder.flush(); out.flush(); return out.toByteArray(); } } }
@Override public void append(final GenericRecord object, final Appendable appendTo) throws IOException { StringBuilder sb = TMP.get(); sb.setLength(0); try (AppendableOutputStream bos = new AppendableOutputStream(appendTo, StandardCharsets.UTF_8)) { final Schema schema = object.getSchema(); GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema); JsonEncoder jsonEncoder = SpecificRecordAppender.EF.jsonEncoder(schema, bos); writer.write(object, jsonEncoder); jsonEncoder.flush(); } catch (IOException | RuntimeException ex) { writeSerializationError(object, sb, ex); } appendTo.append(sb); }
@Override public void append(final SpecificRecord object, final Appendable appendTo) throws IOException { StringBuilder sb = TMP.get(); sb.setLength(0); try (AppendableOutputStream bos = new AppendableOutputStream(sb, StandardCharsets.UTF_8)) { final Schema schema = object.getSchema(); SpecificDatumWriter<SpecificRecord> writer = new SpecificDatumWriter<>(schema); JsonEncoder jsonEncoder = EF.jsonEncoder(schema, bos); writer.write(object, jsonEncoder); jsonEncoder.flush(); } catch (IOException | RuntimeException ex) { writeSerializationError(object, sb, ex); } appendTo.append(sb); }