UserJsonParser implements JsonParser<User> { @Override public User parse(JSONObject json) throws JSONException { final BasicUser basicUser = JsonParseUtil.parseBasicUser(json); final String timezone = JsonParseUtil.getOptionalString(json, "timeZone"); final String avatarUrl = JsonParseUtil.getOptionalString(json, "avatarUrl"); Map<String, URI> avatarUris = Maps.newHashMap(); if (avatarUrl != null) { final URI avatarUri = JsonParseUtil.parseURI(avatarUrl); avatarUris.put(User.S48_48, avatarUri); } else { final JSONObject avatarUrlsJson = json.getJSONObject("avatarUrls"); @SuppressWarnings("unchecked") final Iterator<String> iterator = avatarUrlsJson.keys(); while (iterator.hasNext()) { final String key = iterator.next(); avatarUris.put(key, JsonParseUtil.parseURI(avatarUrlsJson.getString(key))); } } final String emailAddress = json.getString("emailAddress"); final ExpandableProperty<String> groups = JsonParseUtil.parseExpandableProperty(json.getJSONObject("groups"), new JsonParser<String>() { @Override public String parse(JSONObject json) throws JSONException { return json.getString("name"); } }); return new User(basicUser.getSelf(), basicUser.getName(), basicUser.getDisplayName(), emailAddress, groups, avatarUris, timezone); }  @Override User parse(JSONObject json);  }
@Test public void testParse() throws Exception { final UserJsonParser parser = new UserJsonParser(); final User user = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/user/valid.json")); assertEquals(TestUtil.toUri("http: assertNull(user.getSmallAvatarUri()); assertEquals("admin", user.getName()); assertEquals("Administrator", user.getDisplayName()); assertEquals("user@atlassian.com", user.getEmailAddress()); assertEquals(new ExpandableProperty<String>(3, ImmutableList.of("jira-administrators", "jira-developers", "jira-users")), user.getGroups()); assertNull(user.getTimezone()); }
@Test public void testParseJira5x0User() throws Exception { final UserJsonParser parser = new UserJsonParser(); final User user = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/user/valid-5.0.json")); assertEquals(TestUtil.toUri("http: assertEquals(TestUtil.toUri("http: assertEquals("wseliga", user.getName()); assertEquals("Wojciech Seliga", user.getDisplayName()); assertEquals("wseliga@atlassian.com", user.getEmailAddress()); assertEquals(1, user.getGroups().getSize()); assertNull(user.getGroups().getItems()); assertEquals("Europe/Warsaw", user.getTimezone()); }