eolivelli commented on code in PR #20654:
URL: https://github.com/apache/pulsar/pull/20654#discussion_r1243896806


##########
pulsar-client-tools/src/main/java/org/apache/pulsar/client/cli/CmdProduce.java:
##########
@@ -189,6 +208,29 @@ private List<byte[]> generateMessageBodies(List<String> 
stringMessages, List<Str
         return messageBodies;
     }
 
+    private static byte[] jsonToAvro(String m, org.apache.avro.Schema 
avroSchema){
+        try {
+            GenericDatumReader<Object> reader = new 
GenericDatumReader<>(avroSchema);
+            JsonDecoder jsonDecoder = 
DecoderFactory.get().jsonDecoder(avroSchema, m);
+            GenericDatumWriter<Object> writer = new 
GenericDatumWriter<>(avroSchema);
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            Encoder e = EncoderFactory.get().binaryEncoder(out, null);
+            Object datum = null;
+            while (true) {
+                try {
+                    datum = reader.read(datum, jsonDecoder);
+                } catch (EOFException eofException) {
+                    break;
+                }
+                writer.write(datum, e);
+                e.flush();
+            }
+            return out.toByteArray();
+        } catch (IOException e) {
+            throw new RuntimeException("Cannot convert " + m + " to AVRO " + 
e.getMessage(), e);

Review Comment:
   The error is already quite informative, because it is the error reported by 
Jackson



-- 
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]

Reply via email to