import java.util.Map;
public class UserJsonParser implements JsonObjectParser<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) {
// JIRA prior 5.0
final URI avatarUri = JsonParseUtil.parseURI(avatarUrl);
avatarUris.put(User.S48_48, avatarUri);
} else {
// JIRA 5.0+
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)));
}
}
// e-mail may be not set in response if e-mail visibility in jira configuration is set to hidden (in jira 4.3+)
final String emailAddress = JsonParseUtil.getOptionalString(json, "emailAddress");
// we expect always expanded groups, serving them is anyway cheap - that was the case for JIRA prior 5.0, now groups are not expanded...
final ExpandableProperty<String> groups = JsonParseUtil.parseExpandableProperty(json.getJSONObject("groups"), new JsonObjectParser<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);
}