/** Can we handle a Map? */ @Test public void map() throws JSONException { UserWithPhotos basicUser = new UserWithPhotos(); basicUser.uid = 12345L; basicUser.name = "Fred"; Map<String, Object> map = new HashMap<String, Object>(); map.put("testId", new BigInteger("412")); map.put("floatId", 123.45F); map.put("basicUser", basicUser); String json = createJsonMapper().toJson(map); String expectedJson = "{\"floatId\":123.45,\"testId\":412,\"basicUser\":{\"uid\":12345,\"photos\":null,\"name\":\"Fred\"}}"; JSONAssert.assertEquals(expectedJson, json, JSONCompareMode.NON_EXTENSIBLE); }
/** Can we handle a more complex Javabean? */ @Test public void complexJavabean() throws JSONException { UserWithPhotos userWithPhotos = new UserWithPhotos(); userWithPhotos.uid = 12345L; userWithPhotos.name = null; userWithPhotos.photos = new ArrayList<Photo>(); userWithPhotos.photos.add(new Photo()); Photo photo = new Photo(); photo.photoId = 5678L; photo.location = "Las Vegas"; userWithPhotos.photos.add(photo); String json = createJsonMapper().toJson(userWithPhotos); String expectedJson = "{\"uid\":12345,\"photos\":[{\"id\":null,\"location\":null},{\"id\":5678,\"location\":\"Las Vegas\"}],\"name\":null}"; JSONAssert.assertEquals(expectedJson, json, JSONCompareMode.NON_EXTENSIBLE); }