@Test public void testJsonArrayParser() throws Exception { String jsonActivity = "[" + "{\"numberOfComments\":1,\"identityId\":\"d5039b437f0001010011fd153a4fcbd8\",\"liked\":true,}," + "{\"numberOfComments\":2,\"identityId\":\"d5039b437f0001010011fd153a4fcba8\",\"liked\":false,}" + "]"; RestActivity model1 = SocialJSONDecodingSupport.JSONArrayObjectParser(RestActivity.class, jsonActivity).get(0); RestActivity model2 = SocialJSONDecodingSupport.JSONArrayObjectParser(RestActivity.class, jsonActivity).get(1); assertEquals(model1.getIdentityId(), "d5039b437f0001010011fd153a4fcbd8"); assertEquals(model2.getIdentityId(), "d5039b437f0001010011fd153a4fcba8"); String jsonActivity1 = "{\"activities\":[" + "{" + "\"appId\":null,\"identityId\":\"f845f6ed7f000101003ed4d98a09beb3\"," + "\"totalNumberOfComments\":0,\"liked\":false,\"templateParams\":{}," + "\"postedTime\":1309839511830,\"type\":\"DEFAULT_ACTIVITY\"," + "\"posterIdentity\":null,\"activityStream\":null," + "\"id\":\"f884d11a7f000101000230e5c0e8a602\"," + "\"title\":\"hello\",\"priority\":null," + "\"createdAt\":\"Tue Jul 5 11:18:31 +0700 2011\"," + "\"likedByIdentities\":null,\"titleId\":null,\"comments\":null}" + "]}"; JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonActivity1); JSONArray jsonArray = (JSONArray) jsonObject.get("activities"); RestActivity model3 = SocialJSONDecodingSupport.JSONArrayObjectParser( RestActivity.class, jsonArray.toJSONString()) .get(0); assertEquals(model3.getIdentityId(), "f845f6ed7f000101003ed4d98a09beb3"); }
@Test public void testJSONParser() throws Exception { String jsonActivity = "{\"numberOfComments\":1,\"identityId\":\"d5039b437f0001010011fd153a4fcbd8\",\"liked\":true,}"; Map modelMap = SocialJSONDecodingSupport.parser(jsonActivity); assertEquals(modelMap.get("identityId"), "d5039b437f0001010011fd153a4fcbd8"); }
@Test public void testJSONParserWithClassType() throws Exception { String jsonActivity = "{\"numberOfComments\":1,\"identityId\":\"d5039b437f0001010011fd153a4fcbd8\",\"liked\":true,}"; RestActivity model = SocialJSONDecodingSupport.parser(RestActivity.class, jsonActivity); assertEquals(model.getIdentityId(), "d5039b437f0001010011fd153a4fcbd8"); }
/** * Gets the list activities from response. * * @param response * @return */ private List<RestActivity> getListActivitiesFromResponse(HttpResponse response) { try { JSONObject jsonObject = (JSONObject) JSONValue.parse(getContent(response)); JSONArray jsonArray = (JSONArray) jsonObject.get("activities"); List<RestActivity> activities = SocialJSONDecodingSupport.JSONArrayObjectParser( RestActivity.class, jsonArray.toJSONString()); List<RestActivity> copyRestActivities = new ArrayList(activities); Collections.copy(copyRestActivities, activities); return copyRestActivities; } catch (Exception e) { throw new ServiceException(ActivityService.class, "invalid response", null); } }