Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AvroCodecUtil#serializeJson() - add method with bool oneline option #510

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading