Skip to content

Commit

Permalink
AvroCodecUtil#serializeJson() - add method with bool oneline option (
Browse files Browse the repository at this point in the history
  • Loading branch information
dg-builder authored Aug 25, 2023
1 parent 60fa32a commit ecebef2
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ public static byte[] serializeBinary(IndexedRecord record) throws IOException {
return os.toByteArray();
}

/**
* serializes an IndexedRecord to a json string in Avro's json encoding format (pretty-printed).
*/
public static String serializeJson(IndexedRecord record, AvroVersion format) throws IOException {
return serializeJson(record, format, false);
}

/**
* Serialize an IndexedRecord (Generic Record or Specific Record) to json string (in Avro's json encoding format).
* @param record the record to serialize
* @param format the version of avro to use
* @param oneline whether to output the json in one line or pretty printed.
*/
public static String serializeJson(IndexedRecord record, AvroVersion format, boolean oneline) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Encoder encoder = AvroCompatibilityHelper.newJsonEncoder(record.getSchema(), os, true, format);
Encoder encoder = AvroCompatibilityHelper.newJsonEncoder(record.getSchema(), os, !oneline, format);
DatumWriter<IndexedRecord> writer = AvroCompatibilityHelper.isSpecificRecord(record) ?
new SpecificDatumWriter<>(record.getSchema())
: new GenericDatumWriter<>(record.getSchema());
Expand Down

0 comments on commit ecebef2

Please sign in to comment.