mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #31 from stleary/json-path-for-validation
Replace util compare method with JsonPath
This commit is contained in:
commit
cfec741090
5 changed files with 571 additions and 541 deletions
|
@ -2,9 +2,13 @@ package org.json.junit;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import org.json.*;
|
import org.json.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
|
* HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -60,7 +64,6 @@ public class CookieListTest {
|
||||||
@Test
|
@Test
|
||||||
public void emptyStringCookieList() {
|
public void emptyStringCookieList() {
|
||||||
String cookieStr = "";
|
String cookieStr = "";
|
||||||
String expectedCookieStr = "";
|
|
||||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||||
assertTrue(jsonObject.length() == 0);
|
assertTrue(jsonObject.length() == 0);
|
||||||
}
|
}
|
||||||
|
@ -71,10 +74,11 @@ public class CookieListTest {
|
||||||
@Test
|
@Test
|
||||||
public void simpleCookieList() {
|
public void simpleCookieList() {
|
||||||
String cookieStr = "SID=31d4d96e407aad42";
|
String cookieStr = "SID=31d4d96e407aad42";
|
||||||
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
|
|
||||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
// validate JSON content
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
|
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,10 +87,11 @@ public class CookieListTest {
|
||||||
@Test
|
@Test
|
||||||
public void simpleCookieListWithDelimiter() {
|
public void simpleCookieListWithDelimiter() {
|
||||||
String cookieStr = "SID=31d4d96e407aad42;";
|
String cookieStr = "SID=31d4d96e407aad42;";
|
||||||
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
|
|
||||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
// validate JSON content
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
|
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -102,18 +107,16 @@ public class CookieListTest {
|
||||||
" name4=myCookieValue4; "+
|
" name4=myCookieValue4; "+
|
||||||
"name5=myCookieValue5;"+
|
"name5=myCookieValue5;"+
|
||||||
" name6=myCookieValue6;";
|
" name6=myCookieValue6;";
|
||||||
String expectedCookieStr =
|
|
||||||
"{"+
|
|
||||||
"\"name1\":\"myCookieValue1\","+
|
|
||||||
"\"name2\":\"myCookieValue2\","+
|
|
||||||
"\"name3\":\"myCookieValue3\","+
|
|
||||||
"\"name4\":\"myCookieValue4\","+
|
|
||||||
"\"name5\":\"myCookieValue5\","+
|
|
||||||
"\"name6\":\"myCookieValue6\""+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
// validate JSON content
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
|
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
|
||||||
|
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
|
||||||
|
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
|
||||||
|
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
|
||||||
|
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
|
||||||
|
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -139,21 +142,16 @@ public class CookieListTest {
|
||||||
" name4=myCookieValue4; "+
|
" name4=myCookieValue4; "+
|
||||||
"name5=myCookieValue5;"+
|
"name5=myCookieValue5;"+
|
||||||
" name6=myCookieValue6;";
|
" name6=myCookieValue6;";
|
||||||
String expectedCookieStr =
|
|
||||||
"{"+
|
|
||||||
"\"name1\":\"myCookieValue1\","+
|
|
||||||
"\"name2\":\"myCookieValue2\","+
|
|
||||||
"\"name3\":\"myCookieValue3\","+
|
|
||||||
"\"name4\":\"myCookieValue4\","+
|
|
||||||
"\"name5\":\"myCookieValue5\","+
|
|
||||||
"\"name6\":\"myCookieValue6\""+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
// validate JSON content
|
||||||
String cookieToStr = CookieList.toString(jsonObject);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
|
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
|
||||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
|
||||||
|
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
|
||||||
|
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
|
||||||
|
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
|
||||||
|
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,22 +167,15 @@ public class CookieListTest {
|
||||||
" name4=my%25CookieValue4; "+
|
" name4=my%25CookieValue4; "+
|
||||||
"name5=myCookieValue5;"+
|
"name5=myCookieValue5;"+
|
||||||
" name6=myCookieValue6;";
|
" name6=myCookieValue6;";
|
||||||
String expectedCookieStr =
|
|
||||||
"{"+
|
|
||||||
"\"name1\":\"myCookieValue1\","+
|
|
||||||
"\"name2\":\"my Cookie Value 2\","+
|
|
||||||
"\"name3\":\"my+Cookie&Value;3=\","+
|
|
||||||
"\"name4\":\"my%CookieValue4\","+
|
|
||||||
"\"name5\":\"myCookieValue5\","+
|
|
||||||
"\"name6\":\"myCookieValue6\""+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
// validate JSON content
|
||||||
String cookieToStr = CookieList.toString(jsonObject);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
|
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
|
||||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2")));
|
||||||
|
assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3")));
|
||||||
|
assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(JsonPath.read(doc, "$.name4")));
|
||||||
|
assertTrue("expected my%CookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
|
||||||
|
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
165
EnumTest.java
165
EnumTest.java
|
@ -2,9 +2,13 @@ package org.json.junit;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import org.json.*;
|
import org.json.*;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enums are not explicitly supported in JSON-Java. But because enums act like
|
* Enums are not explicitly supported in JSON-Java. But because enums act like
|
||||||
* classes, all required behavior is already be present in some form.
|
* classes, all required behavior is already be present in some form.
|
||||||
|
@ -25,23 +29,32 @@ public class EnumTest {
|
||||||
assertTrue("simple enum has no getters", jsonObject.length() == 0);
|
assertTrue("simple enum has no getters", jsonObject.length() == 0);
|
||||||
|
|
||||||
// enum with a getters should create a non-empty object
|
// enum with a getters should create a non-empty object
|
||||||
String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
|
|
||||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||||
jsonObject = new JSONObject(myEnumField);
|
jsonObject = new JSONObject(myEnumField);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON content
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse(jsonObject.toString());
|
||||||
|
assertTrue("expecting 2 items in top level object", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expecting val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
|
||||||
|
assertTrue("expecting 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class which contains enum instances. Each enum should be stored
|
* class which contains enum instances. Each enum should be stored
|
||||||
* in its own JSONObject
|
* in its own JSONObject
|
||||||
*/
|
*/
|
||||||
expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
|
|
||||||
MyEnumClass myEnumClass = new MyEnumClass();
|
MyEnumClass myEnumClass = new MyEnumClass();
|
||||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||||
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
||||||
jsonObject = new JSONObject(myEnumClass);
|
jsonObject = new JSONObject(myEnumClass);
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON content
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expecting 2 items in top level object", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expecting 2 items in myEnumField object", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
||||||
|
assertTrue("expecting 0 items in myEnum object", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
||||||
|
assertTrue("expecting 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
|
||||||
|
assertTrue("expecting val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,28 +64,30 @@ public class EnumTest {
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectFromEnumWithNames() {
|
public void jsonObjectFromEnumWithNames() {
|
||||||
String [] names;
|
String [] names;
|
||||||
String expectedStr;
|
|
||||||
JSONObject jsonObject;
|
JSONObject jsonObject;
|
||||||
JSONObject finalJsonObject;
|
|
||||||
JSONObject expectedJsonObject;
|
|
||||||
|
|
||||||
expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
|
|
||||||
MyEnum myEnum = MyEnum.VAL1;
|
MyEnum myEnum = MyEnum.VAL1;
|
||||||
names = JSONObject.getNames(myEnum);
|
names = JSONObject.getNames(myEnum);
|
||||||
// The values will be MyEnmField fields, so need to convert back to string for comparison
|
// The values will be MyEnum fields
|
||||||
jsonObject = new JSONObject(myEnum, names);
|
jsonObject = new JSONObject(myEnum, names);
|
||||||
finalJsonObject = new JSONObject(jsonObject.toString());
|
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
|
|
||||||
|
|
||||||
expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
|
// validate JSON object
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
|
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
||||||
|
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
||||||
|
|
||||||
MyEnumField myEnumField = MyEnumField.VAL3;
|
MyEnumField myEnumField = MyEnumField.VAL3;
|
||||||
names = JSONObject.getNames(myEnumField);
|
names = JSONObject.getNames(myEnumField);
|
||||||
// The values will be MyEnmField fields, so need to convert back to string for comparison
|
// The values will be MyEnmField fields
|
||||||
jsonObject = new JSONObject(myEnumField, names);
|
jsonObject = new JSONObject(myEnumField, names);
|
||||||
finalJsonObject = new JSONObject(jsonObject.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
|
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
||||||
|
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,29 +96,33 @@ public class EnumTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void enumPut() {
|
public void enumPut() {
|
||||||
String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}";
|
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
MyEnum myEnum = MyEnum.VAL2;
|
MyEnum myEnum = MyEnum.VAL2;
|
||||||
jsonObject.put("myEnum", myEnum);
|
jsonObject.put("myEnum", myEnum);
|
||||||
assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.get("myEnum")));
|
|
||||||
assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.opt("myEnum")));
|
|
||||||
MyEnumField myEnumField = MyEnumField.VAL1;
|
MyEnumField myEnumField = MyEnumField.VAL1;
|
||||||
jsonObject.putOnce("myEnumField", myEnumField);
|
jsonObject.putOnce("myEnumField", myEnumField);
|
||||||
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.get("myEnumField")));
|
|
||||||
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.opt("myEnumField")));
|
|
||||||
JSONObject finalJsonObject = new JSONObject(jsonObject.toString());
|
|
||||||
JSONObject expectedFinalJsonObject = new JSONObject(expectedFinalStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedFinalJsonObject);
|
|
||||||
|
|
||||||
|
// validate JSON content
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level objects", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum")));
|
||||||
|
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.myEnumField")));
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
jsonArray.put(myEnum);
|
jsonArray.put(myEnum);
|
||||||
jsonArray.put(1, myEnumField);
|
jsonArray.put(1, myEnumField);
|
||||||
|
|
||||||
|
// validate JSON content
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 2 top level objects", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Leaving these tests because they exercise get, opt, and remove
|
||||||
|
*/
|
||||||
assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0)));
|
assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0)));
|
||||||
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1)));
|
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1)));
|
||||||
JSONArray expectedJsonArray = new JSONArray();
|
|
||||||
expectedJsonArray.put(MyEnum.VAL2);
|
|
||||||
expectedJsonArray.put(MyEnumField.VAL1);
|
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
|
||||||
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
|
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,49 +170,66 @@ public class EnumTest {
|
||||||
|
|
||||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||||
jsonObject = new JSONObject(myEnumField);
|
jsonObject = new JSONObject(myEnumField);
|
||||||
expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
|
|
||||||
JSONObject actualJsonObject = new JSONObject(jsonObject.toString());
|
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
|
|
||||||
|
|
||||||
expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
|
// validate JSON content
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
|
||||||
|
|
||||||
MyEnumClass myEnumClass = new MyEnumClass();
|
MyEnumClass myEnumClass = new MyEnumClass();
|
||||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||||
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
||||||
jsonObject = new JSONObject(myEnumClass);
|
jsonObject = new JSONObject(myEnumClass);
|
||||||
actualJsonObject = new JSONObject(jsonObject.toString());
|
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
|
|
||||||
|
|
||||||
expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
|
// validate JSON content
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected 2 myEnumField items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
||||||
|
assertTrue("expected 0 myEnum items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
|
||||||
|
assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
|
||||||
|
|
||||||
String [] names = JSONObject.getNames(myEnum);
|
String [] names = JSONObject.getNames(myEnum);
|
||||||
jsonObject = new JSONObject(myEnum, names);
|
jsonObject = new JSONObject(myEnum, names);
|
||||||
actualJsonObject = new JSONObject(jsonObject.toString());
|
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
|
|
||||||
|
|
||||||
expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
|
// validate JSON content
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
|
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
||||||
|
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
||||||
|
|
||||||
names = JSONObject.getNames(myEnumField);
|
names = JSONObject.getNames(myEnumField);
|
||||||
jsonObject = new JSONObject(myEnumField, names);
|
jsonObject = new JSONObject(myEnumField, names);
|
||||||
actualJsonObject = new JSONObject(jsonObject.toString());
|
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
// validate JSON content
|
||||||
Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
|
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
||||||
|
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
||||||
|
|
||||||
expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
|
expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
|
||||||
jsonObject = new JSONObject();
|
jsonObject = new JSONObject();
|
||||||
jsonObject.putOpt("myEnum", myEnum);
|
jsonObject.putOpt("myEnum", myEnum);
|
||||||
jsonObject.putOnce("myEnumField", myEnumField);
|
jsonObject.putOnce("myEnumField", myEnumField);
|
||||||
actualJsonObject = new JSONObject(jsonObject.toString());
|
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
|
|
||||||
|
|
||||||
expectedStr = "[\"VAL2\", \"VAL2\"]";
|
// validate JSON content
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum")));
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnumField")));
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
jsonArray.put(myEnum);
|
jsonArray.put(myEnum);
|
||||||
jsonArray.put(1, myEnumField);
|
jsonArray.put(1, myEnumField);
|
||||||
JSONArray actualJsonArray = new JSONArray(jsonArray.toString());
|
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
// validate JSON content
|
||||||
Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray);
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[1]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -206,19 +242,28 @@ public class EnumTest {
|
||||||
JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum);
|
JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum);
|
||||||
assertTrue("simple enum has no getters", jsonObject.length() == 0);
|
assertTrue("simple enum has no getters", jsonObject.length() == 0);
|
||||||
|
|
||||||
String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
|
|
||||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||||
jsonObject = (JSONObject)JSONObject.wrap(myEnumField);
|
jsonObject = (JSONObject)JSONObject.wrap(myEnumField);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
|
||||||
|
|
||||||
expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
|
// validate JSON content
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
|
||||||
|
|
||||||
MyEnumClass myEnumClass = new MyEnumClass();
|
MyEnumClass myEnumClass = new MyEnumClass();
|
||||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||||
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
myEnumClass.setMyEnumField(MyEnumField.VAL3);
|
||||||
jsonObject = (JSONObject)JSONObject.wrap(myEnumClass);
|
jsonObject = (JSONObject)JSONObject.wrap(myEnumClass);
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON content
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected 2 myEnumField items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
||||||
|
assertTrue("expected 0 myEnum items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
|
||||||
|
assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,18 +2,15 @@ package org.json.junit;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for JSON-Java JSONArray.java
|
* Tests for JSON-Java JSONArray.java
|
||||||
|
@ -306,41 +303,37 @@ public class JSONArrayTest {
|
||||||
/**
|
/**
|
||||||
* Exercise JSONArray.join() by converting a JSONArray into a
|
* Exercise JSONArray.join() by converting a JSONArray into a
|
||||||
* comma-separated string. Since this is very nearly a JSON document,
|
* comma-separated string. Since this is very nearly a JSON document,
|
||||||
* array braces are added to the beginning and end, and it is reconverted
|
* array braces are added to the beginning and end prior to validation.
|
||||||
* back to a JSONArray for comparison.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void join() {
|
public void join() {
|
||||||
String expectedStr =
|
|
||||||
"["+
|
|
||||||
"true,"+
|
|
||||||
"false,"+
|
|
||||||
"\"true\","+
|
|
||||||
"\"false\","+
|
|
||||||
"\"hello\","+
|
|
||||||
"0.002345,"+
|
|
||||||
"\"23.45\","+
|
|
||||||
"42,"+
|
|
||||||
"\"43\","+
|
|
||||||
"["+
|
|
||||||
"\"world\""+
|
|
||||||
"],"+
|
|
||||||
"{"+
|
|
||||||
"\"key1\":\"value1\","+
|
|
||||||
"\"key2\":\"value2\","+
|
|
||||||
"\"key3\":\"value3\","+
|
|
||||||
"\"key4\":\"value4\""+
|
|
||||||
"},"+
|
|
||||||
"0,"+
|
|
||||||
"\"-1\""+
|
|
||||||
"]";
|
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(arrayStr);
|
||||||
String joinStr = jsonArray.join(",");
|
String joinStr = jsonArray.join(",");
|
||||||
JSONArray finalJsonArray = new JSONArray("["+joinStr+"]");
|
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
// validate JSON
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
/**
|
||||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
* Don't need to remake the JSONArray to perform the parsing
|
||||||
|
*/
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse("["+joinStr+"]");
|
||||||
|
assertTrue("expected 13 items in top level object", ((List<?>)(JsonPath.read(doc, "$"))).size() == 13);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected \"true\"", "true".equals(JsonPath.read(doc, "$[2]")));
|
||||||
|
assertTrue("expected \"false\"", "false".equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(JsonPath.read(doc, "$[5]")));
|
||||||
|
assertTrue("expected \"23.45\"", "23.45".equals(JsonPath.read(doc, "$[6]")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[7]")));
|
||||||
|
assertTrue("expected \"43\"", "43".equals(JsonPath.read(doc, "$[8]")));
|
||||||
|
assertTrue("expected 1 item in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 1);
|
||||||
|
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]")));
|
||||||
|
assertTrue("expected 4 items in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 4);
|
||||||
|
assertTrue("expected value1", "value1".equals(JsonPath.read(doc, "$[10].key1")));
|
||||||
|
assertTrue("expected value2", "value2".equals(JsonPath.read(doc, "$[10].key2")));
|
||||||
|
assertTrue("expected value3", "value3".equals(JsonPath.read(doc, "$[10].key3")));
|
||||||
|
assertTrue("expected value4", "value4".equals(JsonPath.read(doc, "$[10].key4")));
|
||||||
|
assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$[11]")));
|
||||||
|
assertTrue("expected \"-1\"", "-1".equals(JsonPath.read(doc, "$[12]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -419,33 +412,7 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void put() {
|
public void put() {
|
||||||
String expectedStr =
|
|
||||||
"["+
|
|
||||||
"true,"+
|
|
||||||
"false,"+
|
|
||||||
"["+
|
|
||||||
"hello,"+
|
|
||||||
"world"+
|
|
||||||
"],"+
|
|
||||||
"2.5,"+
|
|
||||||
"1,"+
|
|
||||||
"45,"+
|
|
||||||
"\"objectPut\","+
|
|
||||||
"{"+
|
|
||||||
"\"key10\":\"val10\","+
|
|
||||||
"\"key20\":\"val20\","+
|
|
||||||
"\"key30\":\"val30\""+
|
|
||||||
"},"+
|
|
||||||
"{"+
|
|
||||||
"\"k1\":\"v1\""+
|
|
||||||
"},"+
|
|
||||||
"["+
|
|
||||||
"1,"+
|
|
||||||
"2"+
|
|
||||||
"]"+
|
|
||||||
"]";
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
|
||||||
|
|
||||||
// index 0
|
// index 0
|
||||||
jsonArray.put(true);
|
jsonArray.put(true);
|
||||||
|
@ -488,9 +455,30 @@ public class JSONArrayTest {
|
||||||
Collection<Object> collection = new ArrayList<Object>();
|
Collection<Object> collection = new ArrayList<Object>();
|
||||||
collection.add(1);
|
collection.add(1);
|
||||||
collection.add(2);
|
collection.add(2);
|
||||||
// 9
|
// 9
|
||||||
jsonArray.put(collection);
|
jsonArray.put(collection);
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
|
||||||
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 10 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 10);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);
|
||||||
|
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
|
||||||
|
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
|
||||||
|
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
|
||||||
|
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
|
||||||
|
assertTrue("expected 3 items in [7]", ((Map<?,?>)(JsonPath.read(doc, "$[7]"))).size() == 3);
|
||||||
|
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[7].key10")));
|
||||||
|
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[7].key20")));
|
||||||
|
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[7].key30")));
|
||||||
|
assertTrue("expected 1 item in [8]", ((Map<?,?>)(JsonPath.read(doc, "$[8]"))).size() == 1);
|
||||||
|
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[8].k1")));
|
||||||
|
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -499,34 +487,7 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void putIndex() {
|
public void putIndex() {
|
||||||
String expectedStr =
|
|
||||||
"["+
|
|
||||||
"true,"+
|
|
||||||
"false,"+
|
|
||||||
"["+
|
|
||||||
"hello,"+
|
|
||||||
"world"+
|
|
||||||
"],"+
|
|
||||||
"2.5,"+
|
|
||||||
"1,"+
|
|
||||||
"45,"+
|
|
||||||
"\"objectPut\","+
|
|
||||||
"null,"+
|
|
||||||
"{"+
|
|
||||||
"\"key10\":\"val10\","+
|
|
||||||
"\"key20\":\"val20\","+
|
|
||||||
"\"key30\":\"val30\""+
|
|
||||||
"},"+
|
|
||||||
"["+
|
|
||||||
"1,"+
|
|
||||||
"2"+
|
|
||||||
"],"+
|
|
||||||
"{"+
|
|
||||||
"\"k1\":\"v1\""+
|
|
||||||
"},"+
|
|
||||||
"]";
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
|
||||||
|
|
||||||
// 1
|
// 1
|
||||||
jsonArray.put(1, false);
|
jsonArray.put(1, false);
|
||||||
|
@ -573,7 +534,29 @@ public class JSONArrayTest {
|
||||||
jsonArray.put(-1, "abc");
|
jsonArray.put(-1, "abc");
|
||||||
assertTrue("put index < 0 should have thrown exception", false);
|
assertTrue("put index < 0 should have thrown exception", false);
|
||||||
} catch(Exception ignored) {}
|
} catch(Exception ignored) {}
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
|
||||||
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 11 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 11);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);
|
||||||
|
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
|
||||||
|
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
|
||||||
|
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
|
||||||
|
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
|
||||||
|
assertTrue("expected null", null == JsonPath.read(doc, "$[7]"));
|
||||||
|
assertTrue("expected 3 items in [8]", ((Map<?,?>)(JsonPath.read(doc, "$[8]"))).size() == 3);
|
||||||
|
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[8].key10")));
|
||||||
|
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[8].key20")));
|
||||||
|
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[8].key30")));
|
||||||
|
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
|
||||||
|
assertTrue("expected 1 item in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 1);
|
||||||
|
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[10].k1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -587,10 +570,9 @@ public class JSONArrayTest {
|
||||||
"1"+
|
"1"+
|
||||||
"]";
|
"]";
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(arrayStr);
|
||||||
JSONArray expectedJsonArray = new JSONArray();
|
|
||||||
jsonArray.remove(0);
|
jsonArray.remove(0);
|
||||||
assertTrue("array should be empty", null == jsonArray.remove(5));
|
assertTrue("array should be empty", null == jsonArray.remove(5));
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
assertTrue("jsonArray should be empty", jsonArray.length() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -648,15 +630,20 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void objectArrayVsIsArray() {
|
public void objectArrayVsIsArray() {
|
||||||
String expectedStr =
|
|
||||||
"["+
|
|
||||||
"1,2,3,4,5,6,7"+
|
|
||||||
"]";
|
|
||||||
int[] myInts = { 1, 2, 3, 4, 5, 6, 7 };
|
int[] myInts = { 1, 2, 3, 4, 5, 6, 7 };
|
||||||
Object myObject = myInts;
|
Object myObject = myInts;
|
||||||
JSONArray jsonArray = new JSONArray(myObject);
|
JSONArray jsonArray = new JSONArray(myObject);
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 7 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 7);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
||||||
|
assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$[5]")));
|
||||||
|
assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$[6]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -10,15 +10,7 @@ import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.json.CDL;
|
import org.json.CDL;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
@ -28,6 +20,8 @@ import org.json.JSONString;
|
||||||
import org.json.XML;
|
import org.json.XML;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used in testing when a JSONString is needed
|
* Used in testing when a JSONString is needed
|
||||||
*/
|
*/
|
||||||
|
@ -112,17 +106,16 @@ public class JSONObjectTest {
|
||||||
"\"doubleKey\":-23.45e67"+
|
"\"doubleKey\":-23.45e67"+
|
||||||
"}";
|
"}";
|
||||||
String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"};
|
String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"};
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"falseKey\":false,"+
|
|
||||||
"\"nullKey\":null,"+
|
|
||||||
"\"stringKey\":\"hello world!\","+
|
|
||||||
"\"doubleKey\":-23.45e67"+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
JSONObject copyJsonObject = new JSONObject(jsonObject, keys);
|
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
// validate JSON
|
||||||
Util.compareActualVsExpectedJsonObjects(copyJsonObject, expectedJsonObject);
|
JSONObject jsonObjectByName = new JSONObject(jsonObject, keys);
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString());
|
||||||
|
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
||||||
|
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
||||||
|
assertTrue("expected \"nullKey\":null", null == JsonPath.read(doc, "$.nullKey"));
|
||||||
|
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
||||||
|
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -135,8 +128,7 @@ public class JSONObjectTest {
|
||||||
public void jsonObjectByNullMap() {
|
public void jsonObjectByNullMap() {
|
||||||
Map<String, Object> map = null;
|
Map<String, Object> map = null;
|
||||||
JSONObject jsonObject = new JSONObject(map);
|
JSONObject jsonObject = new JSONObject(map);
|
||||||
JSONObject expectedJsonObject = new JSONObject();
|
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -145,26 +137,23 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectByMap() {
|
public void jsonObjectByMap() {
|
||||||
String expectedStr =
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
"{"+
|
map.put("trueKey", new Boolean(true));
|
||||||
"\"trueKey\":true,"+
|
map.put("falseKey", new Boolean(false));
|
||||||
"\"falseKey\":false,"+
|
map.put("stringKey", "hello world!");
|
||||||
"\"stringKey\":\"hello world!\","+
|
map.put("escapeStringKey", "h\be\tllo w\u1234orld!");
|
||||||
"\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+
|
map.put("intKey", new Long(42));
|
||||||
"\"intKey\":42,"+
|
map.put("doubleKey", new Double(-23.45e67));
|
||||||
"\"doubleKey\":-23.45e67"+
|
JSONObject jsonObject = new JSONObject(map);
|
||||||
"}";
|
|
||||||
Map<String, Object> jsonMap = new HashMap<String, Object>();
|
|
||||||
jsonMap.put("trueKey", new Boolean(true));
|
|
||||||
jsonMap.put("falseKey", new Boolean(false));
|
|
||||||
jsonMap.put("stringKey", "hello world!");
|
|
||||||
jsonMap.put("escapeStringKey", "h\be\tllo w\u1234orld!");
|
|
||||||
jsonMap.put("intKey", new Long(42));
|
|
||||||
jsonMap.put("doubleKey", new Double(-23.45e67));
|
|
||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(jsonMap);
|
// validate JSON
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
|
assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
||||||
|
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
||||||
|
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
||||||
|
assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey")));
|
||||||
|
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -290,19 +279,18 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectByMapWithUnsupportedValues() {
|
public void jsonObjectByMapWithUnsupportedValues() {
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"key1\":{},"+
|
|
||||||
"\"key2\":\"java.lang.Exception\""+
|
|
||||||
"}";
|
|
||||||
Map<String, Object> jsonMap = new HashMap<String, Object>();
|
Map<String, Object> jsonMap = new HashMap<String, Object>();
|
||||||
// Just insert some random objects
|
// Just insert some random objects
|
||||||
jsonMap.put("key1", new CDL());
|
jsonMap.put("key1", new CDL());
|
||||||
jsonMap.put("key2", new Exception());
|
jsonMap.put("key2", new Exception());
|
||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(jsonMap);
|
JSONObject jsonObject = new JSONObject(jsonMap);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(JsonPath.read(doc, "$.key2")));
|
||||||
|
assertTrue("expected 0 key1 items", ((Map<?,?>)(JsonPath.read(doc, "$.key1"))).size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -311,27 +299,25 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectByMapWithNullValue() {
|
public void jsonObjectByMapWithNullValue() {
|
||||||
String expectedStr =
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
"{"+
|
map.put("trueKey", new Boolean(true));
|
||||||
"\"trueKey\":true,"+
|
map.put("falseKey", new Boolean(false));
|
||||||
"\"falseKey\":false,"+
|
map.put("stringKey", "hello world!");
|
||||||
"\"stringKey\":\"hello world!\","+
|
map.put("nullKey", null);
|
||||||
"\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+
|
map.put("escapeStringKey", "h\be\tllo w\u1234orld!");
|
||||||
"\"intKey\":42,"+
|
map.put("intKey", new Long(42));
|
||||||
"\"doubleKey\":-23.45e67"+
|
map.put("doubleKey", new Double(-23.45e67));
|
||||||
"}";
|
JSONObject jsonObject = new JSONObject(map);
|
||||||
Map<String, Object> jsonMap = new HashMap<String, Object>();
|
|
||||||
jsonMap.put("trueKey", new Boolean(true));
|
|
||||||
jsonMap.put("falseKey", new Boolean(false));
|
|
||||||
jsonMap.put("stringKey", "hello world!");
|
|
||||||
jsonMap.put("nullKey", null);
|
|
||||||
jsonMap.put("escapeStringKey", "h\be\tllo w\u1234orld!");
|
|
||||||
jsonMap.put("intKey", new Long(42));
|
|
||||||
jsonMap.put("doubleKey", new Double(-23.45e67));
|
|
||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(jsonMap);
|
// validate JSON
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
|
assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
||||||
|
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
||||||
|
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
||||||
|
assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey")));
|
||||||
|
assertTrue("expected \"intKey\":42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey")));
|
||||||
|
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -340,18 +326,6 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectByBean() {
|
public void jsonObjectByBean() {
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"trueKey\":true,"+
|
|
||||||
"\"falseKey\":false,"+
|
|
||||||
"\"stringKey\":\"hello world!\","+
|
|
||||||
"\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+
|
|
||||||
"\"intKey\":42,"+
|
|
||||||
"\"doubleKey\":-23.45e7,"+
|
|
||||||
"\"stringReaderKey\":{},"+
|
|
||||||
"\"callbacks\":[{\"handler\":{}},{}]"+ // sorry, mockito artifact
|
|
||||||
"}";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default access classes have to be mocked since JSONObject, which is
|
* Default access classes have to be mocked since JSONObject, which is
|
||||||
* not in the same package, cannot call MyBean methods by reflection.
|
* not in the same package, cannot call MyBean methods by reflection.
|
||||||
|
@ -377,8 +351,21 @@ public class JSONObjectTest {
|
||||||
});
|
});
|
||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(myBean);
|
JSONObject jsonObject = new JSONObject(myBean);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 8 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 8);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
||||||
|
assertTrue("expected hello world!","hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
||||||
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey")));
|
||||||
|
assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey")));
|
||||||
|
assertTrue("expected 0 items in stringReaderKey", ((Map<?, ?>) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0);
|
||||||
|
// sorry, mockito artifact
|
||||||
|
assertTrue("expected 2 callbacks items", ((List<?>)(JsonPath.read(doc, "$.callbacks"))).size() == 2);
|
||||||
|
assertTrue("expected 0 handler items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0);
|
||||||
|
assertTrue("expected 0 callbacks[1] items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -389,17 +376,16 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectByObjectAndNames() {
|
public void jsonObjectByObjectAndNames() {
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"publicString\":\"abc\","+
|
|
||||||
"\"publicInt\":42"+
|
|
||||||
"}";
|
|
||||||
String[] keys = {"publicString", "publicInt"};
|
String[] keys = {"publicString", "publicInt"};
|
||||||
// just need a class that has public data members
|
// just need a class that has public data members
|
||||||
JSONObjectTest jsonObjectTest = new JSONObjectTest();
|
JSONObjectTest jsonObjectTest = new JSONObjectTest();
|
||||||
JSONObject jsonObject = new JSONObject(jsonObjectTest, keys);
|
JSONObject jsonObject = new JSONObject(jsonObjectTest, keys);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected \"publicString\":\"abc\"", "abc".equals(JsonPath.read(doc, "$.publicString")));
|
||||||
|
assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -408,22 +394,19 @@ public class JSONObjectTest {
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectByResourceBundle() {
|
public void jsonObjectByResourceBundle() {
|
||||||
// TODO: how to improve resource bundle testing?
|
// TODO: how to improve resource bundle testing?
|
||||||
String expectedStr =
|
JSONObject jsonObject = new
|
||||||
"{"+
|
|
||||||
"\"greetings\": {"+
|
|
||||||
"\"hello\":\"Hello, \","+
|
|
||||||
"\"world\":\"World!\""+
|
|
||||||
"},"+
|
|
||||||
"\"farewells\": {"+
|
|
||||||
"\"later\":\"Later, \","+
|
|
||||||
"\"gator\":\"Alligator!\""+
|
|
||||||
"}"+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = new
|
|
||||||
JSONObject("org.json.junit.StringsResourceBundle",
|
JSONObject("org.json.junit.StringsResourceBundle",
|
||||||
Locale.getDefault());
|
Locale.getDefault());
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
|
assertTrue("expected 2 greetings items", ((Map<?,?>)(JsonPath.read(doc, "$.greetings"))).size() == 2);
|
||||||
|
assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello")));
|
||||||
|
assertTrue("expected \"world\":\"World!\"", "World!".equals(JsonPath.read(doc, "$.greetings.world")));
|
||||||
|
assertTrue("expected 2 farewells items", ((Map<?,?>)(JsonPath.read(doc, "$.farewells"))).size() == 2);
|
||||||
|
assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(JsonPath.read(doc, "$.farewells.later")));
|
||||||
|
assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -432,17 +415,7 @@ public class JSONObjectTest {
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectAccumulate() {
|
public void jsonObjectAccumulate() {
|
||||||
// TODO: should include an unsupported object
|
// TODO: should include an unsupported object
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"myArray\": ["+
|
|
||||||
"true,"+
|
|
||||||
"false,"+
|
|
||||||
"\"hello world!\","+
|
|
||||||
"\"h\be\tllo w\u1234orld!\","+
|
|
||||||
"42,"+
|
|
||||||
"-23.45e7"+
|
|
||||||
"]"+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.accumulate("myArray", true);
|
jsonObject.accumulate("myArray", true);
|
||||||
jsonObject.accumulate("myArray", false);
|
jsonObject.accumulate("myArray", false);
|
||||||
|
@ -450,8 +423,17 @@ public class JSONObjectTest {
|
||||||
jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!");
|
jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!");
|
||||||
jsonObject.accumulate("myArray", 42);
|
jsonObject.accumulate("myArray", 42);
|
||||||
jsonObject.accumulate("myArray", -23.45e7);
|
jsonObject.accumulate("myArray", -23.45e7);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
|
assertTrue("expected 6 myArray items", ((List<?>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]")));
|
||||||
|
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]")));
|
||||||
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]")));
|
||||||
|
assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -460,17 +442,6 @@ public class JSONObjectTest {
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectAppend() {
|
public void jsonObjectAppend() {
|
||||||
// TODO: should include an unsupported object
|
// TODO: should include an unsupported object
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"myArray\": ["+
|
|
||||||
"true,"+
|
|
||||||
"false,"+
|
|
||||||
"\"hello world!\","+
|
|
||||||
"\"h\be\tllo w\u1234orld!\","+
|
|
||||||
"42,"+
|
|
||||||
"-23.45e7"+
|
|
||||||
"]"+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.append("myArray", true);
|
jsonObject.append("myArray", true);
|
||||||
jsonObject.append("myArray", false);
|
jsonObject.append("myArray", false);
|
||||||
|
@ -478,8 +449,17 @@ public class JSONObjectTest {
|
||||||
jsonObject.append("myArray", "h\be\tllo w\u1234orld!");
|
jsonObject.append("myArray", "h\be\tllo w\u1234orld!");
|
||||||
jsonObject.append("myArray", 42);
|
jsonObject.append("myArray", 42);
|
||||||
jsonObject.append("myArray", -23.45e7);
|
jsonObject.append("myArray", -23.45e7);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
|
assertTrue("expected 6 myArray items", ((List<?>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]")));
|
||||||
|
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]")));
|
||||||
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]")));
|
||||||
|
assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1058,19 +1038,47 @@ public class JSONObjectTest {
|
||||||
"\"falseKey\":false,"+
|
"\"falseKey\":false,"+
|
||||||
"\"stringKey\":\"hello world!\","+
|
"\"stringKey\":\"hello world!\","+
|
||||||
"}";
|
"}";
|
||||||
String [] expectedNames = {"trueKey", "falseKey", "stringKey"};
|
|
||||||
jsonObject = new JSONObject(str);
|
jsonObject = new JSONObject(str);
|
||||||
names = JSONObject.getNames(jsonObject);
|
names = JSONObject.getNames(jsonObject);
|
||||||
Util.compareActualVsExpectedStringArrays(names, expectedNames);
|
JSONArray jsonArray = new JSONArray(names);
|
||||||
|
|
||||||
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse(jsonArray.toString());
|
||||||
|
List<?> docList = JsonPath.read(doc, "$");
|
||||||
|
assertTrue("expected 3 items", docList.size() == 3);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find trueKey",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find falseKey",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find stringKey",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getNames() from an enum with properties has an interesting result.
|
* getNames() from an enum with properties has an interesting result.
|
||||||
* It returns the enum values, not the selected enum properties
|
* It returns the enum values, not the selected enum properties
|
||||||
*/
|
*/
|
||||||
MyEnumField myEnumField = MyEnumField.VAL1;
|
MyEnumField myEnumField = MyEnumField.VAL1;
|
||||||
String[] enumExpectedNames = {"VAL1", "VAL2", "VAL3"};
|
|
||||||
names = JSONObject.getNames(myEnumField);
|
names = JSONObject.getNames(myEnumField);
|
||||||
Util.compareActualVsExpectedStringArrays(names, enumExpectedNames);
|
|
||||||
|
// validate JSON
|
||||||
|
jsonArray = new JSONArray(names);
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse(jsonArray.toString());
|
||||||
|
docList = JsonPath.read(doc, "$");
|
||||||
|
assertTrue("expected 3 items", docList.size() == 3);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find VAL1",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find VAL2",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find VAL3",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A bean is also an object. But in order to test the static
|
* A bean is also an object. But in order to test the static
|
||||||
|
@ -1078,9 +1086,20 @@ public class JSONObjectTest {
|
||||||
* data members, which have been added to the class.
|
* data members, which have been added to the class.
|
||||||
*/
|
*/
|
||||||
JSONObjectTest jsonObjectTest = new JSONObjectTest();
|
JSONObjectTest jsonObjectTest = new JSONObjectTest();
|
||||||
String [] jsonObjectTestExpectedNames = {"publicString", "publicInt"};
|
|
||||||
names = JSONObject.getNames(jsonObjectTest);
|
names = JSONObject.getNames(jsonObjectTest);
|
||||||
Util.compareActualVsExpectedStringArrays(names, jsonObjectTestExpectedNames);
|
|
||||||
|
// validate JSON
|
||||||
|
jsonArray = new JSONArray(names);
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse(jsonArray.toString());
|
||||||
|
docList = JsonPath.read(doc, "$");
|
||||||
|
assertTrue("expected 2 items", docList.size() == 2);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find publicString",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1);
|
||||||
|
assertTrue(
|
||||||
|
"expected to find publicInt",
|
||||||
|
((List<?>) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1106,22 +1125,16 @@ public class JSONObjectTest {
|
||||||
"\"falseKey\":false,"+
|
"\"falseKey\":false,"+
|
||||||
"\"stringKey\":\"hello world!\","+
|
"\"stringKey\":\"hello world!\","+
|
||||||
"}";
|
"}";
|
||||||
String [] expectedNames = {"trueKey", "falseKey", "stringKey" };
|
|
||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
JSONArray jsonArray = jsonObject.names();
|
JSONArray jsonArray = jsonObject.names();
|
||||||
/**
|
|
||||||
* Cannot really compare to an expected JSONArray because the ordering
|
|
||||||
* of the JSONObject keys is not fixed, and JSONArray comparisons
|
|
||||||
* presume fixed. Since this test is limited to key strings, a
|
|
||||||
* string comparison will have to suffice.
|
|
||||||
*/
|
|
||||||
String namesStr = jsonArray.toString();
|
|
||||||
// remove square brackets, commas, and spaces
|
|
||||||
namesStr = namesStr.replaceAll("[\\]|\\[|\"]", "");
|
|
||||||
String [] names = namesStr.split(",");
|
|
||||||
|
|
||||||
Util.compareActualVsExpectedStringArrays(names, expectedNames);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
|
assertTrue("expected to find trueKey", ((List<?>) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1);
|
||||||
|
assertTrue("expected to find falseKey", ((List<?>) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1);
|
||||||
|
assertTrue("expected to find stringKey", ((List<?>) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1134,38 +1147,6 @@ public class JSONObjectTest {
|
||||||
"\"keyLong\":9999999991,"+
|
"\"keyLong\":9999999991,"+
|
||||||
"\"keyDouble\":1.1,"+
|
"\"keyDouble\":1.1,"+
|
||||||
"}";
|
"}";
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"keyInt\":3,"+
|
|
||||||
"\"keyLong\":9999999993,"+
|
|
||||||
"\"keyDouble\":3.1,"+
|
|
||||||
/**
|
|
||||||
* Should work the same way on any platform! @see
|
|
||||||
* https://docs.oracle
|
|
||||||
* .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is
|
|
||||||
* the effect of a float to double conversion and is inherent to
|
|
||||||
* the shortcomings of the IEEE 754 format, when converting
|
|
||||||
* 32-bit into double-precision 64-bit. Java type-casts float to
|
|
||||||
* double. A 32 bit float is type-casted to 64 bit double by
|
|
||||||
* simply appending zero-bits to the mantissa (and extended the
|
|
||||||
* signed exponent by 3 bits.) and there is no way to obtain
|
|
||||||
* more information than it is stored in the 32-bits float.
|
|
||||||
*
|
|
||||||
* Like 1/3 cannot be represented as base10 number because it is
|
|
||||||
* periodically, 1/5 (for example) cannot be represented as
|
|
||||||
* base2 number since it is periodically in base2 (take a look
|
|
||||||
* at http://www.h-schmidt.net/FloatConverter/) The same happens
|
|
||||||
* to 3.1, that decimal number (base10 representation) is
|
|
||||||
* periodic in base2 representation, therefore appending
|
|
||||||
* zero-bits is inaccurate. Only repeating the periodically
|
|
||||||
* occuring bits (0110) would be a proper conversion. However
|
|
||||||
* one cannot detect from a 32 bit IEE754 representation which
|
|
||||||
* bits would "repeat infinitely", since the missing bits would
|
|
||||||
* not fit into the 32 bit float, i.e. the information needed
|
|
||||||
* simply is not there!
|
|
||||||
*/
|
|
||||||
"\"keyFloat\":3.0999999046325684,"+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
jsonObject.increment("keyInt");
|
jsonObject.increment("keyInt");
|
||||||
jsonObject.increment("keyInt");
|
jsonObject.increment("keyInt");
|
||||||
|
@ -1177,16 +1158,47 @@ public class JSONObjectTest {
|
||||||
jsonObject.put("keyFloat", new Float(1.1));
|
jsonObject.put("keyFloat", new Float(1.1));
|
||||||
jsonObject.increment("keyFloat");
|
jsonObject.increment("keyFloat");
|
||||||
jsonObject.increment("keyFloat");
|
jsonObject.increment("keyFloat");
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
|
||||||
|
|
||||||
/**
|
// validate JSON
|
||||||
* float f = 3.1f;
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
* double df = (double) f;
|
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
||||||
* double d = 3.1d;
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.keyInt")));
|
||||||
* System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(f)));
|
assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(JsonPath.read(doc, "$.keyLong")));
|
||||||
* System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(df)));
|
assertTrue("expected 3.1", Double.valueOf(3.1).equals(JsonPath.read(doc, "$.keyDouble")));
|
||||||
* System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d)));
|
|
||||||
|
/**
|
||||||
|
* Should work the same way on any platform! @see https://docs.oracle
|
||||||
|
* .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the
|
||||||
|
* effect of a float to double conversion and is inherent to the
|
||||||
|
* shortcomings of the IEEE 754 format, when converting 32-bit into
|
||||||
|
* double-precision 64-bit. Java type-casts float to double. A 32 bit
|
||||||
|
* float is type-casted to 64 bit double by simply appending zero-bits
|
||||||
|
* to the mantissa (and extended the signed exponent by 3 bits.) and
|
||||||
|
* there is no way to obtain more information than it is stored in the
|
||||||
|
* 32-bits float.
|
||||||
|
*
|
||||||
|
* Like 1/3 cannot be represented as base10 number because it is
|
||||||
|
* periodically, 1/5 (for example) cannot be represented as base2 number
|
||||||
|
* since it is periodically in base2 (take a look at
|
||||||
|
* http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1,
|
||||||
|
* that decimal number (base10 representation) is periodic in base2
|
||||||
|
* representation, therefore appending zero-bits is inaccurate. Only
|
||||||
|
* repeating the periodically occuring bits (0110) would be a proper
|
||||||
|
* conversion. However one cannot detect from a 32 bit IEE754
|
||||||
|
* representation which bits would "repeat infinitely", since the
|
||||||
|
* missing bits would not fit into the 32 bit float, i.e. the
|
||||||
|
* information needed simply is not there!
|
||||||
|
*/
|
||||||
|
assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(JsonPath.read(doc, "$.keyFloat")));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* float f = 3.1f; double df = (double) f; double d = 3.1d;
|
||||||
|
* System.out.println
|
||||||
|
* (Integer.toBinaryString(Float.floatToRawIntBits(f)));
|
||||||
|
* System.out.println
|
||||||
|
* (Long.toBinaryString(Double.doubleToRawLongBits(df)));
|
||||||
|
* System.out.println
|
||||||
|
* (Long.toBinaryString(Double.doubleToRawLongBits(d)));
|
||||||
*
|
*
|
||||||
* - Float:
|
* - Float:
|
||||||
* seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
|
* seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
|
||||||
|
@ -1216,7 +1228,7 @@ public class JSONObjectTest {
|
||||||
assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) );
|
assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) );
|
||||||
|
|
||||||
JSONObject inc = new JSONObject();
|
JSONObject inc = new JSONObject();
|
||||||
inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value )
|
inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value )
|
||||||
assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float );
|
assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float );
|
||||||
inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double!
|
inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double!
|
||||||
// this.put(key, (Float) value + 1);
|
// this.put(key, (Float) value + 1);
|
||||||
|
@ -1272,17 +1284,6 @@ public class JSONObjectTest {
|
||||||
"\"myKey4\":\"myVal4\""+
|
"\"myKey4\":\"myVal4\""+
|
||||||
"}"+
|
"}"+
|
||||||
"}";
|
"}";
|
||||||
String expectedStrAfterRemoval =
|
|
||||||
"{"+
|
|
||||||
"\"falseKey\":false,"+
|
|
||||||
"\"arrayKey\":[0,1,2],"+
|
|
||||||
"\"objectKey\":{"+
|
|
||||||
"\"myKey1\":\"myVal1\","+
|
|
||||||
"\"myKey2\":\"myVal2\","+
|
|
||||||
"\"myKey3\":\"myVal3\","+
|
|
||||||
"\"myKey4\":\"myVal4\""+
|
|
||||||
"}"+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.put("trueKey", true);
|
jsonObject.put("trueKey", true);
|
||||||
jsonObject.put("falseKey", false);
|
jsonObject.put("falseKey", false);
|
||||||
|
@ -1294,19 +1295,27 @@ public class JSONObjectTest {
|
||||||
myMap.put("myKey3", "myVal3");
|
myMap.put("myKey3", "myVal3");
|
||||||
myMap.put("myKey4", "myVal4");
|
myMap.put("myKey4", "myVal4");
|
||||||
jsonObject.put("objectKey", myMap);
|
jsonObject.put("objectKey", myMap);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON
|
||||||
assertTrue("equal jsonObjects should be similar",
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
jsonObject.similar(expectedJsonObject));
|
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
||||||
|
assertTrue("expected 3 arrayKey items", ((List<?>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
|
||||||
|
assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]")));
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]")));
|
||||||
|
assertTrue("expected 4 objectKey items", ((Map<?,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
|
||||||
|
assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1")));
|
||||||
|
assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2")));
|
||||||
|
assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3")));
|
||||||
|
assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4")));
|
||||||
|
|
||||||
jsonObject.remove("trueKey");
|
jsonObject.remove("trueKey");
|
||||||
JSONObject expectedJsonObjectAfterRemoval =
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
new JSONObject(expectedStrAfterRemoval);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,
|
|
||||||
expectedJsonObjectAfterRemoval);
|
|
||||||
assertTrue("unequal jsonObjects should not be similar",
|
assertTrue("unequal jsonObjects should not be similar",
|
||||||
!jsonObject.similar(expectedJsonObject));
|
!jsonObject.similar(expectedJsonObject));
|
||||||
assertTrue("unequal Objects should not be similar",
|
assertTrue("jsonObject should not be similar to jsonArray",
|
||||||
!jsonObject.similar(new JSONArray()));
|
!jsonObject.similar(new JSONArray()));
|
||||||
|
|
||||||
String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}";
|
String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}";
|
||||||
|
@ -1349,9 +1358,21 @@ public class JSONObjectTest {
|
||||||
"}"+
|
"}"+
|
||||||
"}";
|
"}";
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
String toStr = jsonObject.toString();
|
|
||||||
JSONObject expectedJsonObject = new JSONObject(toStr);
|
// validate JSON
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
||||||
|
assertTrue("expected 3 arrayKey items", ((List<?>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
|
||||||
|
assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]")));
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]")));
|
||||||
|
assertTrue("expected 4 objectKey items", ((Map<?,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
|
||||||
|
assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1")));
|
||||||
|
assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2")));
|
||||||
|
assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3")));
|
||||||
|
assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1367,25 +1388,12 @@ public class JSONObjectTest {
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("abc", "def");
|
map.put("abc", "def");
|
||||||
jsonObject.put("key", map);
|
jsonObject.put("key", map);
|
||||||
String toStr = jsonObject.toString();
|
|
||||||
JSONObject expectedJsonObject = new JSONObject(toStr);
|
// validate JSON
|
||||||
assertTrue("keys should be equal",
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
jsonObject.keySet().iterator().next().equals(
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
expectedJsonObject.keySet().iterator().next()));
|
assertTrue("expected 1 key item", ((Map<?,?>)(JsonPath.read(doc, "$.key"))).size() == 1);
|
||||||
/**
|
assertTrue("expected def", "def".equals(JsonPath.read(doc, "$.key.abc")));
|
||||||
* Can't do a Util compare because although they look the same
|
|
||||||
* in the debugger, one is a map and the other is a JSONObject.
|
|
||||||
* TODO: write a util method for such comparisons
|
|
||||||
*/
|
|
||||||
assertTrue("Maps should be entered as JSONObject", jsonObject.get("key") instanceof JSONObject);
|
|
||||||
JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key");
|
|
||||||
assertTrue("value size should be equal",
|
|
||||||
map.size() == mapJsonObject.length() && map.size() == 1);
|
|
||||||
assertTrue("keys should be equal for key: "+map.keySet().iterator().next(),
|
|
||||||
mapJsonObject.keys().next().equals(map.keySet().iterator().next()));
|
|
||||||
assertTrue("values should be equal for key: "+map.keySet().iterator().next(),
|
|
||||||
mapJsonObject.get(mapJsonObject.keys().next()).toString().equals(
|
|
||||||
map.get(map.keySet().iterator().next())));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1402,25 +1410,12 @@ public class JSONObjectTest {
|
||||||
collection.add("abc");
|
collection.add("abc");
|
||||||
// ArrayList will be added as an object
|
// ArrayList will be added as an object
|
||||||
jsonObject.put("key", collection);
|
jsonObject.put("key", collection);
|
||||||
String toStr = jsonObject.toString();
|
|
||||||
// [abc] will be added as a JSONArray
|
// validate JSON
|
||||||
JSONObject expectedJsonObject = new JSONObject(toStr);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
/**
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
* Can't do a Util compare because although they look the same in the
|
assertTrue("expected 1 key item", ((List<?>)(JsonPath.read(doc, "$.key"))).size() == 1);
|
||||||
* debugger, one is a collection and the other is a JSONArray.
|
assertTrue("expected abc", "abc".equals(JsonPath.read(doc, "$.key[0]")));
|
||||||
*/
|
|
||||||
assertTrue("keys should be equal", jsonObject.keySet().iterator()
|
|
||||||
.next().equals(expectedJsonObject.keySet().iterator().next()));
|
|
||||||
assertTrue("Collections should be converted to JSONArray",
|
|
||||||
jsonObject.get("key") instanceof JSONArray);
|
|
||||||
JSONArray jsonArray = expectedJsonObject.getJSONArray("key");
|
|
||||||
assertTrue("value size should be equal",
|
|
||||||
collection.size() == jsonArray.length());
|
|
||||||
Iterator<String> it = collection.iterator();
|
|
||||||
for (int i = 0; i < collection.size(); ++i) {
|
|
||||||
assertTrue("items should be equal for index: " + i, jsonArray
|
|
||||||
.get(i).toString().equals(it.next().toString()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1478,16 +1473,14 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void valueToStringConfirmException() {
|
public void valueToStringConfirmException() {
|
||||||
String expectedStr = "{\"1\":\"myValue\"}";
|
|
||||||
Map<Integer, String> myMap = new HashMap<Integer, String>();
|
Map<Integer, String> myMap = new HashMap<Integer, String>();
|
||||||
myMap.put(1, "myValue");
|
myMap.put(1, "myValue");
|
||||||
// this is the test, it should not throw an exception
|
// this is the test, it should not throw an exception
|
||||||
String str = JSONObject.valueToString(myMap);
|
String str = JSONObject.valueToString(myMap);
|
||||||
// confirm result, just in case
|
// confirm result, just in case
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,
|
assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1")));
|
||||||
expectedJsonObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1533,28 +1526,46 @@ public class JSONObjectTest {
|
||||||
collection.add(new Integer(1));
|
collection.add(new Integer(1));
|
||||||
collection.add(new Integer(2));
|
collection.add(new Integer(2));
|
||||||
collection.add(new Integer(3));
|
collection.add(new Integer(3));
|
||||||
JSONArray jsonArray = (JSONArray)(JSONObject.wrap(collection));
|
JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection));
|
||||||
String expectedCollectionJsonArrayStr =
|
|
||||||
"[1,2,3]";
|
// validate JSON
|
||||||
JSONArray expectedCollectionJsonArray =
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
new JSONArray(expectedCollectionJsonArrayStr);
|
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray,
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
||||||
expectedCollectionJsonArray);
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
||||||
|
|
||||||
// wrap Array returns JSONArray
|
// wrap Array returns JSONArray
|
||||||
Integer[] array = { new Integer(1), new Integer(2), new Integer(3) };
|
Integer[] array = { new Integer(1), new Integer(2), new Integer(3) };
|
||||||
JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array));
|
JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array));
|
||||||
JSONArray expectedIntegerArrayJsonArray = new JSONArray("[1,2,3]");
|
|
||||||
Util.compareActualVsExpectedJsonArrays(integerArrayJsonArray,
|
// validate JSON
|
||||||
expectedIntegerArrayJsonArray);
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
||||||
|
|
||||||
|
// validate JSON
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString());
|
||||||
|
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
||||||
|
|
||||||
// wrap map returns JSONObject
|
// wrap map returns JSONObject
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
map.put("key1", "val1");
|
map.put("key1", "val1");
|
||||||
map.put("key2", "val2");
|
map.put("key2", "val2");
|
||||||
map.put("key3", "val3");
|
map.put("key3", "val3");
|
||||||
JSONObject mapJsonObject = (JSONObject)(JSONObject.wrap(map));
|
JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map));
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, mapJsonObject);
|
|
||||||
|
// validate JSON
|
||||||
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString());
|
||||||
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
|
assertTrue("expected val1", "val1".equals(JsonPath.read(doc, "$.key1")));
|
||||||
|
assertTrue("expected val2", "val2".equals(JsonPath.read(doc, "$.key2")));
|
||||||
|
assertTrue("expected val3", "val3".equals(JsonPath.read(doc, "$.key3")));
|
||||||
|
|
||||||
// TODO test wrap(package)
|
// TODO test wrap(package)
|
||||||
}
|
}
|
||||||
|
@ -1720,13 +1731,14 @@ public class JSONObjectTest {
|
||||||
// put null should remove the item.
|
// put null should remove the item.
|
||||||
String str = "{\"myKey\": \"myval\"}";
|
String str = "{\"myKey\": \"myval\"}";
|
||||||
JSONObject jsonObjectRemove = new JSONObject(str);
|
JSONObject jsonObjectRemove = new JSONObject(str);
|
||||||
JSONObject jsonObjectPutNull = new JSONObject(str);
|
|
||||||
jsonObjectRemove.remove("myKey");
|
jsonObjectRemove.remove("myKey");
|
||||||
jsonObjectPutNull.put("myKey", (Object)null);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObjectRemove, jsonObjectPutNull);
|
JSONObject jsonObjectPutNull = new JSONObject(str);
|
||||||
assertTrue("jsonObject should be empty",
|
jsonObjectPutNull.put("myKey", (Object) null);
|
||||||
jsonObjectRemove.length() == 0 &&
|
|
||||||
jsonObjectPutNull.length() == 0);
|
// validate JSON
|
||||||
|
assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0
|
||||||
|
&& jsonObjectPutNull.length() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1878,7 +1890,7 @@ public class JSONObjectTest {
|
||||||
obj = null;
|
obj = null;
|
||||||
jsonObjectNull.put("key", obj);
|
jsonObjectNull.put("key", obj);
|
||||||
value = jsonObjectNull.opt("key");
|
value = jsonObjectNull.opt("key");
|
||||||
assertTrue("opt() null should find null", value == null);;
|
assertTrue("opt() null should find null", value == null);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
value = "";
|
value = "";
|
||||||
}
|
}
|
||||||
|
@ -1904,5 +1916,3 @@ public class JSONObjectTest {
|
||||||
assertTrue("null should emit an empty string", "".equals(sNull));
|
assertTrue("null should emit an empty string", "".equals(sNull));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,13 @@ package org.json.junit;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import org.json.*;
|
import org.json.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for JSON-Java JSONStringer.
|
* Tests for JSON-Java JSONStringer.
|
||||||
|
@ -175,16 +179,6 @@ public class JSONStringerTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void simpleObjectString() {
|
public void simpleObjectString() {
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"trueValue\":true,"+
|
|
||||||
"\"falseValue\":false,"+
|
|
||||||
"\"nullValue\":null,"+
|
|
||||||
"\"stringValue\":\"hello world!\","+
|
|
||||||
"\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+
|
|
||||||
"\"intValue\":42,"+
|
|
||||||
"\"doubleValue\":-23.45e67"+
|
|
||||||
"}";
|
|
||||||
JSONStringer jsonStringer = new JSONStringer();
|
JSONStringer jsonStringer = new JSONStringer();
|
||||||
jsonStringer.object();
|
jsonStringer.object();
|
||||||
jsonStringer.key("trueValue").value(true);
|
jsonStringer.key("trueValue").value(true);
|
||||||
|
@ -197,8 +191,17 @@ public class JSONStringerTest {
|
||||||
jsonStringer.endObject();
|
jsonStringer.endObject();
|
||||||
String str = jsonStringer.toString();
|
String str = jsonStringer.toString();
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON content
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 7 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 7);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue")));
|
||||||
|
assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue"));
|
||||||
|
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue")));
|
||||||
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue")));
|
||||||
|
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -207,15 +210,6 @@ public class JSONStringerTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void simpleArrayString() {
|
public void simpleArrayString() {
|
||||||
String expectedStr =
|
|
||||||
"["+
|
|
||||||
"true,"+
|
|
||||||
"false,"+
|
|
||||||
"null,"+
|
|
||||||
"\"hello world!\","+
|
|
||||||
"42,"+
|
|
||||||
"-23.45e67"+
|
|
||||||
"]";
|
|
||||||
JSONStringer jsonStringer = new JSONStringer();
|
JSONStringer jsonStringer = new JSONStringer();
|
||||||
jsonStringer.array();
|
jsonStringer.array();
|
||||||
jsonStringer.value(true);
|
jsonStringer.value(true);
|
||||||
|
@ -227,8 +221,16 @@ public class JSONStringerTest {
|
||||||
jsonStringer.endArray();
|
jsonStringer.endArray();
|
||||||
String str = jsonStringer.toString();
|
String str = jsonStringer.toString();
|
||||||
JSONArray jsonArray = new JSONArray(str);
|
JSONArray jsonArray = new JSONArray(str);
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
// validate JSON content
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
|
assertTrue("expected 6 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected null", null == JsonPath.read(doc, "$[2]"));
|
||||||
|
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$[5]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -237,38 +239,6 @@ public class JSONStringerTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void complexObjectString() {
|
public void complexObjectString() {
|
||||||
String expectedStr =
|
|
||||||
"{"+
|
|
||||||
"\"trueValue\":true,"+
|
|
||||||
"\"falseValue\":false,"+
|
|
||||||
"\"nullValue\":null,"+
|
|
||||||
"\"stringValue\":\"hello world!\","+
|
|
||||||
"\"object2\":{"+
|
|
||||||
"\"k1\":\"v1\","+
|
|
||||||
"\"k2\":\"v2\","+
|
|
||||||
"\"k3\":\"v3\","+
|
|
||||||
"\"array1\":["+
|
|
||||||
"1,"+
|
|
||||||
"2,"+
|
|
||||||
"{"+
|
|
||||||
"\"k4\":\"v4\","+
|
|
||||||
"\"k5\":\"v5\","+
|
|
||||||
"\"k6\":\"v6\","+
|
|
||||||
"\"array2\":["+
|
|
||||||
"5,"+
|
|
||||||
"6,"+
|
|
||||||
"7,"+
|
|
||||||
"8"+
|
|
||||||
"]"+
|
|
||||||
"},"+
|
|
||||||
"3,"+
|
|
||||||
"4"+
|
|
||||||
"]"+
|
|
||||||
"},"+
|
|
||||||
"\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+
|
|
||||||
"\"intValue\":42,"+
|
|
||||||
"\"doubleValue\":-23.45e67"+
|
|
||||||
"}";
|
|
||||||
JSONStringer jsonStringer = new JSONStringer();
|
JSONStringer jsonStringer = new JSONStringer();
|
||||||
jsonStringer.object();
|
jsonStringer.object();
|
||||||
jsonStringer.key("trueValue").value(true);
|
jsonStringer.key("trueValue").value(true);
|
||||||
|
@ -303,8 +273,35 @@ public class JSONStringerTest {
|
||||||
jsonStringer.endObject();
|
jsonStringer.endObject();
|
||||||
String str = jsonStringer.toString();
|
String str = jsonStringer.toString();
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
// validate JSON content
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
|
assertTrue("expected 8 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 8);
|
||||||
|
assertTrue("expected 4 object2 items", ((Map<?,?>)(JsonPath.read(doc, "$.object2"))).size() == 4);
|
||||||
|
assertTrue("expected 5 array1 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1"))).size() == 5);
|
||||||
|
assertTrue("expected 4 array[2] items", ((Map<?,?>)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4);
|
||||||
|
assertTrue("expected 4 array1[2].array2 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue")));
|
||||||
|
assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue"));
|
||||||
|
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue")));
|
||||||
|
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue")));
|
||||||
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue")));
|
||||||
|
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$.object2.k1")));
|
||||||
|
assertTrue("expected v2", "v2".equals(JsonPath.read(doc, "$.object2.k2")));
|
||||||
|
assertTrue("expected v3", "v3".equals(JsonPath.read(doc, "$.object2.k3")));
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.object2.array1[0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.object2.array1[1]")));
|
||||||
|
assertTrue("expected v4", "v4".equals(JsonPath.read(doc, "$.object2.array1[2].k4")));
|
||||||
|
assertTrue("expected v5", "v5".equals(JsonPath.read(doc, "$.object2.array1[2].k5")));
|
||||||
|
assertTrue("expected v6", "v6".equals(JsonPath.read(doc, "$.object2.array1[2].k6")));
|
||||||
|
assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$.object2.array1[2].array2[0]")));
|
||||||
|
assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$.object2.array1[2].array2[1]")));
|
||||||
|
assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$.object2.array1[2].array2[2]")));
|
||||||
|
assertTrue("expected 8", Integer.valueOf(8).equals(JsonPath.read(doc, "$.object2.array1[2].array2[3]")));
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.object2.array1[3]")));
|
||||||
|
assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$.object2.array1[4]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue