private <T> Collection<T> parseOptionalArrayNotNullable(boolean shouldUseNestedValueJson, JSONObject json, JsonWeakParser<T> jsonParser, String... path) throws JSONException { Collection<T> res = parseOptionalArray(shouldUseNestedValueJson, json, jsonParser, path); return res == null ? Collections.<T>emptyList() : res; }
@Override public Issue parse(JSONObject s) throws JSONException { final Iterable<String> expandos = parseExpandos(s); final boolean isJira5x0OrNewer = Iterables.contains(expandos, SCHEMA_SECTION); final boolean shouldUseNestedValueAttribute = !isJira5x0OrNewer; if (isJira5x0OrNewer) { final JSONObject commentsJson = s.getJSONObject(FIELDS).getJSONObject(COMMENT_FIELD.id); comments = parseArray(commentsJson, new JsonWeakParserForJsonObject<Comment>(commentJsonParser), "comments"); final Collection<Comment> commentsTmp = parseOptionalArray( shouldUseNestedValueAttribute, s, new JsonWeakParserForJsonObject<Comment>(commentJsonParser), FIELDS, COMMENT_FIELD.id); comments = commentsTmp != null ? commentsTmp : Lists.<Comment>newArrayList(); final String summary = getFieldStringValue(s, SUMMARY_FIELD.id); final String description = getOptionalFieldStringUnisex(shouldUseNestedValueAttribute, s, DESCRIPTION_FIELD.id); final Collection<Attachment> attachments = parseOptionalArray(shouldUseNestedValueAttribute, s, new JsonWeakParserForJsonObject<Attachment>(attachmentJsonParser), FIELDS, ATTACHMENT_FIELD.id); final Collection<Field> fields = isJira5x0OrNewer ? parseFieldsJira5x0(s) : parseFields(s.getJSONObject(FIELDS)); final BasicIssueType issueType = issueTypeJsonParser.parse(getFieldUnisex(s, ISSUE_TYPE_FIELD.id)); final DateTime creationDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(s, CREATED_FIELD.id)); final DateTime updateDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(s, UPDATED_FIELD.id)); final String dueDateString = getOptionalFieldStringUnisex(shouldUseNestedValueAttribute, s, DUE_DATE_FIELD.id); final DateTime dueDate = dueDateString == null ? null : JsonParseUtil.parseDateTimeOrDate(dueDateString); final BasicPriority priority = getOptionalField(shouldUseNestedValueAttribute, s, PRIORITY_FIELD.id, priorityJsonParser); final BasicResolution resolution = getOptionalField(shouldUseNestedValueAttribute, s, RESOLUTION_FIELD.id, resolutionJsonParser); final BasicUser assignee = getOptionalField(shouldUseNestedValueAttribute, s, ASSIGNEE_FIELD.id, userJsonParser);
@Override public Issue parse(final JSONObject issueJson) throws JSONException { final BasicIssue basicIssue = basicIssueJsonParser.parse(issueJson); final Iterable<String> expandos = parseExpandos(issueJson); final JSONObject jsonFields = issueJson.getJSONObject(FIELDS); final JSONObject commentsJson = jsonFields.optJSONObject(COMMENT_FIELD.id); final Collection<Comment> comments = (commentsJson == null) ? Collections.<Comment>emptyList() : parseArray(commentsJson, new JsonWeakParserForJsonObject<Comment>(commentJsonParser), "comments"); final String summary = getFieldStringValue(issueJson, SUMMARY_FIELD.id); final String description = getOptionalFieldStringUnisex(issueJson, DESCRIPTION_FIELD.id); final Collection<Attachment> attachments = parseOptionalArray(issueJson, new JsonWeakParserForJsonObject<Attachment>(attachmentJsonParser), FIELDS, ATTACHMENT_FIELD.id); final Collection<IssueField> fields = parseFields(issueJson); final IssueType issueType = issueTypeJsonParser.parse(getFieldUnisex(issueJson, ISSUE_TYPE_FIELD.id)); final DateTime creationDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(issueJson, CREATED_FIELD.id)); final DateTime updateDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(issueJson, UPDATED_FIELD.id)); final String dueDateString = getOptionalFieldStringUnisex(issueJson, DUE_DATE_FIELD.id); final DateTime dueDate = dueDateString == null ? null : JsonParseUtil.parseDateTimeOrDate(dueDateString); final BasicPriority priority = getOptionalNestedField(issueJson, PRIORITY_FIELD.id, priorityJsonParser); final Resolution resolution = getOptionalNestedField(issueJson, RESOLUTION_FIELD.id, resolutionJsonParser); final User assignee = getOptionalNestedField(issueJson, ASSIGNEE_FIELD.id, userJsonParser); final User reporter = getOptionalNestedField(issueJson, REPORTER_FIELD.id, userJsonParser); final BasicProject project = projectJsonParser.parse(getFieldUnisex(issueJson, PROJECT_FIELD.id)); final Collection<IssueLink> issueLinks; issueLinks = parseOptionalArray(issueJson, new JsonWeakParserForJsonObject<IssueLink>(issueLinkJsonParserV5), FIELDS, LINKS_FIELD.id);
private Collection<Field> parseFieldsJira5x0(JSONObject issueJson) throws JSONException { final JSONObject names = issueJson.optJSONObject(NAMES_SECTION); final Map<String, String> namesMap = parseNames(names); final JSONObject types = issueJson.optJSONObject(SCHEMA_SECTION); final Map<String, String> typesMap = parseSchema(types); final JSONObject json = issueJson.getJSONObject(FIELDS); final ArrayList<Field> res = new ArrayList<Field>(json.length()); @SuppressWarnings("unchecked") final Iterator<String> iterator = json.keys(); while (iterator.hasNext()) { final String key = iterator.next(); try { if (SPECIAL_FIELDS.contains(key)) { continue; } final Object value = json.opt(key); res.add(new Field(key, namesMap.get(key), typesMap.get("key"), value != JSONObject.NULL ? value : null)); } catch (final Exception e) { throw new JSONException("Error while parsing [" + key + "] field: " + e.getMessage()) { @Override public Throwable getCause() { return e; } }; } } return res; }
@Nullable private <T> Collection<T> parseOptionalArray(boolean shouldUseNestedValueJson, JSONObject json, JsonWeakParser<T> jsonParser, String... path) throws JSONException { if (shouldUseNestedValueJson) { final JSONObject js = JsonParseUtil.getNestedOptionalObject(json, path); if (js == null) { return null; } return parseArray(js, jsonParser, VALUE_ATTR); } else { final JSONArray jsonArray = JsonParseUtil.getNestedOptionalArray(json, path); if (jsonArray == null) { return null; } final Collection<T> res = new ArrayList<T>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { res.add(jsonParser.parse(jsonArray.get(i))); } return res; } }
@Override public Issue parse(JSONObject s) throws JSONException { final Iterable<String> expandos = parseExpandos(s); final boolean isJira5x0OrNewer = Iterables.contains(expandos, SCHEMA_SECTION); final boolean shouldUseNestedValueAttribute = !isJira5x0OrNewer; if (isJira5x0OrNewer) { final JSONObject commentsJson = s.getJSONObject(FIELDS).getJSONObject(COMMENT_FIELD.id); comments = parseArray(commentsJson, new JsonWeakParserForJsonObject<Comment>(commentJsonParser), "comments"); final Collection<Comment> commentsTmp = parseOptionalArray( shouldUseNestedValueAttribute, s, new JsonWeakParserForJsonObject<Comment>(commentJsonParser), FIELDS, COMMENT_FIELD.id); comments = commentsTmp != null ? commentsTmp : Lists.<Comment>newArrayList(); final String summary = getFieldStringValue(s, SUMMARY_FIELD.id); final String description = getOptionalFieldStringUnisex(shouldUseNestedValueAttribute, s, DESCRIPTION_FIELD.id); final Collection<Attachment> attachments = parseOptionalArray(shouldUseNestedValueAttribute, s, new JsonWeakParserForJsonObject<Attachment>(attachmentJsonParser), FIELDS, ATTACHMENT_FIELD.id); final Collection<Field> fields = isJira5x0OrNewer ? parseFieldsJira5x0(s) : parseFields(s.getJSONObject(FIELDS)); final BasicIssueType issueType = issueTypeJsonParser.parse(getFieldUnisex(s, ISSUE_TYPE_FIELD.id)); final DateTime creationDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(s, CREATED_FIELD.id)); final DateTime updateDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(s, UPDATED_FIELD.id)); final String dueDateString = getOptionalFieldStringUnisex(shouldUseNestedValueAttribute, s, DUE_DATE_FIELD.id); final DateTime dueDate = dueDateString == null ? null : JsonParseUtil.parseDateTimeOrDate(dueDateString); final BasicPriority priority = getOptionalField(shouldUseNestedValueAttribute, s, PRIORITY_FIELD.id, priorityJsonParser); final BasicResolution resolution = getOptionalField(shouldUseNestedValueAttribute, s, RESOLUTION_FIELD.id, resolutionJsonParser); final BasicUser assignee = getOptionalField(shouldUseNestedValueAttribute, s, ASSIGNEE_FIELD.id, userJsonParser);
@Override public Issue parse(final JSONObject issueJson) throws JSONException { final BasicIssue basicIssue = basicIssueJsonParser.parse(issueJson); final Iterable<String> expandos = parseExpandos(issueJson); final JSONObject jsonFields = issueJson.getJSONObject(FIELDS); final JSONObject commentsJson = jsonFields.optJSONObject(COMMENT_FIELD.id); final Collection<Comment> comments = (commentsJson == null) ? Collections.<Comment>emptyList() : parseArray(commentsJson, new JsonWeakParserForJsonObject<Comment>(commentJsonParser), "comments"); final String summary = getFieldStringValue(issueJson, SUMMARY_FIELD.id); final String description = getOptionalFieldStringUnisex(issueJson, DESCRIPTION_FIELD.id); final Collection<Attachment> attachments = parseOptionalArray(issueJson, new JsonWeakParserForJsonObject<Attachment>(attachmentJsonParser), FIELDS, ATTACHMENT_FIELD.id); final Collection<IssueField> fields = parseFields(issueJson); final IssueType issueType = issueTypeJsonParser.parse(getFieldUnisex(issueJson, ISSUE_TYPE_FIELD.id)); final DateTime creationDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(issueJson, CREATED_FIELD.id)); final DateTime updateDate = JsonParseUtil.parseDateTime(getFieldStringUnisex(issueJson, UPDATED_FIELD.id)); final String dueDateString = getOptionalFieldStringUnisex(issueJson, DUE_DATE_FIELD.id); final DateTime dueDate = dueDateString == null ? null : JsonParseUtil.parseDateTimeOrDate(dueDateString); final BasicPriority priority = getOptionalNestedField(issueJson, PRIORITY_FIELD.id, priorityJsonParser); final Resolution resolution = getOptionalNestedField(issueJson, RESOLUTION_FIELD.id, resolutionJsonParser); final User assignee = getOptionalNestedField(issueJson, ASSIGNEE_FIELD.id, userJsonParser); final User reporter = getOptionalNestedField(issueJson, REPORTER_FIELD.id, userJsonParser); final BasicProject project = projectJsonParser.parse(getFieldUnisex(issueJson, PROJECT_FIELD.id)); final Collection<IssueLink> issueLinks; issueLinks = parseOptionalArray(issueJson, new JsonWeakParserForJsonObject<IssueLink>(issueLinkJsonParserV5), FIELDS, LINKS_FIELD.id);
private Collection<Field> parseFieldsJira5x0(JSONObject issueJson) throws JSONException { final JSONObject names = issueJson.optJSONObject(NAMES_SECTION); final Map<String, String> namesMap = parseNames(names); final JSONObject types = issueJson.optJSONObject(SCHEMA_SECTION); final Map<String, String> typesMap = parseSchema(types); final JSONObject json = issueJson.getJSONObject(FIELDS); final ArrayList<Field> res = new ArrayList<Field>(json.length()); @SuppressWarnings("unchecked") final Iterator<String> iterator = json.keys(); while (iterator.hasNext()) { final String key = iterator.next(); try { if (SPECIAL_FIELDS.contains(key)) { continue; } final Object value = json.opt(key); res.add(new Field(key, namesMap.get(key), typesMap.get("key"), value != JSONObject.NULL ? value : null)); } catch (final Exception e) { throw new JSONException("Error while parsing [" + key + "] field: " + e.getMessage()) { @Override public Throwable getCause() { return e; } }; } } return res; }
@Nullable private <T> Collection<T> parseOptionalArray(boolean shouldUseNestedValueJson, JSONObject json, JsonWeakParser<T> jsonParser, String... path) throws JSONException { if (shouldUseNestedValueJson) { final JSONObject js = JsonParseUtil.getNestedOptionalObject(json, path); if (js == null) { return null; } return parseArray(js, jsonParser, VALUE_ATTR); } else { final JSONArray jsonArray = JsonParseUtil.getNestedOptionalArray(json, path); if (jsonArray == null) { return null; } final Collection<T> res = new ArrayList<T>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { res.add(jsonParser.parse(jsonArray.get(i))); } return res; } }
private Collection<IssueField> parseFields(final JSONObject issueJson) throws JSONException { final JSONObject names = (providedNames != null) ? providedNames : issueJson.optJSONObject(NAMES_SECTION); final Map<String, String> namesMap = parseNames(names); final JSONObject schema = (providedSchema != null) ? providedSchema : issueJson.optJSONObject(SCHEMA_SECTION); final Map<String, String> typesMap = parseSchema(schema);
private <T> Collection<T> parseOptionalArrayNotNullable(final JSONObject json, final JsonWeakParser<T> jsonParser, final String... path) throws JSONException { Collection<T> res = parseOptionalArray(json, jsonParser, path); return res == null ? Collections.<T>emptyList() : res; }
private Collection<IssueField> parseFields(final JSONObject issueJson) throws JSONException { final JSONObject names = (providedNames != null) ? providedNames : issueJson.optJSONObject(NAMES_SECTION); final Map<String, String> namesMap = parseNames(names); final JSONObject schema = (providedSchema != null) ? providedSchema : issueJson.optJSONObject(SCHEMA_SECTION); final Map<String, String> typesMap = parseSchema(schema); final JSONObject json = issueJson.getJSONObject(FIELDS); final ArrayList<IssueField> res = new ArrayList<IssueField>(json.length()); @SuppressWarnings("unchecked") final Iterator<String> iterator = json.keys(); while (iterator.hasNext()) { final String key = iterator.next(); try { if (SPECIAL_FIELDS.contains(key)) { continue; } // TODO: JRJC-122 // we should use fieldParser here (some new version as the old one probably won't work) // enable IssueJsonParserTest#testParseIssueWithUserPickerCustomFieldFilledOut after fixing this final Object value = json.opt(key); res.add(new IssueField(key, namesMap.get(key), typesMap.get("key"), value != JSONObject.NULL ? value : null)); } catch (final Exception e) { throw new JSONException("Error while parsing [" + key + "] field: " + e.getMessage()) { @Override public Throwable getCause() { return e; } }; } } return res; }
private <T> Collection<T> parseOptionalArrayNotNullable(final JSONObject json, final JsonWeakParser<T> jsonParser, final String... path) throws JSONException { Collection<T> res = parseOptionalArray(json, jsonParser, path); return res == null ? Collections.<T>emptyList() : res; }
private <T> Collection<T> parseOptionalArrayNotNullable(boolean shouldUseNestedValueJson, JSONObject json, JsonWeakParser<T> jsonParser, String... path) throws JSONException { Collection<T> res = parseOptionalArray(shouldUseNestedValueJson, json, jsonParser, path); return res == null ? Collections.<T>emptyList() : res; }