JamesDeCarlo / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Deserialization of a specified Json into an object of type Map<String, Set<Long>> #355

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a Json Object with data of type Map<String, Set<Long>>
2. Deserialize above created Json object back to Map<String, Set<Long>> type
3.

What is the expected output? What do you see instead?
Expected:
Same Map has to be reproduced.

Actual:
Exception in thread "main" com.google.gson.JsonParseException: The 
JsonDeserializer 
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@1eec612 failed to 
deserialize json object "[1313490105362, 1313490105363]" given the type 
java.util.TreeSet<java.lang.Long>
    at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:64)
    at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
    at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:80)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)
    at com.google.gson.JsonDeserializationContextDefault.fromJsonPrimitive(JsonDeserializationContextDefault.java:85)
    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:56)
    at com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:68)
    at com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33)
    at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51)
    at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)
    at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:80)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)
    at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:76)
    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:54)
    at com.google.gson.Gson.fromJson(Gson.java:551)
    at com.google.gson.Gson.fromJson(Gson.java:498)
    at com.google.gson.Gson.fromJson(Gson.java:467)
    at com.google.gson.Gson.fromJson(Gson.java:417)
    at TestClass.main(TestClass.java:50)
Caused by: java.lang.IllegalStateException: This is not a JSON Array.
    at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:99)
    at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:659)
    at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:624)
    at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51)
    ... 18 more

What version of the product are you using? On what operating system?
gson-1.7.1

Please provide any additional information below.

Original issue reported on code.google.com by vinayasi...@gmail.com on 16 Aug 2011 at 10:18

GoogleCodeExporter commented 9 years ago
I couldn't reproduce this.

    Map<String, Set<Long>> m = new HashMap<String, Set<Long>>();
    m.put("a", new TreeSet<Long>(Arrays.asList(1L, 2L, 3L)));
    m.put("b", new TreeSet<Long>(Arrays.asList(4L, 5L, 6L)));

    String s = new Gson().toJson(m);
    System.out.println(s);

    Object value = new Gson().fromJson(s, new TypeToken<Map<String, Set<Long>>>() {}.getType());
    System.out.println(value);

Original comment by limpbizkit on 1 Oct 2011 at 5:10

GoogleCodeExporter commented 9 years ago
Issue is string representation of "JSONObject.toString()" is different from the 
"Gson().toString()". For example

Gson().toString() gives below output:

{"b":[4,5,6],"a":[1,2,3]}

JSONObject.toString() gives below output:

{"b":"[4, 5, 6]","a":"[1, 2, 3]"}

Code for reproducing the issue: 

    Map<String, Set<Long>> m = new HashMap<String, Set<Long>>();
    m.put("a", new TreeSet<Long>(Arrays.asList(1L, 2L, 3L)));
    m.put("b", new TreeSet<Long>(Arrays.asList(4L, 5L, 6L)));

    JSONObject json = new JSONObject(m);
    String s = json.toString();
    System.out.println(s);

    Object value = new Gson().fromJson(s, new TypeToken<Map<String, Set<Long>>>() {}.getType());
    System.out.println(value);

Original comment by vinayasi...@gmail.com on 2 Oct 2011 at 11:54