You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by "Dekinder, Pieter (Nokia - BE/Antwerp)" <pi...@nokia.com> on 2017/03/29 10:11:12 UTC

In Java JsonDecoder fails for Double.NaN, Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY

Hi All,

I'm not sure if this is the proper channel, but here is my question.
We are trying to use JSON serialization, but there are some troubles when using double values.
If we try to serialize Double.NaN, Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY to JSON with JsonEncoder and read them back with JsonDecoder, we get an AvroTypeException.
If we do the same thing with BinaryEncoder and BinaryDecoder, it works fine.

Is this a known bug?

As a maven dependency I am using:
    <groupId>org.apache.avro</groupId>
    <artifactId>avro</artifactId>
    <version>1.8.1</version>

Here is the code to reproduce the issue:
    @Test
    public void test() throws Exception {
        Schema schema = SchemaBuilder.builder()
                .record("record")
                .fields()
                    .optionalDouble("number1")
                    .optionalDouble("number2")
                    .optionalDouble("number3")
                .endRecord();
        GenericData.Record record = new GenericData.Record(schema);
        record.put("number1", Double.NaN);
        record.put("number2", Double.POSITIVE_INFINITY);
        record.put("number3", Double.NEGATIVE_INFINITY);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, out);
        new GenericDatumWriter<GenericRecord>(schema).write(record, encoder);
        encoder.flush();
        System.out.println(out);

        Decoder decoder = DecoderFactory.get().jsonDecoder(schema, out.toString());
        GenericData.Record deserialized = new GenericData.Record(schema);
        new GenericDatumReader<GenericRecord>(schema).read(deserialized, decoder);
    }

Output of the test():
  {"number1":{"double":"NaN"},"number2":{"double":"Infinity"},"number3":{"double":"-Infinity"}}
  org.apache.avro.AvroTypeException: Expected double. Got VALUE_STRING
   ...


Kind regards,
Pieter Dekinder