2008-10-22 00:37:41 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-01-06 08:09:19 +01:00
|
|
|
|
2008-10-22 00:37:41 +02:00
|
|
|
package com.google.gson.functional;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
2008-11-14 00:40:10 +01:00
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.InstanceCreator;
|
2008-10-22 00:37:41 +02:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
|
2009-03-03 22:09:07 +01:00
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.Collection;
|
2009-03-12 21:54:00 +01:00
|
|
|
import java.util.HashMap;
|
2009-03-03 22:09:07 +01:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
2008-10-22 00:37:41 +02:00
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
|
|
|
|
2010-08-20 18:27:46 +02:00
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
2008-10-22 00:37:41 +02:00
|
|
|
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);
|
|
|
|
}
|
2008-12-01 00:01:14 +01:00
|
|
|
|
|
|
|
public void testMapDeserializationEmpty() {
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
|
|
|
Map<String, Integer> map = gson.fromJson("{}", typeOfMap);
|
|
|
|
assertTrue(map.isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMapSerializationWithNullValue() {
|
|
|
|
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
|
|
|
|
map.put("abc", null);
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
|
|
|
String json = gson.toJson(map, typeOfMap);
|
|
|
|
|
|
|
|
// Maps are represented as JSON objects, so ignoring null field
|
|
|
|
assertEquals("{}", json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMapDeserializationWithNullValue() {
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
|
|
|
Map<String, Integer> map = gson.fromJson("{\"abc\":null}", typeOfMap);
|
|
|
|
assertEquals(1, map.size());
|
|
|
|
assertNull(map.get("abc"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMapSerializationWithNullValueButSerializeNulls() {
|
|
|
|
gson = new GsonBuilder().serializeNulls().create();
|
|
|
|
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
|
|
|
|
map.put("abc", null);
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
|
|
|
String json = gson.toJson(map, typeOfMap);
|
|
|
|
|
|
|
|
assertEquals("{\"abc\":null}", json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMapSerializationWithNullKey() {
|
|
|
|
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
|
|
|
|
map.put(null, 123);
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
|
|
|
String json = gson.toJson(map, typeOfMap);
|
|
|
|
|
|
|
|
assertEquals("{\"null\":123}", json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMapDeserializationWithNullKey() {
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, Integer>>() {}.getType();
|
|
|
|
Map<String, Integer> map = gson.fromJson("{\"null\":123}", typeOfMap);
|
|
|
|
assertEquals(1, map.size());
|
|
|
|
assertNull(map.get(null));
|
|
|
|
}
|
2009-01-06 08:09:19 +01:00
|
|
|
|
|
|
|
public void testMapSerializationWithIntegerKeys() {
|
|
|
|
Map<Integer, String> map = new LinkedHashMap<Integer, String>();
|
|
|
|
map.put(123, "456");
|
|
|
|
Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType();
|
|
|
|
String json = gson.toJson(map, typeOfMap);
|
|
|
|
|
|
|
|
assertEquals("{\"123\":\"456\"}", json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMapDeserializationWithIntegerKeys() {
|
|
|
|
Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType();
|
|
|
|
Map<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
|
|
|
|
assertEquals(1, map.size());
|
|
|
|
assertTrue(map.containsKey(123));
|
|
|
|
assertEquals("456", map.get(123));
|
|
|
|
}
|
2008-11-14 03:11:46 +01:00
|
|
|
|
|
|
|
public void testParameterizedMapSubclassSerialization() {
|
|
|
|
MyParameterizedMap<String, String> map = new MyParameterizedMap<String, String>();
|
|
|
|
map.put("a", "b");
|
|
|
|
Type type = new TypeToken<MyParameterizedMap<String, String>>() {}.getType();
|
|
|
|
String json = gson.toJson(map, type);
|
|
|
|
assertTrue(json.contains("\"a\":\"b\""));
|
|
|
|
}
|
|
|
|
|
2010-08-20 18:27:46 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
2008-11-14 03:11:46 +01:00
|
|
|
public void testParameterizedMapSubclassDeserialization() {
|
|
|
|
Type type = new TypeToken<MyParameterizedMap<String, Integer>>() {}.getType();
|
|
|
|
Gson gson = new GsonBuilder().registerTypeAdapter(type,
|
|
|
|
new InstanceCreator<MyParameterizedMap>() {
|
|
|
|
public MyParameterizedMap createInstance(Type type) {
|
|
|
|
return new MyParameterizedMap();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
String json = "{\"a\":1,\"b\":2}";
|
|
|
|
MyParameterizedMap<String, Integer> map = gson.fromJson(json, type);
|
2008-11-14 03:17:19 +01:00
|
|
|
assertEquals(1, map.get("a").intValue());
|
|
|
|
assertEquals(2, map.get("b").intValue());
|
2008-11-14 03:11:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class MyParameterizedMap<K, V> extends LinkedHashMap<K, V> {
|
2008-11-15 06:13:05 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2009-09-23 19:45:16 +02:00
|
|
|
@SuppressWarnings("unused")
|
2009-09-23 21:25:50 +02:00
|
|
|
int foo = 10;
|
2008-11-14 03:11:46 +01:00
|
|
|
}
|
2008-11-14 00:40:10 +01:00
|
|
|
|
|
|
|
public void testMapSubclassSerialization() {
|
|
|
|
MyMap map = new MyMap();
|
|
|
|
map.put("a", "b");
|
2008-11-14 03:11:46 +01:00
|
|
|
String json = gson.toJson(map, MyMap.class);
|
2008-11-14 00:40:10 +01:00
|
|
|
assertTrue(json.contains("\"a\":\"b\""));
|
|
|
|
}
|
|
|
|
|
2010-06-22 01:26:06 +02:00
|
|
|
public void testMapStandardSubclassDeserialization() {
|
|
|
|
String json = "{a:'1',b:'2'}";
|
|
|
|
Type type = new TypeToken<LinkedHashMap<String, String>>() {}.getType();
|
|
|
|
LinkedHashMap<String, Integer> map = gson.fromJson(json, type);
|
|
|
|
assertEquals("1", map.get("a"));
|
|
|
|
assertEquals("2", map.get("b"));
|
|
|
|
}
|
|
|
|
|
2008-11-14 21:52:57 +01:00
|
|
|
public void disable_testMapSubclassDeserialization() {
|
2008-12-14 08:12:04 +01:00
|
|
|
Gson gson = new GsonBuilder().registerTypeAdapter(MyMap.class, new InstanceCreator<MyMap>() {
|
2008-11-14 00:40:10 +01:00
|
|
|
public MyMap createInstance(Type type) {
|
|
|
|
return new MyMap();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
String json = "{\"a\":1,\"b\":2}";
|
|
|
|
MyMap map = gson.fromJson(json, MyMap.class);
|
|
|
|
assertEquals("1", map.get("a"));
|
|
|
|
assertEquals("2", map.get("b"));
|
|
|
|
}
|
|
|
|
|
2009-03-03 22:09:07 +01:00
|
|
|
/**
|
|
|
|
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=99
|
|
|
|
*/
|
|
|
|
private static class ClassWithAMap {
|
|
|
|
Map<String, String> map = new TreeMap<String, String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=99
|
|
|
|
*/
|
|
|
|
public void testMapSerializationWithNullValues() {
|
|
|
|
ClassWithAMap target = new ClassWithAMap();
|
|
|
|
target.map.put("name1", null);
|
|
|
|
target.map.put("name2", "value2");
|
|
|
|
String json = gson.toJson(target);
|
|
|
|
assertFalse(json.contains("name1"));
|
|
|
|
assertTrue(json.contains("name2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created in response to http://code.google.com/p/google-gson/issues/detail?id=99
|
|
|
|
*/
|
|
|
|
public void testMapSerializationWithNullValuesSerialized() {
|
|
|
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
|
|
|
ClassWithAMap target = new ClassWithAMap();
|
|
|
|
target.map.put("name1", null);
|
|
|
|
target.map.put("name2", "value2");
|
|
|
|
String json = gson.toJson(target);
|
|
|
|
assertTrue(json.contains("name1"));
|
|
|
|
assertTrue(json.contains("name2"));
|
|
|
|
}
|
|
|
|
|
2008-12-14 08:12:04 +01:00
|
|
|
public void testMapSerializationWithWildcardValues() {
|
|
|
|
Map<String, ? extends Collection<? extends Integer>> map =
|
|
|
|
new LinkedHashMap<String, Collection<Integer>>();
|
|
|
|
map.put("test", null);
|
|
|
|
Type typeOfMap =
|
|
|
|
new TypeToken<Map<String, ? extends Collection<? extends Integer>>>() {}.getType();
|
|
|
|
String json = gson.toJson(map, typeOfMap);
|
|
|
|
|
|
|
|
assertEquals("{}", json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMapDeserializationWithWildcardValues() {
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, ? extends Long>>() {}.getType();
|
|
|
|
Map<String, ? extends Long> map = gson.fromJson("{\"test\":123}", typeOfMap);
|
|
|
|
assertEquals(1, map.size());
|
|
|
|
assertEquals(new Long(123L), map.get("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-14 00:40:10 +01:00
|
|
|
private static class MyMap extends LinkedHashMap<String, String> {
|
2008-11-15 06:13:05 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2009-09-23 19:45:16 +02:00
|
|
|
@SuppressWarnings("unused")
|
2009-09-23 21:25:50 +02:00
|
|
|
int foo = 10;
|
2008-11-14 00:40:10 +01:00
|
|
|
}
|
2009-03-12 21:54:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* From bug report http://code.google.com/p/google-gson/issues/detail?id=95
|
|
|
|
*/
|
|
|
|
public void testMapOfMapSerialization() {
|
|
|
|
Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
|
|
|
|
Map<String, String> nestedMap = new HashMap<String, String>();
|
|
|
|
nestedMap.put("1", "1");
|
|
|
|
nestedMap.put("2", "2");
|
|
|
|
map.put("nestedMap", nestedMap);
|
|
|
|
String json = gson.toJson(map);
|
|
|
|
assertTrue(json.contains("nestedMap"));
|
|
|
|
assertTrue(json.contains("\"1\":\"1\""));
|
|
|
|
assertTrue(json.contains("\"2\":\"2\""));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From bug report http://code.google.com/p/google-gson/issues/detail?id=95
|
|
|
|
*/
|
|
|
|
public void testMapOfMapDeserialization() {
|
|
|
|
String json = "{nestedMap:{'2':'2','1':'1'}}";
|
|
|
|
Type type = new TypeToken<Map<String, Map<String, String>>>(){}.getType();
|
|
|
|
Map<String, Map<String, String>> map = gson.fromJson(json, type);
|
|
|
|
Map<String, String> nested = map.get("nestedMap");
|
|
|
|
assertEquals("1", nested.get("1"));
|
|
|
|
assertEquals("2", nested.get("2"));
|
|
|
|
}
|
2010-08-20 07:43:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* From bug report http://code.google.com/p/google-gson/issues/detail?id=178
|
|
|
|
*/
|
|
|
|
public void testMapWithQuotes() {
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
map.put("a\"b", "c\"d");
|
|
|
|
String json = gson.toJson(map);
|
|
|
|
assertEquals("{\"a\\\"b\":\"c\\\"d\"}", json);
|
|
|
|
}
|
2008-10-22 00:37:41 +02:00
|
|
|
}
|