1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

clean up a few more todos

This commit is contained in:
stleary 2015-12-30 13:50:51 -06:00
parent 4df6984233
commit 871a3e46d7
2 changed files with 43 additions and 42 deletions

View file

@ -11,8 +11,7 @@ import com.jayway.jsonpath.*;
/** /**
* Tests for JSON-Java JSONStringer. * Tests for JSON-Java JSONStringer and JSONWriter.
* TODO: Could use a lot more testing. For example, cascade-style productions.
*/ */
public class JSONStringerTest { public class JSONStringerTest {
@ -234,43 +233,44 @@ public class JSONStringerTest {
} }
/** /**
* Build a nested JSON doc using JSONString API calls, * Build a nested JSON doc using JSONString API calls, then convert to
* then convert to JSONObject * JSONObject. Will create a long cascade of output by reusing the
* returned values..
*/ */
@Test @Test
public void complexObjectString() { public void complexObjectString() {
JSONStringer jsonStringer = new JSONStringer(); JSONStringer jsonStringer = new JSONStringer();
jsonStringer.object(); jsonStringer.object().
jsonStringer.key("trueValue").value(true); key("trueValue").value(true).
jsonStringer.key("falseValue").value(false); key("falseValue").value(false).
jsonStringer.key("nullValue").value(null); key("nullValue").value(null).
jsonStringer.key("stringValue").value("hello world!"); key("stringValue").value("hello world!").
jsonStringer.key("object2").object(); key("object2").object().
jsonStringer.key("k1").value("v1"); key("k1").value("v1").
jsonStringer.key("k2").value("v2"); key("k2").value("v2").
jsonStringer.key("k3").value("v3"); key("k3").value("v3").
jsonStringer.key("array1").array(); key("array1").array().
jsonStringer.value(1); value(1).
jsonStringer.value(2); value(2).
jsonStringer.object(); object().
jsonStringer.key("k4").value("v4"); key("k4").value("v4").
jsonStringer.key("k5").value("v5"); key("k5").value("v5").
jsonStringer.key("k6").value("v6"); key("k6").value("v6").
jsonStringer.key("array2").array(); key("array2").array().
jsonStringer.value(5); value(5).
jsonStringer.value(6); value(6).
jsonStringer.value(7); value(7).
jsonStringer.value(8); value(8).
jsonStringer.endArray(); endArray().
jsonStringer.endObject(); endObject().
jsonStringer.value(3); value(3).
jsonStringer.value(4); value(4).
jsonStringer.endArray(); endArray().
jsonStringer.endObject(); endObject().
jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!"); key("complexStringValue").value("h\be\tllo w\u1234orld!").
jsonStringer.key("intValue").value(42); key("intValue").value(42).
jsonStringer.key("doubleValue").value(-23.45e67); key("doubleValue").value(-23.45e67).
jsonStringer.endObject(); endObject();
String str = jsonStringer.toString(); String str = jsonStringer.toString();
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);

View file

@ -59,24 +59,26 @@ public class Util {
private static void compareActualVsExpectedObjects(Object value, private static void compareActualVsExpectedObjects(Object value,
Object expectedValue) { Object expectedValue) {
if (value instanceof JSONObject && expectedValue instanceof JSONObject) { if (value instanceof JSONObject && expectedValue instanceof JSONObject) {
// Compare JSONObjects
JSONObject jsonObject = (JSONObject)value; JSONObject jsonObject = (JSONObject)value;
JSONObject expectedJsonObject = (JSONObject)expectedValue; JSONObject expectedJsonObject = (JSONObject)expectedValue;
compareActualVsExpectedJsonObjects( compareActualVsExpectedJsonObjects(
jsonObject, expectedJsonObject); jsonObject, expectedJsonObject);
} else if (value instanceof JSONArray && expectedValue instanceof JSONArray) { } else if (value instanceof JSONArray && expectedValue instanceof JSONArray) {
// Compare JSONArrays
JSONArray jsonArray = (JSONArray)value; JSONArray jsonArray = (JSONArray)value;
JSONArray expectedJsonArray = (JSONArray)expectedValue; JSONArray expectedJsonArray = (JSONArray)expectedValue;
compareActualVsExpectedJsonArrays( compareActualVsExpectedJsonArrays(
jsonArray, expectedJsonArray); jsonArray, expectedJsonArray);
} else { } else {
/** /**
* Certain helper classes (e.g. XML) may create Long instead of * Compare all other types using toString(). First, the types must
* Integer for small int values. As long as both are Numbers, * also be equal, unless both are Number type. Certain helper
* just compare the toString() values. * classes (e.g. XML) may create Long instead of Integer for small
* TODO: this may not work in the case where the underlying types * int values.
* do not have the same precision.
*/ */
if (!(value instanceof Number && expectedValue instanceof Number)) { if (!(value instanceof Number && expectedValue instanceof Number)) {
// Non-Number and non-matching types
assertTrue("object types should be equal for actual: "+ assertTrue("object types should be equal for actual: "+
value.toString()+" ("+ value.toString()+" ("+
value.getClass().toString()+") expected: "+ value.getClass().toString()+") expected: "+
@ -86,8 +88,7 @@ public class Util {
expectedValue.getClass().toString())); expectedValue.getClass().toString()));
} }
/** /**
* When in doubt, compare by string * Same types or both Numbers, compare by toString()
* TODO: should not this be an else to the previous condition?
*/ */
assertTrue("string values should be equal for actual: "+ assertTrue("string values should be equal for actual: "+
value.toString()+" expected: "+expectedValue.toString(), value.toString()+" expected: "+expectedValue.toString(),