Bugfix für urbanairship-java SDK JsonParseExceptionBugfix for urbanairship-java SDK JsonParseException

There (still) exists a bug in urbanairship-java SDK related to parsing valid ISO8601 dates. The original bug report can be found here: GSon date parsing error.

The Cause

ISO8601 states that for date-time strings the separator “T” can be left out. Unfortunately the urbanairship-java SDK expects only fully qualified ISO8601 strings. Thus, when it receives a (perfectly valid) date-time string such as the one shown below it throws a JsonParseException:

{
    "device_token": "XXX",
    "last_registration": "2012-12-27 08:24:33",
    "tz": null,
    "tags": [],
    "alias": null,
    "quiettime": {
        "start": null,
        "end": null
    },
    "active": true,
    "badge": 0
}

The stacktrace caused by the JsonParseException thrown looks similiar like the one shown below:

com.google.gson.JsonParseException: 2012-12-27 08:24:33
	at com.urbanairship.GsonFactory$CalendarAdapter.deserialize(GsonFactory.java:54)
	at com.urbanairship.GsonFactory$CalendarAdapter.deserialize(GsonFactory.java:1)
	at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
	at com.google.gson.Gson.fromJson(Gson.java:795)
	at com.google.gson.Gson.fromJson(Gson.java:761)
	at com.google.gson.Gson.fromJson(Gson.java:710)
	at com.google.gson.Gson.fromJson(Gson.java:682)
	at com.urbanairship.UrbanAirshipClient.fromJson(UrbanAirshipClient.java:527)
	at com.urbanairship.UrbanAirshipClient.fromJson(UrbanAirshipClient.java:385)
	at com.urbanairship.UrbanAirshipClient.get(UrbanAirshipClient.java:507)
	at com.urbanairship.UrbanAirshipClient.getDevice(UrbanAirshipClient.java:250)
	... many more

The problem is caused in the fromJSON function in Urbanairshipclient.java:

protected <T> T fromJson(final String json, final Class<T> clazz) {
  Gson gson = GsonFactory.create();
  return gson.fromJson(json, clazz); // PROBLEM HERE
}

The Fix

Luckily the fix is pretty easy. Open ISO8601.java and update the PATTERN variable as follows:

public class ISO8601
{
	//public static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ"; //BUG HERE
	public static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; //BUG FIXED    
        ...
}

That’s it. I’ve updated the bug report accordingly.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.