|
The MapTest.java Java example source code
/*
* 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.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import com.google.gson.common.TestTypes;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.reflect.TypeToken;
import junit.framework.TestCase;
/**
* 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();
map.put("a", 1);
map.put("b", 2);
Type typeOfMap = new TypeToken<Map() {}.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(){}.getType();
Map<String, Integer> target = gson.fromJson(json, typeOfMap);
assertEquals(1, target.get("a").intValue());
assertEquals(2, target.get("b").intValue());
}
@SuppressWarnings({"unchecked", "rawtypes"})
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();
Type typeOfMap = new TypeToken<Map() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertEquals("{}", json);
}
public void testMapDeserializationEmpty() {
Type typeOfMap = new TypeToken<Map() {}.getType();
Map<String, Integer> map = gson.fromJson("{}", typeOfMap);
assertTrue(map.isEmpty());
}
public void testMapSerializationWithNullValue() {
Map<String, Integer> map = new LinkedHashMap();
map.put("abc", null);
Type typeOfMap = new TypeToken<Map() {}.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() {}.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();
map.put("abc", null);
Type typeOfMap = new TypeToken<Map() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertEquals("{\"abc\":null}", json);
}
public void testMapSerializationWithNullKey() {
Map<String, Integer> map = new LinkedHashMap();
map.put(null, 123);
Type typeOfMap = new TypeToken<Map() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertEquals("{\"null\":123}", json);
}
public void testMapDeserializationWithNullKey() {
Type typeOfMap = new TypeToken<Map() {}.getType();
Map<String, Integer> map = gson.fromJson("{\"null\":123}", typeOfMap);
assertEquals(1, map.size());
assertEquals(123, map.get("null").intValue());
assertNull(map.get(null));
map = gson.fromJson("{null:123}", typeOfMap);
assertEquals(1, map.size());
assertEquals(123, map.get("null").intValue());
assertNull(map.get(null));
}
public void testMapSerializationWithIntegerKeys() {
Map<Integer, String> map = new LinkedHashMap();
map.put(123, "456");
Type typeOfMap = new TypeToken<Map() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertEquals("{\"123\":\"456\"}", json);
}
public void testMapDeserializationWithIntegerKeys() {
Type typeOfMap = new TypeToken<Map() {}.getType();
Map<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
}
public void testMapDeserializationWithUnquotedIntegerKeys() {
Type typeOfMap = new TypeToken<Map() {}.getType();
Map<Integer, String> map = gson.fromJson("{123:\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
}
public void testMapDeserializationWithLongKeys() {
long longValue = 9876543210L;
String json = String.format("{\"%d\":\"456\"}", longValue);
Type typeOfMap = new TypeToken<Map() {}.getType();
Map<Long, String> map = gson.fromJson(json, typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(longValue));
assertEquals("456", map.get(longValue));
}
public void testMapDeserializationWithUnquotedLongKeys() {
long longKey = 9876543210L;
String json = String.format("{%d:\"456\"}", longKey);
Type typeOfMap = new TypeToken<Map() {}.getType();
Map<Long, String> map = gson.fromJson(json, typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(longKey));
assertEquals("456", map.get(longKey));
}
public void testHashMapDeserialization() throws Exception {
Type typeOfMap = new TypeToken<HashMap() {}.getType();
HashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
}
public void testSortedMap() throws Exception {
Type typeOfMap = new TypeToken<SortedMap() {}.getType();
SortedMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
}
public void testConcurrentMap() throws Exception {
Type typeOfMap = new TypeToken<ConcurrentMap() {}.getType();
ConcurrentMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
String json = gson.toJson(map);
assertEquals("{\"123\":\"456\"}", json);
}
public void testConcurrentHashMap() throws Exception {
Type typeOfMap = new TypeToken<ConcurrentHashMap() {}.getType();
ConcurrentHashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
String json = gson.toJson(map);
assertEquals("{\"123\":\"456\"}", json);
}
public void testConcurrentNavigableMap() throws Exception {
Type typeOfMap = new TypeToken<ConcurrentNavigableMap() {}.getType();
ConcurrentNavigableMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
String json = gson.toJson(map);
assertEquals("{\"123\":\"456\"}", json);
}
public void testConcurrentSkipListMap() throws Exception {
Type typeOfMap = new TypeToken<ConcurrentSkipListMap() {}.getType();
ConcurrentSkipListMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
String json = gson.toJson(map);
assertEquals("{\"123\":\"456\"}", json);
}
public void testParameterizedMapSubclassSerialization() {
MyParameterizedMap<String, String> map = new MyParameterizedMap(10);
map.put("a", "b");
Type type = new TypeToken<MyParameterizedMap() {}.getType();
String json = gson.toJson(map, type);
assertTrue(json.contains("\"a\":\"b\""));
}
@SuppressWarnings({ "unused", "serial" })
private static class MyParameterizedMap<K, V> extends LinkedHashMap {
final int foo;
MyParameterizedMap(int foo) {
this.foo = foo;
}
}
public void testMapSubclassSerialization() {
MyMap map = new MyMap();
map.put("a", "b");
String json = gson.toJson(map, MyMap.class);
assertTrue(json.contains("\"a\":\"b\""));
}
public void testMapStandardSubclassDeserialization() {
String json = "{a:'1',b:'2'}";
Type type = new TypeToken<LinkedHashMap() {}.getType();
LinkedHashMap<String, Integer> map = gson.fromJson(json, type);
assertEquals("1", map.get("a"));
assertEquals("2", map.get("b"));
}
public void testMapSubclassDeserialization() {
Gson gson = new GsonBuilder().registerTypeAdapter(MyMap.class, new InstanceCreator<MyMap>() {
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"));
}
public void testCustomSerializerForSpecificMapType() {
Type type = $Gson$Types.newParameterizedTypeWithOwner(
null, Map.class, String.class, Long.class);
Gson gson = new GsonBuilder()
.registerTypeAdapter(type, new JsonSerializer<Map() {
public JsonElement serialize(Map<String, Long> src, Type typeOfSrc,
JsonSerializationContext context) {
JsonArray array = new JsonArray();
for (long value : src.values()) {
array.add(new JsonPrimitive(value));
}
return array;
}
}).create();
Map<String, Long> src = new LinkedHashMap();
src.put("one", 1L);
src.put("two", 2L);
src.put("three", 3L);
assertEquals("[1,2,3]", gson.toJson(src, type));
}
/**
* 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();
}
/**
* 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"));
}
public void testMapSerializationWithWildcardValues() {
Map<String, ? extends Collection extends Integer>> map =
new LinkedHashMap<String, Collection();
map.put("test", null);
Type typeOfMap =
new TypeToken<Map>() {}.getType();
String json = gson.toJson(map, typeOfMap);
assertEquals("{}", json);
}
public void testMapDeserializationWithWildcardValues() {
Type typeOfMap = new TypeToken<Map() {}.getType();
Map<String, ? extends Long> map = gson.fromJson("{\"test\":123}", typeOfMap);
assertEquals(1, map.size());
assertEquals(new Long(123L), map.get("test"));
}
private static class MyMap extends LinkedHashMap<String, String> {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
int foo = 10;
}
/**
* From bug report http://code.google.com/p/google-gson/issues/detail?id=95
*/
public void testMapOfMapSerialization() {
Map<String, Map map = new HashMap>();
Map<String, String> nestedMap = new HashMap();
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>(){}.getType();
Map<String, Map map = gson.fromJson(json, type);
Map<String, String> nested = map.get("nestedMap");
assertEquals("1", nested.get("1"));
assertEquals("2", nested.get("2"));
}
/**
* From bug report http://code.google.com/p/google-gson/issues/detail?id=178
*/
public void testMapWithQuotes() {
Map<String, String> map = new HashMap();
map.put("a\"b", "c\"d");
String json = gson.toJson(map);
assertEquals("{\"a\\\"b\":\"c\\\"d\"}", json);
}
/**
* From issue 227.
*/
public void testWriteMapsWithEmptyStringKey() {
Map<String, Boolean> map = new HashMap();
map.put("", true);
assertEquals("{\"\":true}", gson.toJson(map));
}
public void testReadMapsWithEmptyStringKey() {
Map<String, Boolean> map = gson.fromJson("{\"\":true}", new TypeToken
Other Java examples (source code examples)
Here is a short list of links related to this Java MapTest.java source code file:
|