@Override public void read(JSONObject object) throws JSONException { setLibVer(object.optString(LIB_VER, null)); setEpoch(object.optString(EPOCH, null)); setSeq(JSONUtils.readLong(object, SEQ)); if (object.has(INSTALL_ID)) { setInstallId(UUID.fromString(object.getString(INSTALL_ID))); } }
@Override public void read(JSONObject object) throws JSONException { super.read(object); setId(UUID.fromString(object.getString(ID))); setProcessId(JSONUtils.readInteger(object, PROCESS_ID)); setProcessName(object.optString(PROCESS_NAME, null)); setParentProcessId(JSONUtils.readInteger(object, PARENT_PROCESS_ID)); setParentProcessName(object.optString(PARENT_PROCESS_NAME, null)); setErrorThreadId(JSONUtils.readLong(object, ERROR_THREAD_ID)); setErrorThreadName(object.optString(ERROR_THREAD_NAME, null)); setFatal(JSONUtils.readBoolean(object, FATAL)); setAppLaunchTimestamp(JSONDateUtils.toDate(object.getString(APP_LAUNCH_TIMESTAMP))); setArchitecture(object.optString(ARCHITECTURE, null)); }
@Override public void read(JSONObject object) throws JSONException { /* Override abstract log JSON since it's Common Schema and not App Center schema. */ /* Read top level PART A simple fields. */ setVer(object.getString(VER)); setName(object.getString(NAME)); setTimestamp(JSONDateUtils.toDate(object.getString(TIME))); if (object.has(POP_SAMPLE)) { setPopSample(object.getDouble(POP_SAMPLE)); } setIKey(object.optString(IKEY, null)); setFlags(JSONUtils.readLong(object, FLAGS)); setCV(object.optString(CV, null)); /* Read extensions. */ if (object.has(EXT)) { Extensions extensions = new Extensions(); extensions.read(object.getJSONObject(EXT)); setExt(extensions); } /* Read Parts B&C. */ if (object.has(DATA)) { Data data = new Data(); data.read(object.getJSONObject(DATA)); setData(data); } }
@Test public void readKeyNotExists() throws JSONException { /* Create an empty JSON object. */ JSONObject object = new JSONObject("{}"); /* Verify. */ assertNull(JSONUtils.readInteger(object, "key")); assertNull(JSONUtils.readLong(object, "key")); assertNull(JSONUtils.readBoolean(object, "key")); assertNull(JSONUtils.readMap(object, "key")); assertNull(JSONUtils.readArray(object, "key", new MockLogFactory())); }
@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")); }