77 lines
2.3 KiB
Java
77 lines
2.3 KiB
Java
|
/*
|
||
|
* 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);
|
||
|
}
|
||
|
}
|