use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.
the class JSONUtilsAndroidTest method writeReadObject.
@Test
public void writeReadObject() throws JSONException {
/* Write to JSON object. */
JSONStringer writer = new JSONStringer();
writer.object();
JSONUtils.write(writer, "int", 1);
JSONUtils.write(writer, "long", 1000000000L);
JSONUtils.write(writer, "boolean", true);
writer.endObject();
/* Convert to string. */
String json = writer.toString();
assertNotNull(json);
/* Read a JSON object and verify. */
JSONObject object = new JSONObject(json);
assertEquals(Integer.valueOf(1), JSONUtils.readInteger(object, "int"));
assertEquals(Long.valueOf(1000000000L), JSONUtils.readLong(object, "long"));
assertEquals(true, JSONUtils.readBoolean(object, "boolean"));
}
use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.
the class JSONUtilsAndroidTest method writeReadArray.
@Test
public void writeReadArray() throws JSONException {
/* Generate mock logs. */
MockLog firstLog = AndroidTestUtils.generateMockLog();
MockLog secondLog = AndroidTestUtils.generateMockLog();
/* Create a test list. */
final List<MockLog> list = new ArrayList<>();
list.add(firstLog);
list.add(secondLog);
/* Write to JSON object. */
JSONStringer writer = new JSONStringer();
writer.object();
JSONUtils.writeArray(writer, "list", list);
writer.endObject();
/* Convert to string. */
String json = writer.toString();
assertNotNull(json);
/* Read a JSON object and verify. */
JSONObject object = new JSONObject(json);
assertEquals(list, JSONUtils.readArray(object, "list", new MockLogFactory()));
/* Test null value. */
writer = new JSONStringer();
JSONUtils.writeArray(writer, "null", null);
assertNull(writer.toString());
}
use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.
the class JSONUtilsAndroidTest method writeReadStringArray.
@Test
public void writeReadStringArray() throws JSONException {
/* Create a test list. */
final List<String> list = new ArrayList<>();
list.add("FIRST");
list.add("SECOND");
/* Write to JSON object. */
JSONStringer writer = new JSONStringer();
writer.object();
JSONUtils.writeStringArray(writer, "list", list);
writer.endObject();
/* Convert to string. */
String json = writer.toString();
assertNotNull(json);
/* Read a JSON object and verify. */
JSONObject object = new JSONObject(json);
assertEquals(list, JSONUtils.readStringArray(object, "list"));
assertNull(JSONUtils.readStringArray(object, "missing"));
/* Test null value. */
writer = new JSONStringer();
JSONUtils.writeStringArray(writer, "null", null);
assertNull(writer.toString());
}
use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.
the class JSONUtilsAndroidTest method writeReadMap.
@Test
public void writeReadMap() throws JSONException {
/* Create a test map. */
final Map<String, String> map = new HashMap<>();
map.put("key", "value");
/* Write to JSON object. */
JSONStringer writer = new JSONStringer();
writer.object();
JSONUtils.writeMap(writer, "map", map);
writer.endObject();
/* Convert to string. */
String json = writer.toString();
assertNotNull(json);
/* Read a JSON object and verify. */
JSONObject object = new JSONObject(json);
assertEquals(map, JSONUtils.readMap(object, "map"));
}
use of org.json.JSONStringer in project quorrabot by GloriousEggroll.
the class SoundBoard method doVersion.
private void doVersion(WebSocket webSocket, String id) {
JSONStringer jsonObject = new JSONStringer();
String version = "";
try {
version = Quorrabot.instance().getBotInfo();
} catch (NullPointerException ex) {
if (!dbCallNull) {
dbCallNull = true;
debugMsg("NULL returned from DB. DB Object not created yet.");
}
return;
}
jsonObject.object().key("versionresult").value(id).key("version").value(version).endObject();
webSocket.send(jsonObject.toString());
}
Aggregations