Home » Java » java – Support leading zeros in Avro json-Exceptionshub

java – Support leading zeros in Avro json-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a requirement, where I need to parse the json which has date field defined as long in Avro schema. I am aware in normal JSON leading zeros are not supported. But Jackson JSON parser provides option to configure support for leading zeros. In my code, I am using Avro JsonDecoder which internally uses Jackson JsonFactory. So I would like to know is there any way to configure Avro JsonDecoder to set ALLOW_NUMERIC_LEADING_ZEROS property.

Sample Json Data:

{ "date": 021019}

Avro Schema part for date field

{
    "name": "date",
    "size": 8,
    "type": ["null", "long"],
    "logicalType": "date",
    "doc": "1"
}

Code used to convert JSON to Avro generic record

JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, jsonMessage);
DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema);
GenericRecord avroRecord = datumReader.read(null, jsonDecoder);
How to&Answers: