diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index 8594559d..c8d41154 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -36,6 +36,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Properties; import java.util.Queue; import java.util.Set; import java.util.SortedSet; @@ -77,6 +78,7 @@ final class DefaultTypeAdapters { private static final ShortTypeAdapter SHORT_TYPE_ADAPTER = new ShortTypeAdapter(); 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(); // 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(SortedSet.class, TREE_SET_CREATOR); + map.register(Properties.class, PROPERTIES_CREATOR); map.makeUnmodifiable(); return map; } @@ -445,6 +448,12 @@ final class DefaultTypeAdapters { } } + static class PropertiesCreator implements InstanceCreator { + public Properties createInstance(Type type) { + return new Properties(); + } + } + @SuppressWarnings("unchecked") static class MapTypeAdapter implements JsonSerializer, JsonDeserializer, InstanceCreator { diff --git a/gson/src/main/java/com/google/gson/TypeInfoMap.java b/gson/src/main/java/com/google/gson/TypeInfoMap.java index ba9db948..ea8eb07b 100644 --- a/gson/src/main/java/com/google/gson/TypeInfoMap.java +++ b/gson/src/main/java/com/google/gson/TypeInfoMap.java @@ -19,6 +19,7 @@ package com.google.gson; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Map; +import java.util.Properties; /** * A convenience object for retrieving the map type information. @@ -27,24 +28,31 @@ import java.util.Map; * @author Joel Leitch */ final class TypeInfoMap { - private final ParameterizedType mapType; - + private final Type keyType; + private final Type valueType; + 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( "Map objects need to be parameterized unless you use a custom serializer. " + "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() { - return mapType.getActualTypeArguments()[0]; + return keyType; } public Type getValueType() { - return mapType.getActualTypeArguments()[1]; + return valueType; } } diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index 6b732b6a..9773331c 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -262,7 +262,7 @@ public class DefaultTypeAdaptersTest extends TestCase { public void testPropertiesSerialization() { Properties props = new Properties(); - props.put("foo", "bar"); + props.setProperty("foo", "bar"); String json = gson.toJson(props); System.out.println(json); } @@ -270,6 +270,6 @@ public class DefaultTypeAdaptersTest extends TestCase { public void testPropertiesDeserialization() { String json = "{foo:'bar'}"; Properties props = gson.fromJson(json, Properties.class); - assertEquals("bar", props.get("foo")); + assertEquals("bar", props.getProperty("foo")); } }