Added support for serialization of raw maps.

Also, refactored tests for maps in MapTest class.
This commit is contained in:
Inderjeet Singh 2008-10-21 22:37:41 +00:00
parent 9dfa454f6d
commit 3cd665b199
4 changed files with 96 additions and 42 deletions

View File

@ -18,6 +18,7 @@ package com.google.gson;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
@ -327,10 +328,16 @@ final class DefaultTypeAdapters {
InstanceCreator<Map> { InstanceCreator<Map> {
public JsonElement serialize(Map src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(Map src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject map = new JsonObject(); JsonObject map = new JsonObject();
Type childType = new TypeInfoMap(typeOfSrc).getValueType(); Type childGenericType = null;
if (typeOfSrc instanceof ParameterizedType) {
childGenericType = new TypeInfoMap(typeOfSrc).getValueType();
}
for (Iterator iterator = src.entrySet().iterator(); iterator.hasNext(); ) { for (Iterator iterator = src.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry entry = (Map.Entry) iterator.next(); Map.Entry entry = (Map.Entry) iterator.next();
JsonElement valueElement = context.serialize(entry.getValue(), childType); Object value = entry.getValue();
Type childType = (childGenericType == null) ?
childType = value.getClass() : childGenericType;
JsonElement valueElement = context.serialize(value, childType);
map.add(entry.getKey().toString(), valueElement); map.add(entry.getKey().toString(), valueElement);
} }
return map; return map;

View File

@ -43,10 +43,10 @@ public class DefaultMapJsonSerializerTest extends TestCase {
public void testEmptyMapNoTypeSerialization() { public void testEmptyMapNoTypeSerialization() {
Map<String, String> emptyMap = new HashMap<String, String>(); Map<String, String> emptyMap = new HashMap<String, String>();
try { JsonElement element = mapSerializer.serialize(emptyMap, emptyMap.getClass(), null);
mapSerializer.serialize(emptyMap, emptyMap.getClass(), null); assertTrue(element instanceof JsonObject);
fail("Parameterized types need to have a ParameterizedType passed in, not a Class."); JsonObject emptyMapJsonObject = (JsonObject) element;
} catch (IllegalArgumentException expected) { } assertTrue(emptyMapJsonObject.entrySet().isEmpty());
} }
public void testEmptyMapSerialization() { public void testEmptyMapSerialization() {

View File

@ -15,14 +15,6 @@
*/ */
package com.google.gson.functional; 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.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URI; import java.net.URI;
@ -31,14 +23,18 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
/** /**
* Functional test for Json serialization and deserialization for common classes for which default * Functional test for Json serialization and deserialization for common classes for which default
* support is provided in Gson. * support is provided in Gson. The tests for Map types are available in {@link MapTest}.
* *
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch * @author Joel Leitch
@ -114,31 +110,6 @@ public class DefaultTypeAdaptersTest extends TestCase {
assertEquals("EURO", locale.getVariant()); assertEquals("EURO", locale.getVariant());
} }
public void testMapSerialization() {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertTrue(json.contains("\"a\":1"));
assertTrue(json.contains("\"b\":2"));
}
public void testMapDeserialization() {
String json = "{\"a\":1,\"b\":2}";
Type typeOfMap = new TypeToken<Map<String,Integer>>(){}.getType();
Map<String, Integer> target = gson.fromJson(json, typeOfMap);
assertEquals(1, target.get("a").intValue());
assertEquals(2, target.get("b").intValue());
}
public void testMapSerializationEmpty() {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertEquals("{}", json);
}
public void testBigDecimalFieldSerialization() { public void testBigDecimalFieldSerialization() {
ClassWithBigDecimal target = new ClassWithBigDecimal("-122.01e-21"); ClassWithBigDecimal target = new ClassWithBigDecimal("-122.01e-21");
String json = gson.toJson(target); String json = gson.toJson(target);

View File

@ -0,0 +1,76 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson.functional;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.TestCase;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* Functional test for Json serialization and deserialization for Maps
*
* @author Inderjeet Singh
* @author Joel Leitch
*/
public class MapTest extends TestCase {
private Gson gson;
@Override
protected void setUp() throws Exception {
super.setUp();
gson = new Gson();
}
public void testMapSerialization() {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertTrue(json.contains("\"a\":1"));
assertTrue(json.contains("\"b\":2"));
}
public void testMapDeserialization() {
String json = "{\"a\":1,\"b\":2}";
Type typeOfMap = new TypeToken<Map<String,Integer>>(){}.getType();
Map<String, Integer> target = gson.fromJson(json, typeOfMap);
assertEquals(1, target.get("a").intValue());
assertEquals(2, target.get("b").intValue());
}
@SuppressWarnings("unchecked")
public void testRawMapSerialization() {
Map map = new LinkedHashMap();
map.put("a", 1);
map.put("b", "string");
String json = gson.toJson(map);
assertTrue(json.contains("\"a\":1"));
assertTrue(json.contains("\"b\":\"string\""));
}
public void testMapSerializationEmpty() {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertEquals("{}", json);
}
}