I am trying to convert a Json string to a shared Java object using an Avro schema.
Below is my code.
String json = "{\"foo\": 30.1, \"bar\": 60.2}"; String schemaLines = "{\"type\":\"record\",\"name\":\"FooBar\",\"namespace\":\"com.foo.bar\",\"fields\":[{\"name\":\"foo\",\"type\":[\"null\",\"double\"],\"default\":null},{\"name\":\"bar\",\"type\":[\"null\",\"double\"],\"default\":null}]}"; InputStream input = new ByteArrayInputStream(json.getBytes()); DataInputStream din = new DataInputStream(input); Schema schema = Schema.parse(schemaLines); Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din); DatumReader<Object> reader = new GenericDatumReader<Object>(schema); Object datum = reader.read(null, decoder);
I get "org.apache.avro.AvroTypeException: expected start-join. VALUE_NUMBER_FLOAT exception thrown."
The same code works if I do not have unions in the circuit. Can someone explain and give me a solution.
java json avro
Princey james
source share