Search in sources :

Example 1 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 (MobileCenterLog.getLogLevel() <= android.util.Log.VERBOSE) {
        try {
            Constructor<JSONStringer> constructor = JSONStringer.class.getDeclaredConstructor(int.class);
            constructor.setAccessible(true);
            writer = constructor.newInstance(2);
        } catch (Exception e) {
            MobileCenterLog.error(MobileCenter.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 : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) JSONStringer(org.json.JSONStringer) JSONException(org.json.JSONException) NonNull(android.support.annotation.NonNull)

Example 2 with JSONStringer

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

the class ErrorModelTest method deserializeInvalidBase64forErrorAttachment.

@Test
public void deserializeInvalidBase64forErrorAttachment() throws JSONException {
    ErrorAttachmentLog log = new ErrorAttachmentLog();
    log.setId(UUID.randomUUID());
    log.setErrorId(UUID.randomUUID());
    log.setData(new byte[0]);
    log.setContentType("text/plain");
    JSONStringer jsonWriter = new JSONStringer();
    jsonWriter.object();
    log.write(jsonWriter);
    jsonWriter.endObject();
    JSONObject json = new JSONObject(jsonWriter.toString());
    json.put(DATA, "a");
    try {
        new ErrorAttachmentLog().read(json);
        Assert.fail("Expected json exception here");
    } catch (JSONException e) {
        assertEquals("bad base-64", e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONStringer(org.json.JSONStringer) Test(org.junit.Test)

Example 3 with JSONStringer

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

the class AbstractLogTest method writeNullDeviceTest.

@Test
public void writeNullDeviceTest() throws JSONException {
    JSONStringer mockJsonStringer = mock(JSONStringer.class);
    when(mockJsonStringer.key(anyString())).thenReturn(mockJsonStringer);
    when(mockJsonStringer.value(anyString())).thenReturn(mockJsonStringer);
    AbstractLog mockLog = new MockLog();
    mockLog.write(mockJsonStringer);
    verify(mockJsonStringer, never()).key(AbstractLog.DEVICE);
}
Also used : JSONStringer(org.json.JSONStringer) Test(org.junit.Test)

Example 4 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 5 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)

Aggregations

JSONStringer (org.json.JSONStringer)24 JSONObject (org.json.JSONObject)7 Test (org.junit.Test)7 JSONException (org.json.JSONException)5 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 NonNull (android.support.annotation.NonNull)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 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1