Fixed issue 100 by adding support for deserialization of collections of elements that do not implement Comparable.

This commit is contained in:
Inderjeet Singh 2009-03-12 01:59:48 +00:00
parent 3d1f7251c1
commit a595032a90
2 changed files with 37 additions and 7 deletions

View File

@ -31,6 +31,7 @@ import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -79,7 +80,7 @@ final class DefaultTypeAdapters {
private static final StringTypeAdapter STRING_TYPE_ADAPTER = new StringTypeAdapter(); private static final StringTypeAdapter STRING_TYPE_ADAPTER = new StringTypeAdapter();
private static final PropertiesCreator PROPERTIES_CREATOR = new PropertiesCreator(); private static final PropertiesCreator PROPERTIES_CREATOR = new PropertiesCreator();
private static final TreeSetCreator TREE_SET_CREATOR = new TreeSetCreator(); private static final HashSetCreator HASH_SET_CREATOR = new HashSetCreator();
private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER = private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER =
new GregorianCalendarTypeAdapter(); new GregorianCalendarTypeAdapter();
@ -178,8 +179,8 @@ final class DefaultTypeAdapters {
map.register(List.class, COLLECTION_TYPE_ADAPTER); map.register(List.class, COLLECTION_TYPE_ADAPTER);
map.register(Queue.class, COLLECTION_TYPE_ADAPTER); map.register(Queue.class, COLLECTION_TYPE_ADAPTER);
map.register(Set.class, TREE_SET_CREATOR); map.register(Set.class, HASH_SET_CREATOR);
map.register(SortedSet.class, TREE_SET_CREATOR); map.register(SortedSet.class, HASH_SET_CREATOR);
map.register(Properties.class, PROPERTIES_CREATOR); map.register(Properties.class, PROPERTIES_CREATOR);
map.makeUnmodifiable(); map.makeUnmodifiable();
return map; return map;
@ -779,13 +780,13 @@ final class DefaultTypeAdapters {
} }
} }
private static class TreeSetCreator implements InstanceCreator<TreeSet<?>> { private static class HashSetCreator implements InstanceCreator<HashSet<?>> {
public TreeSet<?> createInstance(Type type) { public HashSet<?> createInstance(Type type) {
return new TreeSet<Object>(); return new HashSet<Object>();
} }
@Override @Override
public String toString() { public String toString() {
return TreeSetCreator.class.getSimpleName(); return HashSetCreator.class.getSimpleName();
} }
} }
} }

View File

@ -30,10 +30,12 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.Set;
/** /**
* Functional tests for Json serialization and deserialization of collections. * Functional tests for Json serialization and deserialization of collections.
@ -315,4 +317,31 @@ public class CollectionTest extends TestCase {
return collection; return collection;
} }
} }
private static class Entry {
int value;
Entry() {
this(10);
}
Entry(int value) {
this.value = value;
}
}
public void testSetSerialization() {
Set<Entry> set = new HashSet<Entry>();
set.add(new Entry(1));
set.add(new Entry(2));
String json = gson.toJson(set);
assertTrue(json.contains("1"));
assertTrue(json.contains("2"));
}
public void testSetDeserialization() {
String json = "[{value:1},{value:2}]";
Type type = new TypeToken<Set<Entry>>() {}.getType();
Set<Entry> set = gson.fromJson(json, type);
assertEquals(2, set.size());
for (Entry entry : set) {
assertTrue(entry.value == 1 || entry.value == 2);
}
}
} }