diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/AvroCodecUtil.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/AvroCodecUtil.java index d7bd8343a..2f0145e9c 100644 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/AvroCodecUtil.java +++ b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/AvroCodecUtil.java @@ -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 writer = AvroCompatibilityHelper.isSpecificRecord(record) ? new SpecificDatumWriter<>(record.getSchema()) : new GenericDatumWriter<>(record.getSchema());