private static JSONObject readJSONFileQuietly(File file) { JSONObject json = null; if (file != null) { try { json = ParseFileUtils.readFileToJSONObject(file); } catch (IOException | JSONException e) { // do nothing } } return json; }
/** * Grabs one entry in the local id map off the disk. */ private synchronized MapEntry getMapEntry(String localId) { if (!isLocalId(localId)) { throw new IllegalStateException("Tried to get invalid local id: \"" + localId + "\"."); } try { JSONObject json = ParseFileUtils.readFileToJSONObject(new File(diskPath, localId)); MapEntry entry = new MapEntry(); entry.retainCount = json.optInt("retainCount", 0); entry.objectId = json.optString("objectId", null); return entry; } catch (IOException | JSONException e) { return new MapEntry(); } }
/** * Retrieves a {@code ParseConfig} from a file on disk. * * @return The {@code ParseConfig} that was retrieved. If the file wasn't found, or the contents * of the file is an invalid {@code ParseConfig}, returns null. */ /* package for test */ ParseConfig getFromDisk() { JSONObject json; try { json = ParseFileUtils.readFileToJSONObject(currentConfigFile); } catch (IOException | JSONException e) { return null; } return ParseConfig.decode(json, ParseDecoder.get()); }
/** * Removes a file from the file system and any internal caches. */ private void removeFile(File file) { synchronized (lock) { // Remove the data in memory for this command. pendingTasks.remove(file); // Release all the localIds referenced by the command. // Read one command from the cache. JSONObject json; try { json = ParseFileUtils.readFileToJSONObject(file); ParseRESTCommand command = commandFromJSON(json); command.releaseLocalIds(); } catch (Exception e) { // Well, we did our best. We'll just have to leak a localId. } // Delete the command file itself. ParseFileUtils.deleteQuietly(file); } }
@Test public void testReadFileToJSONObject() throws Exception { File file = temporaryFolder.newFile("file.txt"); BufferedOutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); out.write(TEST_JSON.getBytes("UTF-8")); } finally { ParseIOUtils.closeQuietly(out); } JSONObject json = ParseFileUtils.readFileToJSONObject(file); assertNotNull(json); assertEquals("bar", json.getString("foo")); }
@Test public void testSetAsync() throws Exception { File file = new File(temporaryFolder.getRoot(), "test"); ParseUser.State state = mock(ParseUser.State.class); JSONObject json = new JSONObject(); json.put("foo", "bar"); ParseUserCurrentCoder coder = mock(ParseUserCurrentCoder.class); when(coder.encode(eq(state), (ParseOperationSet) isNull(), any(PointerEncoder.class))) .thenReturn(json); FileObjectStore<ParseUser> store = new FileObjectStore<>(ParseUser.class, file, coder); ParseUser user = mock(ParseUser.class); when(user.getState()).thenReturn(state); ParseTaskUtils.wait(store.setAsync(user)); JSONObject jsonAgain = ParseFileUtils.readFileToJSONObject(file); assertEquals(json, jsonAgain, JSONCompareMode.STRICT); }
/** * Retrieves a {@code ParseObject} from a file on disk in /2/ format. * * @param coder Current coder to decode the ParseObject. * @param file The file to retrieve the object from. * @param builder An empty builder which is used to generate a empty state and rebuild a ParseObject. * @return The {@code ParseObject} that was retrieved. If the file wasn't found, or the contents * of the file is an invalid {@code ParseObject}, returns {@code null}. * @see #saveToDisk(ParseObjectCurrentCoder, ParseObject, File) */ private static <T extends ParseObject> T getFromDisk( ParseObjectCurrentCoder coder, File file, ParseObject.State.Init builder) { JSONObject json; try { json = ParseFileUtils.readFileToJSONObject(file); } catch (IOException | JSONException e) { return null; } ParseObject.State newState = coder.decode(builder, json, ParseDecoder.get()) .isComplete(true) .build(); return ParseObject.from(newState); }
json = ParseFileUtils.readFileToJSONObject(file); } catch (FileNotFoundException e) {