Fixed issue 167 by adding support for TreeSet deserialization. Added tests for serialization/deserialization of treesets.

This commit is contained in:
Inderjeet Singh 2010-01-08 19:14:42 +00:00
parent 058f7344db
commit 7079799890
2 changed files with 18 additions and 0 deletions

View File

@ -195,6 +195,7 @@ final class DefaultTypeAdapters {
map.register(Set.class, HASH_SET_CREATOR);
map.register(SortedSet.class, TREE_SET_CREATOR);
map.register(TreeSet.class, TREE_SET_CREATOR);
map.register(Properties.class, PROPERTIES_CREATOR);
map.makeUnmodifiable();
return map;

View File

@ -18,9 +18,11 @@ package com.google.gson.functional;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import junit.framework.TestCase;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
@ -37,6 +39,7 @@ import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;
import java.util.UUID;
/**
@ -390,4 +393,18 @@ public class DefaultTypeAdaptersTest extends TestCase {
Properties props = gson.fromJson(json, Properties.class);
assertEquals("bar", props.getProperty("foo"));
}
public void testTreeSetSerialization() {
TreeSet<String> treeSet = new TreeSet<String>();
treeSet.add("Value1");
String json = gson.toJson(treeSet);
assertEquals("[\"Value1\"]", json);
}
public void testTreeSetDeserialization() {
String json = "['Value1']";
Type type = new TypeToken<TreeSet<String>>() {}.getType();
TreeSet<String> treeSet = gson.fromJson(json, type);
assertTrue(treeSet.contains("Value1"));
}
}