Fixed issue 87 by adding support for deserialization of java.util.Properties

This commit is contained in:
Inderjeet Singh 2008-12-31 01:01:57 +00:00
parent 6fe2fdf7a0
commit cc334475b0
3 changed files with 27 additions and 10 deletions

View File

@ -36,6 +36,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Queue; import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
@ -77,6 +78,7 @@ final class DefaultTypeAdapters {
private static final ShortTypeAdapter SHORT_TYPE_ADAPTER = new ShortTypeAdapter(); private static final ShortTypeAdapter SHORT_TYPE_ADAPTER = new ShortTypeAdapter();
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 TreeSetCreator TREE_SET_CREATOR = new TreeSetCreator(); private static final TreeSetCreator TREE_SET_CREATOR = new TreeSetCreator();
// The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS // The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS
@ -173,6 +175,7 @@ final class DefaultTypeAdapters {
map.register(Set.class, TREE_SET_CREATOR); map.register(Set.class, TREE_SET_CREATOR);
map.register(SortedSet.class, TREE_SET_CREATOR); map.register(SortedSet.class, TREE_SET_CREATOR);
map.register(Properties.class, PROPERTIES_CREATOR);
map.makeUnmodifiable(); map.makeUnmodifiable();
return map; return map;
} }
@ -445,6 +448,12 @@ final class DefaultTypeAdapters {
} }
} }
static class PropertiesCreator implements InstanceCreator<Properties> {
public Properties createInstance(Type type) {
return new Properties();
}
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static class MapTypeAdapter implements JsonSerializer<Map>, JsonDeserializer<Map>, static class MapTypeAdapter implements JsonSerializer<Map>, JsonDeserializer<Map>,
InstanceCreator<Map> { InstanceCreator<Map> {

View File

@ -19,6 +19,7 @@ package com.google.gson;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Map; import java.util.Map;
import java.util.Properties;
/** /**
* A convenience object for retrieving the map type information. * A convenience object for retrieving the map type information.
@ -27,24 +28,31 @@ import java.util.Map;
* @author Joel Leitch * @author Joel Leitch
*/ */
final class TypeInfoMap { final class TypeInfoMap {
private final ParameterizedType mapType; private final Type keyType;
private final Type valueType;
public TypeInfoMap(Type mapType) { public TypeInfoMap(Type mapType) {
if (!(mapType instanceof ParameterizedType)) { if (mapType instanceof Class && Properties.class.isAssignableFrom((Class<?>) mapType)) {
keyType = String.class;
valueType = String.class;
} else if (mapType instanceof ParameterizedType) {
TypeInfo rawType = new TypeInfo(mapType);
Preconditions.checkArgument(Map.class.isAssignableFrom(rawType.getRawClass()));
ParameterizedType paramType = (ParameterizedType) mapType;
keyType = paramType.getActualTypeArguments()[0];
valueType = paramType.getActualTypeArguments()[1];
} else {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Map objects need to be parameterized unless you use a custom serializer. " "Map objects need to be parameterized unless you use a custom serializer. "
+ "Use the com.google.gson.reflect.TypeToken to extract the ParameterizedType."); + "Use the com.google.gson.reflect.TypeToken to extract the ParameterizedType.");
} }
TypeInfo rawType = new TypeInfo(mapType);
Preconditions.checkArgument(Map.class.isAssignableFrom(rawType.getRawClass()));
this.mapType = (ParameterizedType) mapType;
} }
public Type getKeyType() { public Type getKeyType() {
return mapType.getActualTypeArguments()[0]; return keyType;
} }
public Type getValueType() { public Type getValueType() {
return mapType.getActualTypeArguments()[1]; return valueType;
} }
} }

View File

@ -262,7 +262,7 @@ public class DefaultTypeAdaptersTest extends TestCase {
public void testPropertiesSerialization() { public void testPropertiesSerialization() {
Properties props = new Properties(); Properties props = new Properties();
props.put("foo", "bar"); props.setProperty("foo", "bar");
String json = gson.toJson(props); String json = gson.toJson(props);
System.out.println(json); System.out.println(json);
} }
@ -270,6 +270,6 @@ public class DefaultTypeAdaptersTest extends TestCase {
public void testPropertiesDeserialization() { public void testPropertiesDeserialization() {
String json = "{foo:'bar'}"; String json = "{foo:'bar'}";
Properties props = gson.fromJson(json, Properties.class); Properties props = gson.fromJson(json, Properties.class);
assertEquals("bar", props.get("foo")); assertEquals("bar", props.getProperty("foo"));
} }
} }