1

I have a JSON date which I'm attempting to deserialise:

{
   "created_at":"2015-05-26 10:14:32"
}

However, the space appears to be causing an issue with GSON and it throws the following error:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 160 path $.created_at

com.google.gson.JsonSyntaxException: 2015-05-26

Caused by: java.text.ParseException: Unparseable date: "2015-05-26" (at offset 10)


Here is my GSON configuration:

Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            .create();

My model:

public class MyModel extends ReamlObject {

    @SerializedName("created_at")
    private Date createdAt = new Date(System.currentTimeMillis());
}

Finally

gson.fromJson(json, MyModel.class);

Does anyone have any solutions that allow for the space within date? I've tried a custom date deserialiser but that didn't make any difference either.

  • The space isn't a problem... and would never be, inside or outside of quotes. What you posted is valid json. There must be more to the issue than what you are posting. Can you provide the context for this json? – 323go Jun 25 '15 at 17:43
  • Sorry, I forgot to mention the other error messages which is why I assumed the space - please see my edit. –  Jun 25 '15 at 17:46

3 Answers3

0

From here: malformedjsonexception I do believe that you have a false "space" in your JSON, that the GSON sees as invalid:

GSON throws that particular error when there's extra characters after the end of the object that aren't whitespace, and it defines whitespace very narrowly (as the JSON spec does) - only \t, \n, \r, and space count as whitespace. In particular, note that trailing NUL (\0) characters do not count as whitespace and will cause this error.

Also, from the Gson user guide, we read:

... This is often the case when dealing with library classes (DateTime, etc). Gson allows you to register your own custom serializers and deserializers. This is done by defining two parts: ...

Meaning that you must do this

Community
  • 1
  • 1
Bonatti
  • 2,778
  • 5
  • 23
  • 42
0

Try using this format. With 'T' replacing space.

  Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
0

It may be your text input,

check this unit test is working just right:

public class ParesDateTest{
    @Test
    public void test(){
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .setExclusionStrategies(new ExclusionStrategy() {
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        return false;
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                        return false;
                    }
                })
                .create();

        MyModel o = gson.fromJson("{\n" +
                "   \"created_at\":\"2015-03-26 10:14:32\"\n" +
                "}",MyModel.class);
        Assert.assertNotNull(o);


    }

    public class MyModel  {

        @SerializedName("created_at")
        private Date createdAt = new Date(System.currentTimeMillis());
    }
}  
dgofactory
  • 348
  • 5
  • 13
  • Thanks for the heads up, it was indeed my input in the end. I changed the fromJson input to a JsonElement rather than a String and it now works perfectly. –  Jun 25 '15 at 18:25