Text '2020-10-18T19:24:27.510476' could not be parsed at index 26

Just getting started but falling at the first hurdle unfortunately.

I’ve generated a Java client from the Swagger spec and attempting to retrieve a session token. The call is successful, as in I’m getting a response with a token and session expiry time, but it’s failing to parse the expiry time. I get the following exception:

Exception in thread "main" org.threeten.bp.format.DateTimeParseException: Text '2020-10-18T19:24:27.510476' could not be parsed at index 26
at org.threeten.bp.format.DateTimeFormatter.parseToBuilder(DateTimeFormatter.java:1587)
at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1491)
at org.threeten.bp.OffsetDateTime.parse(OffsetDateTime.java:359)
at io.swagger.client.JSON$OffsetDateTimeTypeAdapter.read(JSON.java:184)
at io.swagger.client.JSON$OffsetDateTimeTypeAdapter.read(JSON.java:148)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at io.swagger.client.JSON.deserialize(JSON.java:134)
at io.swagger.client.ApiClient.deserialize(ApiClient.java:710)
at io.swagger.client.ApiClient.handleResponse(ApiClient.java:913)
at io.swagger.client.ApiClient.execute(ApiClient.java:840)
at io.swagger.client.api.SessionsApi.createSessionWithHttpInfo(SessionsApi.java:145)
at io.swagger.client.api.SessionsApi.createSession(SessionsApi.java:131)

Seems it’s not able to parse the JSON response into an OffsetDateTime using the defined DateTimeFormatter:

DateTimeFormatter.ISO_OFFSET_DATE_TIME

I don’t know why it’s trying to parse anything at index 26 in that string, my understanding is the DateTimeFormatter should be adding the timezone offset that will appear at that position in the string.

This is in the generated JSON.java file btw, not my own code, hence the request for help.

It would be helpful if someone from Smarkets could respond to this… it’s been confirmed to me a couple of weeks ago via email that this is an issue your end.

Still not found any solution? A couple of weeks is pretty long time.

Hi both. I just wanted to update you to say that while the issue is not yet fixed, our tech team is aware of this issue. We’ll update via this thread once it’s fixed.

For now I’m working around this by adding the following to the “read” method in JSON.java:

if (!date.endsWith("Z")) {
    date = date.concat("Z");
}

So the method now looks like this:

public OffsetDateTime read(JsonReader in) throws IOException {
        switch (in.peek()) {
            case NULL:
                in.nextNull();
                return null;
            default:
                String date = in.nextString();
                if (date.endsWith("+0000")) {
                    date = date.substring(0, date.length()-5) + "Z";
                }
                if (!date.endsWith("Z")) {
                    date = date.concat("Z");
                }
                return OffsetDateTime.parse(date, formatter);
        }
    }