[ 
https://issues.apache.org/jira/browse/AVRO-2194?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16524977#comment-16524977
 ] 

Andy Coates commented on AVRO-2194:
-----------------------------------

If you want to deserialize to a GenericRecord using logical types you need to 
specify this in your code:


{code:java}
private static GenericRecord readJsonGeneric(
            final String fileName,
            final Schema schema) throws IOException {

        final GenericData genericData = new GenericData();
        genericData.addLogicalTypeConversion(new 
TimeConversions.TimestampConversion());

        try (final InputStream is = new FileInputStream(fileName)) {
            final Decoder decoder =
                    DecoderFactory.get().jsonDecoder(schema, is);
            final DatumReader<GenericRecord> reader =
                    new GenericDatumReader<>(schema, schema, genericData);
            return reader.read(null, decoder);
        }
    }
{code}

With this change your code outputs:


{noformat}
------------ Generic record --------------
{"timestampLong": 1530102121825, "timestampDateTime": 2018-06-27T12:22:01.828Z}
class org.joda.time.DateTime
------------ Specific record --------------
{"timestampLong": 1530102121825, "timestampDateTime": 2018-06-27T12:22:01.828Z}
class org.joda.time.DateTime
{noformat}



> GenericRecord and specific class return different java type for the same 
> field with logicalType
> -----------------------------------------------------------------------------------------------
>
>                 Key: AVRO-2194
>                 URL: https://issues.apache.org/jira/browse/AVRO-2194
>             Project: Avro
>          Issue Type: Bug
>          Components: java, logical types
>    Affects Versions: 1.8.2
>            Reporter: Eva Krejcirova
>            Priority: Major
>         Attachments: avrotest.zip
>
>
> I have an Avro schema which contains field with logical type 
> timestamp-millis. When I deserialize  an object with this schema from json to 
> GenericRecord, I get Long as type of this field. When I deserialize it to 
> specific class, I get DateTime. This prevents me to write a generic code 
> which would handle both cases (I would expect this to be possible since the 
> generated class implements GenericRecord interface).
> E.g.
> {code:java}
> final GenericRecord timestampGenericRecord = 
> readJsonGeneric("timestamptest.json", schema);
> final TimestampTest timestampTestIn = readJson("timestamptest.json", 
> timestampTestOut.getClass(), schema);
> System.out.println("------------ Generic record --------------");
> printTimestamp(timestampGenericRecord);
> System.out.println("------------ Specific record --------------");
> printTimestamp(timestampTestIn);
> ----
> private static void printTimestamp(GenericRecord record) {
>    System.out.println(record);
>    System.out.println(record.get("timestampDateTime").getClass());
> }{code}
> prints out:
> {code:java}
> ------------ Generic record --------------
> {"timestampLong": 1530023620474, "timestampDateTime": 1530023620478}
> class java.lang.Long
> ------------ Specific record --------------
> {"timestampLong": 1530023620474, "timestampDateTime": 
> 2018-06-26T14:33:40.478Z}
> class org.joda.time.DateTime
> {code}
> I am attaching sample code which demonstrates the problem.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to