Adding new convenience method to JsonObject for adding Booleans and Characters.
This commit is contained in:
parent
b90228dcc0
commit
5ae7e1f803
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user