Search in sources :

Example 26 with JSONStringer

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"));
}
Also used : JSONObject(org.json.JSONObject) JSONStringer(org.json.JSONStringer) Test(org.junit.Test)

Example 27 with JSONStringer

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());
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONStringer(org.json.JSONStringer) Test(org.junit.Test)

Example 28 with JSONStringer

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"));
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONStringer(org.json.JSONStringer) Test(org.junit.Test)

Example 29 with JSONStringer

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());
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONStringer(org.json.JSONStringer) Test(org.junit.Test)

Example 30 with JSONStringer

use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.

the class DefaultLogSerializer method serializeContainer.

@NonNull
@Override
public String serializeContainer(@NonNull LogContainer logContainer) throws JSONException {
    /* Init JSON serializer, in verbose: try to make it pretty. */
    JSONStringer writer = null;
    if (AppCenterLog.getLogLevel() <= android.util.Log.VERBOSE) {
        try {
            Constructor<JSONStringer> constructor = JSONStringer.class.getDeclaredConstructor(int.class);
            constructor.setAccessible(true);
            writer = constructor.newInstance(2);
        } catch (Exception e) {
            AppCenterLog.error(AppCenter.LOG_TAG, "Failed to setup pretty json, falling back to default one", e);
        }
    }
    if (writer == null) {
        writer = new JSONStringer();
    }
    /* Start writing JSON. */
    writer.object();
    writer.key(LOGS).array();
    for (Log log : logContainer.getLogs()) {
        writeLog(writer, log);
    }
    writer.endArray();
    writer.endObject();
    return writer.toString();
}
Also used : AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) JSONStringer(org.json.JSONStringer) JSONException(org.json.JSONException) NonNull(android.support.annotation.NonNull)

Aggregations

JSONStringer (org.json.JSONStringer)34 Test (org.junit.Test)16 JSONObject (org.json.JSONObject)12 JSONException (org.json.JSONException)7 ArrayList (java.util.ArrayList)4 NonNull (android.support.annotation.NonNull)2 IOException (java.io.IOException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HttpResponse (org.apache.http.HttpResponse)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2 InSequence (org.jboss.arquillian.junit.InSequence)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Log (com.microsoft.appcenter.ingestion.models.Log)1 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)1 Log (com.microsoft.azure.mobile.ingestion.models.Log)1 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1