Adding restrictions on the JsonElements to disallow Java "null"s.

This commit is contained in:
Joel Leitch 2009-10-08 22:03:08 +00:00
parent 10c39317a1
commit 93b0008486
3 changed files with 73 additions and 64 deletions

View File

@ -48,6 +48,9 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* @param element the element that needs to be added to the array.
*/
public void add(JsonElement element) {
if (element == null) {
element = JsonNull.createJsonNull();
}
elements.add(element);
}
@ -235,7 +238,7 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
}
throw new IllegalStateException();
}
@Override
public char getAsCharacter() {
if (elements.size() == 1) {

View File

@ -23,18 +23,18 @@ import java.util.Set;
import java.util.Map.Entry;
/**
* A class representing an object type in Json. An object consists of name-value pairs where names
* are strings, and values are any other type of {@link JsonElement}. This allows for a creating a
* tree of JsonElements. The member elements of this object are maintained in order they were added.
*
* A class representing an object type in Json. An object consists of name-value pairs where names
* are strings, and values are any other type of {@link JsonElement}. This allows for a creating a
* tree of JsonElements. The member elements of this object are maintained in order they were added.
*
* @author Inderjeet Singh
* @author Joel Leitch
*/
public final class JsonObject extends JsonElement {
// We are using a linked hash map because it is important to preserve
// the order in which elements are inserted. This is needed to ensure
// that the fields of an object are inserted in the order they were
// defined in the class.
// that the fields of an object are inserted in the order they were
// defined in the class.
private final Map<String, JsonElement> members;
/**
@ -43,22 +43,28 @@ public final class JsonObject extends JsonElement {
public JsonObject() {
members = new LinkedHashMap<String, JsonElement>();
}
/**
* Adds a member, which is a name-value pair, to self. The name must be a String, but the value
* can be an arbitrary JsonElement, thereby allowing you to build a full tree of JsonElements
* rooted at this node.
*
* rooted at this node.
*
* @param property name of the member.
* @param value the member object.
*/
public void add(String property, JsonElement value) {
Preconditions.checkArgument(property != null && !"".equals(property.trim()));
Preconditions.checkNotNull(property);
if (value == null) {
value = JsonNull.createJsonNull();
}
members.put(property, value);
}
/**
* Removes the {@code property} from this {@link JsonObject}.
*
*
* @param property name of the member that should be removed.
* @return the {@link JsonElement} object that is being removed.
* @since 1.3
@ -66,44 +72,44 @@ public final class JsonObject extends JsonElement {
public JsonElement remove(String property) {
return members.remove(property);
}
/**
* Convenience method to add a primitive member. The specified value is converted to a
* JsonPrimitive of String.
*
* Convenience method to add a primitive member. The specified value is converted to a
* JsonPrimitive of String.
*
* @param property name of the member.
* @param value the string value associated with the member.
*/
public void addProperty(String property, String value) {
add(property, createJsonElement(value));
}
/**
* Convenience method to add a primitive member. The specified value is converted to a
* JsonPrimitive of Number.
*
* Convenience method to add a primitive member. The specified value is converted to a
* JsonPrimitive of Number.
*
* @param property name of the member.
* @param value the number value associated with the member.
*/
public void addProperty(String property, Number value) {
add(property, createJsonElement(value));
}
/**
* Convenience method to add a boolean member. The specified value is converted to a
* JsonPrimitive of Boolean.
*
* 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.
*
* 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.
*/
@ -126,30 +132,30 @@ public final class JsonObject extends JsonElement {
}
/**
* Returns a set of members of this object. The set is ordered, and the order is in which the
* elements were added.
*
* @return a set of members of this object.
* Returns a set of members of this object. The set is ordered, and the order is in which the
* elements were added.
*
* @return a set of members of this object.
*/
public Set<Entry<String, JsonElement>> entrySet() {
return members.entrySet();
}
/**
* Convenience method to check if a member with the specified name is present in this object.
*
* Convenience method to check if a member with the specified name is present in this object.
*
* @param memberName name of the member that is being checked for presence.
* @return true if there is a member with the specified name, false otherwise.
* @return true if there is a member with the specified name, false otherwise.
*/
public boolean has(String memberName) {
return members.containsKey(memberName);
}
/**
* Returns the member with the specified name.
*
* Returns the member with the specified name.
*
* @param memberName name of the member that is being requested.
* @return the member matching the name. Null if no such member exists.
* @return the member matching the name. Null if no such member exists.
*/
public JsonElement get(String memberName) {
if (members.containsKey(memberName)) {
@ -159,21 +165,21 @@ public final class JsonObject extends JsonElement {
return null;
}
}
/**
* Convenience method to get the specified member as a JsonPrimitive element.
*
* @param memberName name of the member being requested.
* @return the JsonPrimitive corresponding to the specified member.
* Convenience method to get the specified member as a JsonPrimitive element.
*
* @param memberName name of the member being requested.
* @return the JsonPrimitive corresponding to the specified member.
*/
public JsonPrimitive getAsJsonPrimitive(String memberName) {
return (JsonPrimitive) members.get(memberName);
}
/**
* Convenience method to get the specified member as a JsonArray.
*
* @param memberName name of the member being requested.
*
* @param memberName name of the member being requested.
* @return the JsonArray corresponding to the specified member.
*/
public JsonArray getAsJsonArray(String memberName) {
@ -182,8 +188,8 @@ public final class JsonObject extends JsonElement {
/**
* Convenience method to get the specified member as a JsonObject.
*
* @param memberName name of the member being requested.
*
* @param memberName name of the member being requested.
* @return the JsonObject corresponding to the specified member.
*/
public JsonObject getAsJsonObject(String memberName) {

View File

@ -44,7 +44,7 @@ public final class JsonPrimitive extends JsonElement {
* @param bool the value to create the primitive with.
*/
public JsonPrimitive(Boolean bool) {
this.value = bool;
setValue(bool);
}
/**
@ -53,7 +53,7 @@ public final class JsonPrimitive extends JsonElement {
* @param number the value to create the primitive with.
*/
public JsonPrimitive(Number number) {
this.value = number;
setValue(number);
}
/**
@ -62,7 +62,7 @@ public final class JsonPrimitive extends JsonElement {
* @param string the value to create the primitive with.
*/
public JsonPrimitive(String string) {
this.value = string;
setValue(string);
}
/**
@ -72,7 +72,7 @@ public final class JsonPrimitive extends JsonElement {
* @param c the value to create the primitive with.
*/
public JsonPrimitive(Character c) {
this.value = String.valueOf(c);
setValue(c);
}
/**
@ -283,7 +283,7 @@ public final class JsonPrimitive extends JsonElement {
return Integer.parseInt(getAsString());
}
}
@Override
public byte getAsByte() {
if (isNumber()) {
@ -292,7 +292,7 @@ public final class JsonPrimitive extends JsonElement {
return Byte.parseByte(getAsString());
}
}
@Override
public char getAsCharacter() {
return getAsString().charAt(0);
@ -327,7 +327,7 @@ public final class JsonPrimitive extends JsonElement {
sb.append(value.toString());
}
}
private static boolean isPrimitiveOrString(Object target) {
if (target instanceof String) {
return true;
@ -364,7 +364,7 @@ public final class JsonPrimitive extends JsonElement {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
JsonPrimitive other = (JsonPrimitive)obj;
@ -378,23 +378,23 @@ public final class JsonPrimitive extends JsonElement {
return getAsNumber().doubleValue() == other.getAsNumber().doubleValue();
}
return value.equals(other.value);
}
}
/**
* Returns true if the specified number is an integral type
* (Long, Integer, Short, Byte, BigInteger)
* Returns true if the specified number is an integral type
* (Long, Integer, Short, Byte, BigInteger)
*/
private static boolean isIntegral(JsonPrimitive primitive) {
if (primitive.value instanceof Number) {
Number number = (Number) primitive.value;
return number instanceof BigInteger || number instanceof Long || number instanceof Integer
return number instanceof BigInteger || number instanceof Long || number instanceof Integer
|| number instanceof Short || number instanceof Byte;
}
return false;
}
/**
* Returns true if the specified number is a floating point type (BigDecimal, double, float)
* Returns true if the specified number is a floating point type (BigDecimal, double, float)
*/
private static boolean isFloatingPoint(JsonPrimitive primitive) {
if (primitive.value instanceof Number) {