diff --git a/gson/src/main/java/com/google/gson/JsonObject.java b/gson/src/main/java/com/google/gson/JsonObject.java index 3c9a55b0..7e5bc7d6 100644 --- a/gson/src/main/java/com/google/gson/JsonObject.java +++ b/gson/src/main/java/com/google/gson/JsonObject.java @@ -72,7 +72,7 @@ public final class JsonObject extends JsonElement { * @param value the string value associated with the member. */ public void addProperty(String property, String value) { - members.put(property, new JsonPrimitive(value)); + add(property, createJsonElement(value)); } /** @@ -83,7 +83,43 @@ public final class JsonObject extends JsonElement { * @param value the number value associated with the member. */ public void addProperty(String property, Number value) { - members.put(property, new JsonPrimitive(value)); + add(property, createJsonElement(value)); + } + + /** + * Convenience method to add a boolean member. The specified value is converted to a + * JsonPrimitive of Boolean. + * + * @param property name of the member. + * @param value the number value associated with the member. + */ + public void addProperty(String property, Boolean value) { + add(property, createJsonElement(value)); + } + + /** + * Convenience method to add a char member. The specified value is converted to a + * JsonPrimitive of Character. + * + * @param property name of the member. + * @param value the number value associated with the member. + */ + public void addProperty(String property, Character value) { + add(property, createJsonElement(value)); + } + + /** + * Creates the proper {@link JsonElement} object from the given {@code value} object. + * + * @param value the object to generate the {@link JsonElement} for + * @return a {@link JsonPrimitive} if the {@code value} is not null, otherwise a {@link JsonNull} + */ + private JsonElement createJsonElement(Object value) { + if (value == null) { + return JsonNull.createJsonNull(); + } else { + return new JsonPrimitive(value); + } } /** diff --git a/gson/src/main/java/com/google/gson/JsonPrimitive.java b/gson/src/main/java/com/google/gson/JsonPrimitive.java index b12224bb..e8b73708 100644 --- a/gson/src/main/java/com/google/gson/JsonPrimitive.java +++ b/gson/src/main/java/com/google/gson/JsonPrimitive.java @@ -71,16 +71,6 @@ public final class JsonPrimitive extends JsonElement { this.value = String.valueOf(c); } - /** - * Create a primitive containing a character. The character is turned into a one character String - * since Json only supports String. - * - * @param c the value to create the primitive with. - */ - public JsonPrimitive(char c) { - this.value = String.valueOf(c); - } - /** * Create a primitive using the specified Object. It must be an instance of {@link Number}, a * Java primitive type, or a String. @@ -95,7 +85,7 @@ public final class JsonPrimitive extends JsonElement { if (primitive instanceof Character) { // convert characters to strings since in JSON, characters are represented as a single // character string - char c = ((Character)primitive).charValue(); + char c = ((Character) primitive).charValue(); this.value = String.valueOf(c); } else { Preconditions.checkArgument(primitive instanceof Number diff --git a/gson/src/test/java/com/google/gson/JsonObjectTest.java b/gson/src/test/java/com/google/gson/JsonObjectTest.java index 5744653b..a201ded9 100644 --- a/gson/src/test/java/com/google/gson/JsonObjectTest.java +++ b/gson/src/test/java/com/google/gson/JsonObjectTest.java @@ -51,4 +51,45 @@ public class JsonObjectTest extends TestCase { assertNotNull(jsonElement); assertTrue(jsonElement.isJsonNull()); } + + public void testAddingBooleanProperties() throws Exception { + String propertyName = "property"; + JsonObject jsonObj = new JsonObject(); + jsonObj.addProperty(propertyName, true); + + assertTrue(jsonObj.has(propertyName)); + + JsonElement jsonElement = jsonObj.get(propertyName); + assertNotNull(jsonElement); + assertTrue(jsonElement.getAsBoolean()); + } + + public void testAddingStringProperties() throws Exception { + String propertyName = "property"; + String value = "blah"; + + JsonObject jsonObj = new JsonObject(); + jsonObj.addProperty(propertyName, value); + + assertTrue(jsonObj.has(propertyName)); + + JsonElement jsonElement = jsonObj.get(propertyName); + assertNotNull(jsonElement); + assertEquals(value, jsonElement.getAsString()); + } + + public void testAddingCharacterProperties() throws Exception { + String propertyName = "property"; + char value = 'a'; + + JsonObject jsonObj = new JsonObject(); + jsonObj.addProperty(propertyName, value); + + assertTrue(jsonObj.has(propertyName)); + + JsonElement jsonElement = jsonObj.get(propertyName); + assertNotNull(jsonElement); + assertEquals(String.valueOf(value), jsonElement.getAsString()); + assertEquals(value, jsonElement.getAsCharacter()); + } }