sefasdfasd / google-gson

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

deserializing a Set requires elements to implement Comparable, when ordering is not required #100

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. See attached beans Tree and Monkey
2. Run this test:

    Tree tree = new Tree();
    tree.getMonkeys().add(new Monkey());
    tree.getMonkeys().add(new Monkey());

    String serialised = json.toJson(tree);
    Tree newTree = json.fromJson(serialised, Tree.class);

What is the expected output? What do you see instead?

Deserialize fails, Caused by: java.lang.ClassCastException:
com.jpmorgan.ibmlbus.beans.serialise.Monkey cannot be cast to
java.lang.Comparable
    at java.util.TreeMap.put(Unknown Source)
    at java.util.TreeSet.add(Unknown Source)
    at
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTyp
eAdapters.java:404)
    at
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTyp
eAdapters.java:369)
    at
com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExc
eptionWrapper.java:50)
    ... 27 more

The point is that Monkeys have no natural ordering. We should be able to
use them in a Set though, because uniqueness is fine.

What version of the product are you using? On what operating system?
1.2.3

Please provide any additional information below.

Original issue reported on code.google.com by aaron.b...@gmail.com on 12 Feb 2009 at 4:08

Attachments:

GoogleCodeExporter commented 9 years ago
Hi - could you please delete the package names from the attached beans? My 
error.

Original comment by aaron.b...@gmail.com on 12 Feb 2009 at 4:22

GoogleCodeExporter commented 9 years ago
Sorry, I do not have the ability to edit the original bug report. 

Original comment by inder123 on 12 Mar 2009 at 2:00

GoogleCodeExporter commented 9 years ago
Fixed this bug in r394 by using HashSet instead of TreeSet while instantiating 
sets
during deserialization

Original comment by inder123 on 12 Mar 2009 at 2:00

GoogleCodeExporter commented 9 years ago
Updated in r395 to ensure that sorted sets continue to use TreeSets and HashSet 
is
used for other sets.

Original comment by inder123 on 12 Mar 2009 at 6:17

GoogleCodeExporter commented 9 years ago

Original comment by inder123 on 27 Mar 2009 at 7:39