Permalink
Browse files

Replaced get(..., Class) methods with getNumber, getObject, getArray.

This change is intended to make JsonObject and JsonArray more pleasant to program against.
  • Loading branch information...
1 parent dce612c commit c88ec370a78154fa348758b9edbf022cf746d301 @jfuerth committed Feb 4, 2013
@@ -109,18 +109,40 @@
public interface JsonArray extends JsonStructure, List<JsonValue> {
/**
- * Returns the value at the specified position in this JSON array values.
- * This is just a convenience method for {@code (T) get(index)} to get
- * the value.
+ * Returns the JsonObject at the specified position in this JSON array.
+ * This is just a convenience method for {@code (JsonObject) get(index)}.
*
* @param index index of the value to return
- * @param clazz value class
- * @return the value at the specified position in this array values
+ * @return the JsonObject at the specified position in this array
* @throws IndexOutOfBoundsException if the index is out of range
* @throws ClassCastException if the value at the specified position is not
- * assignable to the type T
+ * a JsonObject
*/
- <T extends JsonValue> T get(int index, Class<T> clazz);
+ JsonObject getObject(int index);
+
+ /**
+ * Returns the JsonNumber at the specified position in this JSON array.
+ * This is just a convenience method for {@code (JsonNumber) get(index)}.
+ *
+ * @param index index of the value to return
+ * @return the JsonNumber at the specified position in this array
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * @throws ClassCastException if the value at the specified position is not
+ * a JsonNumber
+ */
+ JsonNumber getNumber(int index);
+
+ /**
+ * Returns the JsonArray at the specified position in this JSON array.
+ * This is just a convenience method for {@code (JsonArray) get(index)}.
+ *
+ * @param index index of the value to return
+ * @return the JsonArray at the specified position in this array
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * @throws ClassCastException if the value at the specified position is not
+ * a JsonArray
+ */
+ JsonArray getArray(int index);
/**
* A convenience method for
@@ -255,13 +255,23 @@ public int size() {
}
@Override
- public <T extends JsonValue> T get(int index, Class<T> clazz) {
- return clazz.cast(valueList.get(index));
+ public JsonObject getObject(int index) {
+ return (JsonObject) get(index);
+ }
+
+ @Override
+ public JsonNumber getNumber(int index) {
+ return (JsonNumber) get(index);
+ }
+
+ @Override
+ public JsonArray getArray(int index) {
+ return (JsonArray) get(index);
}
@Override
public String getString(int index) {
- return get(index, JsonString.class).getValue();
+ return ((JsonString) get(index)).getValue();
}
@Override
@@ -275,7 +285,7 @@ public String getString(int index, String defaultValue) {
@Override
public int getInt(int index) {
- return get(index, JsonNumber.class).intValue();
+ return ((JsonNumber) get(index)).intValue();
}
@Override
@@ -130,18 +130,40 @@
public interface JsonObject extends JsonStructure, Map<String, JsonValue> {
/**
- * Returns the value to which the specified name is mapped.
- * This is just a convenience method for {@code (T) get(name)} to get
- * the value.
+ * Returns the JsonObject to which the specified name is mapped.
+ * This is just a convenience method for {@code (JsonObject) get(name)}.
*
* @param name the name(key) whose associated value is to be returned
- * @param clazz value class
* @return the value to which the specified name is mapped, or
* {@code null} if this object contains no mapping for the name(key)
* @throws ClassCastException if the value for specified name/key mapping
- * is not assignable to the type T
+ * is not a JsonObject.
*/
- <T extends JsonValue> T get(String name, Class<T> clazz);
+ JsonObject getObject(String name);
+
+ /**
+ * Returns the JsonNumber to which the specified name is mapped.
+ * This is just a convenience method for {@code (JsonNumber) get(name)}.
+ *
+ * @param name the name(key) whose associated value is to be returned
+ * @return the value to which the specified name is mapped, or
+ * {@code null} if this object contains no mapping for the name(key)
+ * @throws ClassCastException if the value for specified name/key mapping
+ * is not a JsonNumber.
+ */
+ JsonNumber getNumber(String name);
+
+ /**
+ * Returns the JsonArray to which the specified name is mapped.
+ * This is just a convenience method for {@code (JsonArray) get(name)}.
+ *
+ * @param name the name(key) whose associated value is to be returned
+ * @return the value to which the specified name is mapped, or
+ * {@code null} if this object contains no mapping for the name(key)
+ * @throws ClassCastException if the value for specified name/key mapping
+ * is not a JsonArray.
+ */
+ JsonArray getArray(String name);
/**
* A convenience method for
@@ -307,13 +307,23 @@ public JsonObject build() {
}
@Override
- public <T extends JsonValue> T get(String name, Class<T> clazz) {
- return clazz.cast(valueMap.get(name));
+ public JsonObject getObject(String name) {
+ return ((JsonObject) get(name));
+ }
+
+ @Override
+ public JsonNumber getNumber(String name) {
+ return (JsonNumber) get(name);
+ }
+
+ @Override
+ public JsonArray getArray(String name) {
+ return (JsonArray) get(name);
}
@Override
public String getString(String name) {
- return get(name, JsonString.class).getValue();
+ return ((JsonString) get(name)).getValue();
}
@Override
@@ -327,7 +337,7 @@ public String getString(String name, String defaultValue) {
@Override
public int getInt(String name) {
- return get(name, JsonNumber.class).intValue();
+ return getNumber(name).intValue();
}
@Override
@@ -40,9 +40,13 @@
package org.glassfish.json;
-import junit.framework.TestCase;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
-import javax.json.*;
+import junit.framework.TestCase;
/**
* @author Jitendra Kotamraju
@@ -73,7 +77,7 @@ public void testObject() throws Exception {
public void testNumber() throws Exception {
JsonObject person = buildPerson();
- JsonNumber number = person.get("age", JsonNumber.class);
+ JsonNumber number = person.getNumber("age");
assertEquals(25, number.intValueExact());
assertEquals(25, number.intValue());
assertEquals(JsonNumber.NumberType.INTEGER, number.getNumberType());
@@ -40,14 +40,24 @@
package org.glassfish.json;
-import junit.framework.TestCase;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
-import javax.json.*;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonValue;
import javax.json.stream.JsonGenerationException;
import javax.json.stream.JsonGenerator;
-import java.io.*;
-import java.util.HashMap;
-import java.util.Map;
+
+import junit.framework.TestCase;
/**
* {@link JsonGenerator} tests
@@ -179,7 +189,7 @@ public void testEscapedString1() throws Exception {
JsonReader jr = new JsonReader(new StringReader(sw.toString()));
JsonArray array = jr.readArray();
- String got = array.get(0, JsonString.class).getValue();
+ String got = array.getString(0);
jr.close();
assertEquals(expected, got);
@@ -40,14 +40,20 @@
package org.glassfish.json;
-import junit.framework.TestCase;
-
-import javax.json.*;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonNumber;
+import javax.json.JsonReader;
+import javax.json.JsonValue;
+import javax.json.JsonWriter;
+
+import junit.framework.TestCase;
+
/**
* @author Jitendra Kotamraju
*/
@@ -223,7 +229,7 @@ public void testLeadingZeroes() {
public void testBigIntegerExact() {
try {
JsonArray array = new JsonArrayBuilder().add(12345.12345).build();
- array.get(0, JsonNumber.class).bigIntegerValueExact();
+ array.getNumber(0).bigIntegerValueExact();
fail("Expected Arithmetic exception");
} catch (ArithmeticException expected) {
// no-op
@@ -40,9 +40,12 @@
package org.glassfish.json;
-import junit.framework.TestCase;
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonValue;
-import javax.json.*;
+import junit.framework.TestCase;
/**
* @author Jitendra Kotamraju
@@ -74,30 +77,28 @@ public void testPersonObjectEquals() throws Exception {
static void testPerson(JsonObject person) {
assertEquals(5, person.size());
- assertEquals("John", person.get("firstName", JsonString.class).getValue());
- assertEquals("Smith", person.get("lastName", JsonString.class).getValue());
- assertEquals(25, person.get("age", JsonNumber.class).intValue());
+ assertEquals("John", person.getString("firstName"));
+ assertEquals("Smith", person.getString("lastName"));
+ assertEquals(25, person.getNumber("age").intValue());
assertEquals(25, person.getInt("age"));
- JsonObject address = person.get("address", JsonObject.class);
+ JsonObject address = person.getObject("address");
assertEquals(4, address.size());
- assertEquals("21 2nd Street", address.get("streetAddress", JsonString.class).getValue());
- assertEquals("New York", address.get("city", JsonString.class).getValue());
- assertEquals("NY", address.get("state", JsonString.class).getValue());
- assertEquals("10021", address.get("postalCode", JsonString.class).getValue());
+ assertEquals("21 2nd Street", address.getString("streetAddress"));
+ assertEquals("New York", address.getString("city"));
+ assertEquals("NY", address.getString("state"));
+ assertEquals("10021", address.getString("postalCode"));
- JsonArray phoneNumber = person.get("phoneNumber", JsonArray.class);
+ JsonArray phoneNumber = person.getArray("phoneNumber");
assertEquals(2, phoneNumber.size());
- JsonObject home = phoneNumber.get(0, JsonObject.class);
+ JsonObject home = phoneNumber.getObject(0);
assertEquals(2, home.size());
- assertEquals("home", home.get("type", JsonString.class).getValue());
- assertEquals("212 555-1234", home.get("number", JsonString.class).getValue());
+ assertEquals("home", home.getString("type"));
assertEquals("212 555-1234", home.getString("number"));
- JsonObject fax = phoneNumber.get(1, JsonObject.class);
+ JsonObject fax = phoneNumber.getObject(1);
assertEquals(2, fax.size());
- assertEquals("fax", fax.get("type", JsonString.class).getValue());
- assertEquals("646 555-4567", fax.get("number", JsonString.class).getValue());
+ assertEquals("fax", fax.getString("type"));
assertEquals("646 555-4567", fax.getString("number"));
}
@@ -109,7 +110,7 @@ public void testClassCastException() {
JsonObject obj = new JsonObjectBuilder()
.add("foo", JsonValue.FALSE).build();
try {
- obj.get("foo", JsonNumber.class);
+ obj.getNumber("foo");
fail("Expected ClassCastException for casting JsonValue.FALSE to JsonNumber");
} catch (ClassCastException ce) {
// Expected
@@ -40,13 +40,20 @@
package org.glassfish.json;
-import junit.framework.TestCase;
-
-import javax.json.*;
-import java.io.*;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.json.JsonStructure;
+import javax.json.JsonValue;
+
+import junit.framework.TestCase;
+
/**
* @author Jitendra Kotamraju
*/
@@ -65,7 +72,7 @@ public void testEscapedString() throws Exception {
JsonReader reader = new JsonReader(new StringReader("[\"\\u0000\\u00ff\u00ff\"]"));
JsonArray array = reader.readArray();
reader.close();
- String str = array.get(0, JsonString.class).getValue();
+ String str = array.getString(0);
assertEquals("\u0000\u00ff\u00ff", str);
}

0 comments on commit c88ec37

Please sign in to comment.