Skip to content
Advertisement

How to serialize objects to json?

I have a list of objects which I want to serialize to json. I am using the following code to do this:

JavaScript

it works as expected but in this case if there is an exception the whole list of events would be discarded. What I want is to skip any individual event if it fails to serialize. Plus these are json lines delimitted by ‘n’ as I am uploading the stream to s3 and want to be queryable by athena.

JavaScript

Advertisement

Answer

To easily serialize objects to json in Java, I suggest using the GSON library.

JavaScript

You can then turn this back to a Java object with

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement