2011-09-09 05:31:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
2011-09-09 06:20:25 +02:00
|
|
|
import java.util.Collections;
|
2011-09-09 05:31:16 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
public final class ObjectTypeAdapterTest extends TestCase {
|
2011-11-20 16:23:08 +01:00
|
|
|
private final Gson gson = new GsonBuilder().create();
|
2011-09-09 05:31:16 +02:00
|
|
|
private final TypeAdapter<Object> adapter = gson.getAdapter(Object.class);
|
|
|
|
|
|
|
|
public void testDeserialize() throws Exception {
|
2011-10-20 23:32:46 +02:00
|
|
|
Map<?, ?> map = (Map<?, ?>) adapter.fromJson("{a: 5, b: [1, 2, null], c: {x: y}}");
|
2011-09-09 05:31:16 +02:00
|
|
|
assertEquals(5.0, map.get("a"));
|
|
|
|
assertEquals(Arrays.asList(1.0, 2.0, null), map.get("b"));
|
2011-09-09 06:20:25 +02:00
|
|
|
assertEquals(Collections.singletonMap("x", "y"), map.get("c"));
|
|
|
|
assertEquals(3, map.size());
|
2011-09-09 05:31:16 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 08:03:16 +02:00
|
|
|
@SuppressWarnings("unused")
|
2011-09-09 05:31:16 +02:00
|
|
|
public void testSerialize() throws Exception {
|
2011-11-20 16:23:08 +01:00
|
|
|
Object object = new RuntimeType();
|
2011-09-09 05:31:16 +02:00
|
|
|
assertEquals("{'a':5,'b':[1,2,null]}", adapter.toJson(object).replace("\"", "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSerializeObject() throws Exception {
|
|
|
|
assertEquals("{}", adapter.toJson(new Object()));
|
|
|
|
}
|
2011-11-20 16:23:08 +01:00
|
|
|
|
|
|
|
private class RuntimeType {
|
|
|
|
Object a = 5;
|
|
|
|
Object b = Arrays.asList(1, 2, null);
|
|
|
|
}
|
2011-09-09 05:31:16 +02:00
|
|
|
}
|