0

I am subscribing to an SQS and the message body has a few epoch times. It looks like below

"createdAt" : 1660744139,\n "updatedAt" : 1660744139,\n

I have a Java POJO which contains 2 fields

Date createdAt;
Date updatedAt;

I am using GSON to deserialize the SQS message body to my POJO, but it's failing with an error com.google.gson.JsonSyntaxException: 1660744139.

My JSON instantiation looks like

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

If I understand it right, the error is being thrown because I am trying to deserialize a Long type to Date. What will be the correct way to deserialize?

I have seen suggestions to instantiate my GSON object with dateFormatter, but not sure if that will work for my case. Thank you.

  • 2
    I strongly recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 04 '22 at 19:14
  • 1
    Does this answer your question? [Gson dateformat to serialize/deserialize unix-timestamps](https://stackoverflow.com/questions/41348055/gson-dateformat-to-serialize-deserialize-unix-timestamps) – Marcono1234 Sep 04 '22 at 21:26
  • @OleV.V. can you please elaborate a little bit more. I would really appreciate an explanation with a pseudo example. – Conquistador Sep 06 '22 at 21:09
  • 1
    I appreciate your positive interest, I am confident that it will pay off for you. I don’t know GSON, sorry, so can’t help you with that side. A quick search led me to [How to serialize and deserialize Java 8's java.time types with Gson? \[closed\]](https://stackoverflow.com/questions/23072733/how-to-serialize-and-deserialize-java-8s-java-time-types-with-gson), which seems helpful to me. You can probably find a lot more and also newer and more up to date. The link I gave earlier should get you started with java.time. Also on that side you can search for more. – Ole V.V. Sep 07 '22 at 07:51
  • 1
    @Marcono1234 I followed the post you suggested, but it's failing for a null date object. Along with the above2, I have another Date deletedAt which i am getting in the event and it's null sometimes. but the custom Adapter that I am registering(it's the same in post) with GSON is failing with ```Error thrown was: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a long but was NULL at line 1 column 4074 path $.supplySourceOperationalDetails.deletedAt```. I am not able to comment on that post due to lack of reputation. Will appreciate some help to mitigate the issue. – Conquistador Sep 22 '22 at 19:34
  • @Conquistador, thanks for mentioning that. The accepted answer of that post indeed had a bug for JSON null handling. I have edited that answer to fix it. – Marcono1234 Sep 22 '22 at 21:21
  • Thanks @Marcono1234, I looked for a while yesterday and found about the JSONToken.NULL and it worked for me. Forgot to update here, but you have already taken care of that. – Conquistador Sep 23 '22 at 19:41

0 Answers0