How to covert Json to a Java Object auto-generated from Avro Schema

Viewed 469

I am trying to convert a JSON String to a Java Object in my code. The java object is an instance of a class auto-generated from an AVRO Schema. Does anyone know how to do this conversion? Following are a few things I've tried so far with no luck.

I have tried using the ObjectMapper class from the Jackson library to convert json to the target java object, but ObjectMapper fails during the conversion.

I tried AvroMapper which exends ObjectMapper, part of the jackson-dataformats library, but couldn't figure out how to use it to convert json to an avro generated object.

I've noticed that every auto-generated Avro Java class has a method named "fromByteBuffer()", I tried using that method also hoping that if I pass my json as java.nio.ByteBuffer to that method then it would return me an instance of the Object.

Thanks

1 Answers

The AvroMapper should have various readValue methods inherited from the ObjectMapper class. Try using one of those and provide the class.

avroMapper.readValue(json, Car.class);
Related