/** * Converts an iterable type to a JSON array. * * @param iter Iterable type containing field values * @param type Name of the item type * @param custom Name of the custom type * @return a JSON-encoded array of items */ public static JSONArray toArray(Iterable iter, String type, String custom) throws JiraException { JSONArray results = new JSONArray(); if (type == null) throw new JiraException("Array field metadata is missing item type"); for (Object val : iter) { Operation oper = null; Object realValue = null; Object realResult = null; if (val instanceof Operation) { oper = (Operation) val; realValue = oper.value; } else realValue = val; if (type.equals("component") || type.equals("group") || type.equals("user") || type.equals("version")) { JSONObject itemMap = new JSONObject(); if (realValue instanceof ValueTuple) { ValueTuple tuple = (ValueTuple) realValue; itemMap.put(tuple.type, tuple.value.toString()); } else itemMap.put(ValueType.NAME.toString(), realValue.toString()); realResult = itemMap; } else if (type.equals("option") || (type.equals("string") && custom != null && (custom.equals("com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes") || custom.equals( "com.atlassian.jira.plugin.system.customfieldtypes:multiselect")))) { realResult = new JSONObject(); ((JSONObject) realResult).put(ValueType.VALUE.toString(), realValue.toString()); } else if (type.equals("string")) realResult = realValue.toString(); if (oper != null) { JSONObject operMap = new JSONObject(); operMap.put(oper.name, realResult); results.add(operMap); } else results.add(realResult); } return results; }
/** * Converts the given value to a JSON object. * * @param name Field name * @param value New field value * @param editmeta Edit metadata JSON object * @return a JSON-encoded field value * @throws JiraException when a value is bad or field has invalid metadata * @throws UnsupportedOperationException when a field type isn't supported */ public static Object toJson(String name, Object value, JSONObject editmeta) throws JiraException, UnsupportedOperationException { Meta m = getFieldMetadata(name, editmeta); if (m.type == null) throw new JiraException("Field '" + name + "' is missing metadata type"); if (m.type.equals("array")) { if (value == null) value = new ArrayList(); else if (!(value instanceof Iterable)) throw new JiraException("Field '" + name + "' expects an Iterable value"); return toArray((Iterable) value, m.items, m.custom); } else if (m.type.equals("date")) { if (value == null) return JSONNull.getInstance(); Date d = toDate(value); if (d == null) throw new JiraException("Field '" + name + "' expects a date value or format is invalid"); SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); return df.format(d); } else if (m.type.equals("datetime")) { if (value == null) return JSONNull.getInstance(); else if (!(value instanceof Timestamp)) throw new JiraException("Field '" + name + "' expects a Timestamp value"); SimpleDateFormat df = new SimpleDateFormat(DATETIME_FORMAT); return df.format(value); } else if (m.type.equals("issuetype") || m.type.equals("priority") || m.type.equals("user") || m.type.equals("resolution")) { JSONObject json = new JSONObject(); if (value == null) return JSONNull.getInstance(); else if (value instanceof ValueTuple) { ValueTuple tuple = (ValueTuple) value; json.put(tuple.type, tuple.value.toString()); } else json.put(ValueType.NAME.toString(), value.toString()); return json.toString(); } else if (m.type.equals("project") || m.type.equals("issuelink")) { JSONObject json = new JSONObject(); if (value == null) return JSONNull.getInstance(); else if (value instanceof ValueTuple) { ValueTuple tuple = (ValueTuple) value; json.put(tuple.type, tuple.value.toString()); } else json.put(ValueType.KEY.toString(), value.toString()); return json.toString(); } else if (m.type.equals("string") || (m.type.equals("securitylevel"))) { if (value == null) return ""; else if (value instanceof List) return toJsonMap((List) value); else if (value instanceof ValueTuple) { JSONObject json = new JSONObject(); ValueTuple tuple = (ValueTuple) value; json.put(tuple.type, tuple.value.toString()); return json.toString(); } return value.toString(); } else if (m.type.equals("timetracking")) { if (value == null) return JSONNull.getInstance(); else if (value instanceof TimeTracking) return ((TimeTracking) value).toJsonObject(); } else if (m.type.equals("number")) { if (!(value instanceof java.lang.Integer) && !(value instanceof java.lang.Double) && !(value instanceof java.lang.Float) && !(value instanceof java.lang.Long)) { throw new JiraException("Field '" + name + "' expects a Numeric value"); } return value; } throw new UnsupportedOperationException(m.type + " is not a supported field type"); }