[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Jackson serialisation of GenericJson subclasses

You're very welcome.  Glad you have it sorted.

On Fri, May 11, 2018 at 12:48 PM, Carlos Alonso <carlos@xxxxxxxxxxxxx> wrote:
Hi Tim, many thanks for your help. It's definitely interesting, but unfortunately not useful this time, I think, as that JsonTypeInfo and JsonSubClasses annotations are on the base class, which, in my case, I don't own and even if I did, I don't think I could list all the subclasses GenericJson has.

I've discovered, though, that I can configure the ObjectMapper to add that type information to all objects using 'enableDefaultTyping' https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/ObjectMapper.html#enableDefaultTyping()


On Wed, May 9, 2018 at 8:01 PM Tim Robertson <timrobertson100@xxxxxxxxx> wrote:
Hi Carlos

Here is an example of subclassing with Jackson using the @Type annotation that I did many years ago:

It decorates the JSON with an extra field ("@Type" in this case) which instructs the deserializers which Object to instantiate. I'm not sure if newer Jackson versions have changed.

I haven't considered if this is appropriate or not in your case, but I hope this helps with the Jackson bit of your question at least.

Best wishes,

On Wed, May 9, 2018 at 7:02 PM, Carlos Alonso <carlos@xxxxxxxxxxxxx> wrote:
Hi everyone!!

I'm working on BEAM-4257 issue and the approach I'm following is to create a new class 'BigQueryInsertError' that also extends 'GenericJson' and that contains three keys 'TableRow row', 'TableDataInsertAllResponse.InsertErrors error', and 'TableReference ref' and use this type as the contained items returned by WriteResults.getFailedInserts

I have now to create a Coder for this new type and I'm following the TableRowJsonCoder way https://github.com/apache/beam/blob/master/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/TableRowJsonCoder.java#L34 by relying on Jackson's ObjectMapper and StringUtf8Encoder.

The problem is that I always get errors when deserialising as it deserialises the inner TableRow as a LinkedHashMap and fails when trying to assign it. Here you can see the full stacktrace: https://pastebin.com/MkUD9L3W

Testing it a bit further I've spotted other GenericJson subclasses that cannot be encoded/decoded following that method. For example TableDataInsertAllResponse.InsertErrors itself. See the example below:

TableDataInsertAllResponse.InsertErrors err = new TableDataInsertAllResponse.InsertErrors().setIndex(0L);
ObjectMapper mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.readValue(mapper.writeValueAsString(err), TableDataInsertAllResponse.InsertErrors.class);

Fails with a similar error, but in this case is because it deserialises the index into an int: https://pastebin.com/bzXMR3z5

So a couple of questions here:
* Which is the appropriate way of encoding/decoding GenericJson subclasses? (Maybe this issues can be tackled using Jackson's type annotations, but I'm quite a newbie on Jackson and I couldn't figure out how)
* This will (hopefully) be my very first contribution to Apache Beam and I'd like to get some feedback/comments/ideas/... on the issue and the suggested solution.

Thanks everyone!