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.setTimestamp(new Date());
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());
}
}
use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.
the class DefaultLogSerializerTest method failToUsePrettyJson.
@Test
public void failToUsePrettyJson() throws Exception {
/* Mock logs to verify interactions. */
mockStatic(MobileCenterLog.class);
when(MobileCenterLog.getLogLevel()).thenReturn(Log.VERBOSE);
/* Mock stub JSONStringer. */
JSONStringer stringer = mock(JSONStringer.class);
whenNew(JSONStringer.class).withAnyArguments().thenReturn(stringer);
when(stringer.key(anyString())).thenReturn(stringer);
when(stringer.toString()).thenReturn("{}");
/*
* We are in test folder so the stub does not contain the private methods,
* this test thus simulates what happens if we can't access the private methods
* via reflection to use pretty json. We check it serializes gracefully.
*/
assertEquals("{}", new DefaultLogSerializer().serializeContainer(mock(LogContainer.class)));
/* And that it logs why the pretty json could not be used. */
verifyStatic();
MobileCenterLog.error(eq(MobileCenter.LOG_TAG), anyString(), any(NoSuchMethodError.class));
}
use of org.json.JSONStringer in project vidtry by commonsguy.
the class URLHistory method save.
void save(Writer out) throws JSONException, IOException {
JSONStringer json = new JSONStringer().object();
for (HistoryItem h : spareCopy) {
h.emit(json);
}
out.write(json.endObject().toString());
}
use of org.json.JSONStringer in project Parse-SDK-Android by ParsePlatform.
the class ParseRESTCommand method toDeterministicString.
// Encodes the object to JSON, but ensures that JSONObjects
// and nested JSONObjects are encoded with keys in alphabetical order.
/** package */
static String toDeterministicString(Object o) throws JSONException {
JSONStringer stringer = new JSONStringer();
addToStringer(stringer, o);
return stringer.toString();
}
use of org.json.JSONStringer in project quorrabot by GloriousEggroll.
the class GameWispAPI method readJsonFromUrl.
@SuppressWarnings("UseSpecificCatch")
private static JSONObject readJsonFromUrl(String methodType, String urlAddress) {
JSONObject jsonResult = new JSONObject("{}");
InputStream inputStream = null;
OutputStream outputStream = null;
URL urlRaw;
HttpsURLConnection urlConn;
String jsonText = "";
if (sAccessToken.length() == 0) {
if (!noAccessWarning) {
com.gmt2001.Console.err.println("GameWispAPI: Attempting to use GameWisp API without key. Disable GameWisp module.");
noAccessWarning = true;
}
JSONStringer jsonObject = new JSONStringer();
return (new JSONObject(jsonObject.object().key("result").object().key("status").value(-1).endObject().endObject().toString()));
}
try {
urlRaw = new URL(urlAddress);
urlConn = (HttpsURLConnection) urlRaw.openConnection();
urlConn.setDoInput(true);
urlConn.setRequestMethod(methodType);
urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 QuorraBot/2015");
if (methodType.equals("POST")) {
urlConn.setDoOutput(true);
urlConn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
} else {
urlConn.addRequestProperty("Content-Type", "application/json");
}
urlConn.connect();
if (urlConn.getResponseCode() == 200) {
inputStream = urlConn.getInputStream();
} else {
inputStream = urlConn.getErrorStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
jsonText = readAll(rd);
jsonResult = new JSONObject(jsonText);
fillJSONObject(jsonResult, true, methodType, urlAddress, urlConn.getResponseCode(), "", "", jsonText);
} catch (JSONException ex) {
fillJSONObject(jsonResult, false, methodType, urlAddress, 0, "JSONException", ex.getMessage(), jsonText);
com.gmt2001.Console.err.println("GameWispAPI::Bad JSON (" + urlAddress + "): " + jsonText.substring(0, 100) + "...");
} catch (NullPointerException ex) {
fillJSONObject(jsonResult, false, methodType, urlAddress, 0, "NullPointerException", ex.getMessage(), "");
com.gmt2001.Console.err.println("GameWispAPI::readJsonFromUrl::Exception: " + ex.getMessage());
} catch (MalformedURLException ex) {
fillJSONObject(jsonResult, false, methodType, urlAddress, 0, "MalformedURLException", ex.getMessage(), "");
com.gmt2001.Console.err.println("GameWispAPI::readJsonFromUrl::Exception: " + ex.getMessage());
} catch (SocketTimeoutException ex) {
fillJSONObject(jsonResult, false, methodType, urlAddress, 0, "SocketTimeoutException", ex.getMessage(), "");
com.gmt2001.Console.err.println("GameWispAPI::readJsonFromUrl::Exception: " + ex.getMessage());
} catch (IOException ex) {
fillJSONObject(jsonResult, false, methodType, urlAddress, 0, "IOException", ex.getMessage(), "");
com.gmt2001.Console.err.println("GameWispAPI::readJsonFromUrl::Exception: " + ex.getMessage());
} catch (Exception ex) {
fillJSONObject(jsonResult, false, methodType, urlAddress, 0, "Exception", ex.getMessage(), "");
com.gmt2001.Console.err.println("GameWispAPI::readJsonFromUrl::Exception: " + ex.getMessage());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
fillJSONObject(jsonResult, false, methodType, urlAddress, 0, "IOException", ex.getMessage(), "");
com.gmt2001.Console.err.println("GameWispAPI::readJsonFromUrl::Exception: " + ex.getMessage());
}
}
}
return (jsonResult);
}
Aggregations