3

I have an API that takes requests and generates returned JSON. That JSON is generated using the object below and my utility class.

When the JSON is returned to the application, I actually use the exact same utility class to serialize the JSON back into the object.

However, I get an exception (see below). How do I get around that? I've tried just about everything I can find online, I haven't had any luck.

Object:

public class MyPerson() {
    private String name;
    private Date lastEdited;
}

Utility class for converting:

public class GsonUtils {

    private static final Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'hh:mm:ss")
            .create();

    public static String deserializeObjectToJSON(Object obj) {
        return gson.toJson(obj);
    }

    public static <T> Object serializeObjectFromJSON(String json, Class<T> classType) {
        return gson.fromJson(json, classType);
    }

    public static <T> List<T> serializeListOfObjectsFromJSON(String json, Type listType) {
        return gson.fromJson(json, listType);
    }
}

Error:

Caused by: java.text.ParseException: Failed to parse date ["2019-02-12T12:00:00.0"]: No time zone indicator     at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274) ~[gson-2.8.5.jar:na]    at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85) ~[gson-2.8.5.jar:na]    ... 66 common frames omitted

Edit:

Took the suggestions and updated the utility class, same error:

Caused by: java.text.ParseException: Failed to parse date ["2019-02-12T12:00:00.0"]: No time zone indicator
    at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274) ~[gson-2.8.5.jar:na]
    at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85) ~[gson-2.8.5.jar:na]
    ... 66 common frames omitted
user10776719
  • 301
  • 4
  • 15
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 14 '19 at 20:46
  • Good call - force of habit to just roll with java.util.Date – user10776719 Feb 15 '19 at 17:36

3 Answers3

2

yyyy-MM-dd'T'hh:mm:ss.S
put this as your Date format

Kiran S
  • 78
  • 1
  • 10
2

Figured out the issue

I changed the format to -

private static final Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'")
        .create();

This link helped me figure out my issue (UTC time formatting was what I wanted):

Java Date to UTC using gson

user10776719
  • 301
  • 4
  • 15
0

From your error, it is evident that the JSON which is returned to your application has timezone information as well "2019-02-12T12:00:00.**0** (The .0 in the end means local timezone)

Caused by: java.text.ParseException: Failed to parse date ["2019-02-12T12:00:00.0"]: No time zone indicator     at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274) ~[gson-2.8.5.jar:na]    at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85) ~[gson-2.8.5.jar:na]    ... 66 common frames omitted

In order to serialize this, you have to update the same in the GsonBuilder.

 private static final Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'hh:mm:ss.S")
            .create();