From 93b0008486b3186f5a0113062dd566076a1df60a Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Thu, 8 Oct 2009 22:03:08 +0000 Subject: [PATCH] Adding restrictions on the JsonElements to disallow Java "null"s. --- .../main/java/com/google/gson/JsonArray.java | 5 +- .../main/java/com/google/gson/JsonObject.java | 104 +++++++++--------- .../java/com/google/gson/JsonPrimitive.java | 28 ++--- 3 files changed, 73 insertions(+), 64 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonArray.java b/gson/src/main/java/com/google/gson/JsonArray.java index 2cac2992..8aa6075f 100644 --- a/gson/src/main/java/com/google/gson/JsonArray.java +++ b/gson/src/main/java/com/google/gson/JsonArray.java @@ -48,6 +48,9 @@ public final class JsonArray extends JsonElement implements Iterable members; /** @@ -43,22 +43,28 @@ public final class JsonObject extends JsonElement { public JsonObject() { members = new LinkedHashMap(); } - + /** * 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> 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) { diff --git a/gson/src/main/java/com/google/gson/JsonPrimitive.java b/gson/src/main/java/com/google/gson/JsonPrimitive.java index f76dce04..9ce3a2dc 100644 --- a/gson/src/main/java/com/google/gson/JsonPrimitive.java +++ b/gson/src/main/java/com/google/gson/JsonPrimitive.java @@ -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) {