danny0405 commented on code in PR #10943:
URL: https://github.com/apache/hudi/pull/10943#discussion_r1545945200
##########
hudi-common/src/main/java/org/apache/hudi/avro/HoodieAvroUtils.java:
##########
@@ -209,10 +210,18 @@ public static byte[] avroToJson(GenericRecord record,
boolean pretty) throws IOE
private static ByteArrayOutputStream avroToJsonHelper(GenericRecord record,
boolean pretty) throws IOException {
DatumWriter<Object> writer = new GenericDatumWriter<>(record.getSchema());
ByteArrayOutputStream out = new ByteArrayOutputStream();
- JsonEncoder jsonEncoder =
EncoderFactory.get().jsonEncoder(record.getSchema(), out, pretty);
- writer.write(record, jsonEncoder);
- jsonEncoder.flush();
- return out;
+ try {
+ JsonEncoder jsonEncoder =
EncoderFactory.get().jsonEncoder(record.getSchema(), out, pretty);
+ writer.write(record, jsonEncoder);
+ jsonEncoder.flush();
+ return out;
+ } catch (ClassCastException | NullPointerException ex) {
+ // NullPointerException will be thrown in cases where the field values
are missing
+ // ClassCastException will be thrown in cases where the field values do
not match the schema type
+ // Fallback to using `toString` which also returns json but without a
pretty-print option
+ out.write(record.toString().getBytes(StandardCharsets.UTF_8));
Review Comment:
> You get a string that represents the json of the object, it does not do
any validation on types/nullability
I kind of remember we have some cases for converting the json into avro then
back to json again operations for our commit metadata.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]