danny0405 commented on code in PR #10943:
URL: https://github.com/apache/hudi/pull/10943#discussion_r1545879498


##########
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:
   What do we get for `record.toString` when `NullPointerException` is thrown?



-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to