2008-11-19 02:22:21 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.google.gson;
|
|
|
|
|
2011-02-15 01:14:20 +01:00
|
|
|
import com.google.gson.common.MoreAsserts;
|
2008-11-19 02:22:21 +01:00
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit test for the {@link JsonObject} class.
|
|
|
|
*
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
|
|
|
public class JsonObjectTest extends TestCase {
|
|
|
|
|
|
|
|
public void testAddingAndRemovingObjectProperties() throws Exception {
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
String propertyName = "property";
|
|
|
|
assertFalse(jsonObj.has(propertyName));
|
|
|
|
assertNull(jsonObj.get(propertyName));
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-11-19 02:22:21 +01:00
|
|
|
JsonPrimitive value = new JsonPrimitive("blah");
|
|
|
|
jsonObj.add(propertyName, value);
|
|
|
|
assertEquals(value, jsonObj.get(propertyName));
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-11-19 02:22:21 +01:00
|
|
|
JsonElement removedElement = jsonObj.remove(propertyName);
|
|
|
|
assertEquals(value, removedElement);
|
|
|
|
assertFalse(jsonObj.has(propertyName));
|
|
|
|
}
|
2009-10-09 23:12:07 +02:00
|
|
|
|
|
|
|
public void testAddingNullPropertyValue() throws Exception {
|
2008-11-19 02:22:21 +01:00
|
|
|
String propertyName = "property";
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
jsonObj.add(propertyName, null);
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-11-19 02:22:21 +01:00
|
|
|
assertTrue(jsonObj.has(propertyName));
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-11-19 02:22:21 +01:00
|
|
|
JsonElement jsonElement = jsonObj.get(propertyName);
|
|
|
|
assertNotNull(jsonElement);
|
|
|
|
assertTrue(jsonElement.isJsonNull());
|
|
|
|
}
|
2009-10-09 23:12:07 +02:00
|
|
|
|
|
|
|
public void testAddingNullOrEmptyPropertyName() throws Exception {
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
try {
|
|
|
|
jsonObj.add(null, JsonNull.createJsonNull());
|
|
|
|
fail("Should not allow null property names.");
|
2011-03-16 08:23:44 +01:00
|
|
|
} catch (NullPointerException expected) { }
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2010-08-28 10:18:13 +02:00
|
|
|
jsonObj.add("", JsonNull.createJsonNull());
|
|
|
|
jsonObj.add(" \t", JsonNull.createJsonNull());
|
2009-10-09 23:12:07 +02:00
|
|
|
}
|
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
public void testAddingBooleanProperties() throws Exception {
|
|
|
|
String propertyName = "property";
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
jsonObj.addProperty(propertyName, true);
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
assertTrue(jsonObj.has(propertyName));
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
JsonElement jsonElement = jsonObj.get(propertyName);
|
|
|
|
assertNotNull(jsonElement);
|
|
|
|
assertTrue(jsonElement.getAsBoolean());
|
|
|
|
}
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
public void testAddingStringProperties() throws Exception {
|
|
|
|
String propertyName = "property";
|
|
|
|
String value = "blah";
|
|
|
|
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
jsonObj.addProperty(propertyName, value);
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
assertTrue(jsonObj.has(propertyName));
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
JsonElement jsonElement = jsonObj.get(propertyName);
|
|
|
|
assertNotNull(jsonElement);
|
|
|
|
assertEquals(value, jsonElement.getAsString());
|
|
|
|
}
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
public void testAddingCharacterProperties() throws Exception {
|
|
|
|
String propertyName = "property";
|
|
|
|
char value = 'a';
|
|
|
|
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
jsonObj.addProperty(propertyName, value);
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
assertTrue(jsonObj.has(propertyName));
|
2009-10-09 23:12:07 +02:00
|
|
|
|
2008-12-01 00:36:29 +01:00
|
|
|
JsonElement jsonElement = jsonObj.get(propertyName);
|
|
|
|
assertNotNull(jsonElement);
|
|
|
|
assertEquals(String.valueOf(value), jsonElement.getAsString());
|
|
|
|
assertEquals(value, jsonElement.getAsCharacter());
|
|
|
|
}
|
2010-08-20 07:45:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* From bug report http://code.google.com/p/google-gson/issues/detail?id=182
|
|
|
|
*/
|
|
|
|
public void testPropertyWithQuotes() {
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
jsonObj.add("a\"b", new JsonPrimitive("c\"d"));
|
|
|
|
String json = new Gson().toJson(jsonObj);
|
|
|
|
assertEquals("{\"a\\\"b\":\"c\\\"d\"}", json);
|
|
|
|
}
|
2010-08-28 07:02:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* From issue 227.
|
|
|
|
*/
|
|
|
|
public void testWritePropertyWithEmptyStringName() {
|
|
|
|
JsonObject jsonObj = new JsonObject();
|
|
|
|
jsonObj.add("", new JsonPrimitive(true));
|
|
|
|
assertEquals("{\"\":true}", new Gson().toJson(jsonObj));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testReadPropertyWithEmptyStringName() {
|
|
|
|
JsonObject jsonObj = new JsonParser().parse("{\"\":true}").getAsJsonObject();
|
|
|
|
assertEquals(true, jsonObj.get("").getAsBoolean());
|
2011-02-15 01:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testEqualsOnEmptyObject() {
|
|
|
|
MoreAsserts.assertEqualsAndHashCode(new JsonObject(), new JsonObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testEqualsNonEmptyObject() {
|
|
|
|
JsonObject a = new JsonObject();
|
|
|
|
JsonObject b = new JsonObject();
|
|
|
|
|
|
|
|
a.add("foo", new JsonObject());
|
|
|
|
assertFalse(a.equals(b));
|
|
|
|
assertFalse(b.equals(a));
|
|
|
|
|
|
|
|
b.add("foo", new JsonObject());
|
|
|
|
MoreAsserts.assertEqualsAndHashCode(a, b);
|
|
|
|
|
|
|
|
a.add("bar", new JsonObject());
|
|
|
|
assertFalse(a.equals(b));
|
|
|
|
assertFalse(b.equals(a));
|
2010-08-28 07:02:33 +02:00
|
|
|
|
2011-02-15 01:14:20 +01:00
|
|
|
b.add("bar", new JsonNull());
|
|
|
|
assertFalse(a.equals(b));
|
|
|
|
assertFalse(b.equals(a));
|
2010-08-28 07:02:33 +02:00
|
|
|
}
|
2008-11-19 02:22:21 +01:00
|
|
|
}
|