我正在尝试使用Avro架构将Json字符串转换为通用Java对象.
以下是我的代码.
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);
Run Code Online (Sandbox Code Playgroud)
我得到"org.apache.avro.AvroTypeException:期望的start-union.得到VALUE_NUMBER_FLOAT"异常.
如果模式中没有联合,则相同的代码可以工作.有人可以解释并给我一个解决方案.