1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.jira.rest.client.internal.json.gen;
18
19 import com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue;
20 import org.codehaus.jettison.json.JSONArray;
21 import org.codehaus.jettison.json.JSONException;
22 import org.codehaus.jettison.json.JSONObject;
23
24 import java.util.Map;
25
26
27
28
29
30
31 public class ComplexIssueInputFieldValueJsonGenerator implements JsonGenerator<ComplexIssueInputFieldValue> {
32 @Override
33 public JSONObject generate(ComplexIssueInputFieldValue bean) throws JSONException {
34 final JSONObject json = new JSONObject();
35 for (Map.Entry<String, Object> entry : bean.getValuesMap().entrySet()) {
36 json.put(entry.getKey(), generateFieldValueForJson(entry.getValue()));
37 }
38 return json;
39 }
40
41 public Object generateFieldValueForJson(Object rawValue) throws JSONException {
42 if (rawValue == null) {
43 return JSONObject.NULL;
44 } else if (rawValue instanceof ComplexIssueInputFieldValue) {
45 return generate((ComplexIssueInputFieldValue) rawValue);
46 } else if (rawValue instanceof Iterable) {
47
48 final JSONArray array = new JSONArray();
49 for (Object value : (Iterable) rawValue) {
50 array.put(generateFieldValueForJson(value));
51 }
52 return array;
53 } else if (rawValue instanceof CharSequence) {
54 return rawValue.toString();
55 } else if (rawValue instanceof Number) {
56 return rawValue;
57 } else {
58 throw new JSONException("Cannot generate value - unknown type for me: " + rawValue.getClass());
59 }
60 }
61 }