1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00
JSON-java/JSONObjectTest.java
2015-04-06 19:01:54 -05:00

161 lines
5.7 KiB
Java

package org.json.junit;
import java.util.*;
import org.json.*;
import org.junit.*;
public class JSONObjectTest {
@Test
public void jsonObjectByNames() {
String str =
"{"+
"\"trueKey\":true,"+
"\"falseKey\":false,"+
"\"nullKey\":null,"+
"\"stringKey\":\"hello world!\","+
"\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+
"\"intKey\":42,"+
"\"doubleKey\":-23.45e67"+
"}";
String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"};
String expectedStr =
"{"+
"\"falseKey\":false,"+
"\"nullKey\":null,"+
"\"stringKey\":\"hello world!\","+
"\"doubleKey\":-23.45e67"+
"}";
JSONObject jsonObject = new JSONObject(str);
JSONObject copyJsonObject = new JSONObject(jsonObject, keys);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(copyJsonObject, expectedJsonObject);
}
@Test
public void jsonObjectByMap() {
String expectedStr =
"{"+
"\"trueKey\":true,"+
"\"falseKey\":false,"+
"\"stringKey\":\"hello world!\","+
"\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+
"\"intKey\":42,"+
"\"doubleKey\":-23.45e67"+
"}";
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("complexStringKey", "h\be\tllo w\u1234orld!");
jsonMap.put("intKey", new Long(42));
jsonMap.put("doubleKey", new Double(-23.45e67));
JSONObject jsonObject = new JSONObject(jsonMap);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
@Test
public void jsonObjectByBean() {
String expectedStr =
"{"+
"\"trueKey\":true,"+
"\"falseKey\":false,"+
"\"stringKey\":\"hello world!\","+
"\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+
"\"intKey\":42,"+
"\"doubleKey\":-23.45e7"+
"}";
MyBean myBean = new MyBean();
JSONObject jsonObject = new JSONObject(myBean);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
@Test
public void jsonObjectByBeanAndNames() {
String expectedStr =
"{"+
"\"trueKey\":true,"+
"\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+
"\"doubleKey\":-23.45e7"+
"}";
String[] keys = {"trueKey", "complexStringKey", "doubleKey"};
MyBean myBean = new MyBean();
JSONObject jsonObject = new JSONObject(myBean, keys);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
@Test
public void jsonObjectByResourceBundle() {
String expectedStr =
"{"+
"\"greetings\": {"+
"\"hello\":\"Hello, \","+
"\"world\":\"World!\""+
"},"+
"\"farewells\": {"+
"\"later\":\"Later, \","+
"\"gator\":\"Alligator!\""+
"}"+
"}";
JSONObject jsonObject = new
JSONObject("org.json.junit.StringsResourceBundle",
Locale.getDefault());
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
@Test
public void jsonObjectAccumulate() {
String expectedStr =
"{"+
"\"myArray\": ["+
"true,"+
"false,"+
"\"hello world!\","+
"\"h\be\tllo w\u1234orld!\","+
"42,"+
"-23.45e7"+
"]"+
"}";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("myArray", true);
jsonObject.accumulate("myArray", false);
jsonObject.accumulate("myArray", "hello world!");
jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!");
jsonObject.accumulate("myArray", 42);
jsonObject.accumulate("myArray", -23.45e7);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
@Test
public void jsonObjectAppend() {
String expectedStr =
"{"+
"\"myArray\": ["+
"true,"+
"false,"+
"\"hello world!\","+
"\"h\be\tllo w\u1234orld!\","+
"42,"+
"-23.45e7"+
"]"+
"}";
JSONObject jsonObject = new JSONObject();
jsonObject.append("myArray", true);
jsonObject.append("myArray", false);
jsonObject.append("myArray", "hello world!");
jsonObject.append("myArray", "h\be\tllo w\u1234orld!");
jsonObject.append("myArray", 42);
jsonObject.append("myArray", -23.45e7);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
}
}