/** * Writes one entry to the local id map on disk. */ private synchronized void putMapEntry(String localId, MapEntry entry) { if (!isLocalId(localId)) { throw new IllegalStateException("Tried to get invalid local id: \"" + localId + "\"."); } JSONObject json = new JSONObject(); try { json.put("retainCount", entry.retainCount); if (entry.objectId != null) { json.put("objectId", entry.objectId); } } catch (JSONException je) { throw new IllegalStateException("Error creating local id map entry.", je); } File file = new File(diskPath, localId); if (!diskPath.exists()) { diskPath.mkdirs(); } try { ParseFileUtils.writeJSONObjectToFile(file, json); } catch (IOException e) { //TODO (grantland): We should do something if this fails... } }
private synchronized void saveStateToDisk() { try { ParseFileUtils.writeJSONObjectToFile(diskState, toJSON()); } catch (IOException | JSONException e) { PLog.e(TAG, "Unexpected error when serializing push state to " + diskState, e); } }
/** * Saves the {@code ParseConfig} to the a file on disk as JSON. * * @param config The ParseConfig which needs to be saved. */ /* package for test */ void saveToDisk(ParseConfig config) { JSONObject object = new JSONObject(); try { JSONObject jsonParams = (JSONObject) NoObjectsEncoder.get().encode(config.getParams()); object.put("params", jsonParams); } catch (JSONException e) { throw new RuntimeException("could not serialize config to JSON"); } try { ParseFileUtils.writeJSONObjectToFile(currentConfigFile, object); } catch (IOException e) { //TODO (grantland): We should do something if this fails... } } }
/** * Saves the {@code ParseObject} to the a file on disk as JSON in /2/ format. * * @param coder Current coder to encode the ParseObject. * @param current ParseObject which needs to be saved to disk. * @param file The file to save the object to. * @see #getFromDisk(ParseObjectCurrentCoder, File, ParseObject.State.Init) */ private static void saveToDisk( ParseObjectCurrentCoder coder, ParseObject current, File file) { JSONObject json = coder.encode(current.getState(), null, PointerEncoder.get()); try { ParseFileUtils.writeJSONObjectToFile(file, json); } catch (IOException e) { //TODO(grantland): We should do something if this fails... } }
@Test public void testWriteJSONObjectToFile() throws Exception { File file = temporaryFolder.newFile("file.txt"); ParseFileUtils.writeJSONObjectToFile(file, new JSONObject(TEST_JSON)); InputStream in = null; String content; try { in = new FileInputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream(); ParseIOUtils.copy(in, out); content = new String(out.toByteArray()); } finally { ParseIOUtils.closeQuietly(in); } JSONObject json = new JSONObject(content); assertNotNull(json); assertEquals("bar", json.getString("foo")); } }
@Test public void testGetAsync() throws Exception { File file = new File(temporaryFolder.getRoot(), "test"); JSONObject json = new JSONObject(); ParseFileUtils.writeJSONObjectToFile(file, json); ParseUser.State.Builder builder = new ParseUser.State.Builder(); builder.put("foo", "bar"); ParseUserCurrentCoder coder = mock(ParseUserCurrentCoder.class); when(coder.decode(any(ParseUser.State.Builder.class), any(JSONObject.class), any(ParseDecoder.class))) .thenReturn(builder); FileObjectStore<ParseUser> store = new FileObjectStore<>(ParseUser.class, file, coder); ParseUser user = ParseTaskUtils.wait(store.getAsync()); assertEquals("bar", user.getState().get("foo")); }