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

View File

@ -23,18 +23,18 @@ import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
/** /**
* A class representing an object type in Json. An object consists of name-value pairs where names * 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 * 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. * tree of JsonElements. The member elements of this object are maintained in order they were added.
* *
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch * @author Joel Leitch
*/ */
public final class JsonObject extends JsonElement { public final class JsonObject extends JsonElement {
// We are using a linked hash map because it is important to preserve // 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 // 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 // that the fields of an object are inserted in the order they were
// defined in the class. // defined in the class.
private final Map<String, JsonElement> members; private final Map<String, JsonElement> members;
/** /**
@ -43,22 +43,28 @@ public final class JsonObject extends JsonElement {
public JsonObject() { public JsonObject() {
members = new LinkedHashMap<String, JsonElement>(); 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 * 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 * 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 property name of the member.
* @param value the member object. * @param value the member object.
*/ */
public void add(String property, JsonElement value) { 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); members.put(property, value);
} }
/** /**
* Removes the {@code property} from this {@link JsonObject}. * Removes the {@code property} from this {@link JsonObject}.
* *
* @param property name of the member that should be removed. * @param property name of the member that should be removed.
* @return the {@link JsonElement} object that is being removed. * @return the {@link JsonElement} object that is being removed.
* @since 1.3 * @since 1.3
@ -66,44 +72,44 @@ public final class JsonObject extends JsonElement {
public JsonElement remove(String property) { public JsonElement remove(String property) {
return members.remove(property); return members.remove(property);
} }
/** /**
* Convenience method to add a primitive member. The specified value is converted to a * Convenience method to add a primitive member. The specified value is converted to a
* JsonPrimitive of String. * JsonPrimitive of String.
* *
* @param property name of the member. * @param property name of the member.
* @param value the string value associated with the member. * @param value the string value associated with the member.
*/ */
public void addProperty(String property, String value) { public void addProperty(String property, String value) {
add(property, createJsonElement(value)); add(property, createJsonElement(value));
} }
/** /**
* Convenience method to add a primitive member. The specified value is converted to a * Convenience method to add a primitive member. The specified value is converted to a
* JsonPrimitive of Number. * JsonPrimitive of Number.
* *
* @param property name of the member. * @param property name of the member.
* @param value the number value associated with the member. * @param value the number value associated with the member.
*/ */
public void addProperty(String property, Number value) { public void addProperty(String property, Number value) {
add(property, createJsonElement(value)); add(property, createJsonElement(value));
} }
/** /**
* Convenience method to add a boolean member. The specified value is converted to a * Convenience method to add a boolean member. The specified value is converted to a
* JsonPrimitive of Boolean. * JsonPrimitive of Boolean.
* *
* @param property name of the member. * @param property name of the member.
* @param value the number value associated with the member. * @param value the number value associated with the member.
*/ */
public void addProperty(String property, Boolean value) { public void addProperty(String property, Boolean value) {
add(property, createJsonElement(value)); add(property, createJsonElement(value));
} }
/** /**
* Convenience method to add a char member. The specified value is converted to a * Convenience method to add a char member. The specified value is converted to a
* JsonPrimitive of Character. * JsonPrimitive of Character.
* *
* @param property name of the member. * @param property name of the member.
* @param value the number value associated with 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 * Returns a set of members of this object. The set is ordered, and the order is in which the
* elements were added. * elements were added.
* *
* @return a set of members of this object. * @return a set of members of this object.
*/ */
public Set<Entry<String, JsonElement>> entrySet() { public Set<Entry<String, JsonElement>> entrySet() {
return members.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. * @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) { public boolean has(String memberName) {
return members.containsKey(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. * @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) { public JsonElement get(String memberName) {
if (members.containsKey(memberName)) { if (members.containsKey(memberName)) {
@ -159,21 +165,21 @@ public final class JsonObject extends JsonElement {
return null; return null;
} }
} }
/** /**
* Convenience method to get the specified member as a JsonPrimitive element. * Convenience method to get the specified member as a JsonPrimitive element.
* *
* @param memberName name of the member being requested. * @param memberName name of the member being requested.
* @return the JsonPrimitive corresponding to the specified member. * @return the JsonPrimitive corresponding to the specified member.
*/ */
public JsonPrimitive getAsJsonPrimitive(String memberName) { public JsonPrimitive getAsJsonPrimitive(String memberName) {
return (JsonPrimitive) members.get(memberName); return (JsonPrimitive) members.get(memberName);
} }
/** /**
* Convenience method to get the specified member as a JsonArray. * 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. * @return the JsonArray corresponding to the specified member.
*/ */
public JsonArray getAsJsonArray(String memberName) { 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. * 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. * @return the JsonObject corresponding to the specified member.
*/ */
public JsonObject getAsJsonObject(String memberName) { 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. * @param bool the value to create the primitive with.
*/ */
public JsonPrimitive(Boolean bool) { 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. * @param number the value to create the primitive with.
*/ */
public JsonPrimitive(Number number) { 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. * @param string the value to create the primitive with.
*/ */
public JsonPrimitive(String string) { 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. * @param c the value to create the primitive with.
*/ */
public JsonPrimitive(Character c) { 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()); return Integer.parseInt(getAsString());
} }
} }
@Override @Override
public byte getAsByte() { public byte getAsByte() {
if (isNumber()) { if (isNumber()) {
@ -292,7 +292,7 @@ public final class JsonPrimitive extends JsonElement {
return Byte.parseByte(getAsString()); return Byte.parseByte(getAsString());
} }
} }
@Override @Override
public char getAsCharacter() { public char getAsCharacter() {
return getAsString().charAt(0); return getAsString().charAt(0);
@ -327,7 +327,7 @@ public final class JsonPrimitive extends JsonElement {
sb.append(value.toString()); sb.append(value.toString());
} }
} }
private static boolean isPrimitiveOrString(Object target) { private static boolean isPrimitiveOrString(Object target) {
if (target instanceof String) { if (target instanceof String) {
return true; return true;
@ -364,7 +364,7 @@ public final class JsonPrimitive extends JsonElement {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null || getClass() != obj.getClass()) { if (obj == null || getClass() != obj.getClass()) {
return false; return false;
} }
JsonPrimitive other = (JsonPrimitive)obj; JsonPrimitive other = (JsonPrimitive)obj;
@ -378,23 +378,23 @@ public final class JsonPrimitive extends JsonElement {
return getAsNumber().doubleValue() == other.getAsNumber().doubleValue(); return getAsNumber().doubleValue() == other.getAsNumber().doubleValue();
} }
return value.equals(other.value); return value.equals(other.value);
} }
/** /**
* Returns true if the specified number is an integral type * Returns true if the specified number is an integral type
* (Long, Integer, Short, Byte, BigInteger) * (Long, Integer, Short, Byte, BigInteger)
*/ */
private static boolean isIntegral(JsonPrimitive primitive) { private static boolean isIntegral(JsonPrimitive primitive) {
if (primitive.value instanceof Number) { if (primitive.value instanceof Number) {
Number number = (Number) primitive.value; 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; || number instanceof Short || number instanceof Byte;
} }
return false; 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) { private static boolean isFloatingPoint(JsonPrimitive primitive) {
if (primitive.value instanceof Number) { if (primitive.value instanceof Number) {