View Javadoc

1   package com.atlassian.asap.nimbus.parser;
2   
3   import com.atlassian.asap.core.serializer.Json;
4   import net.minidev.json.JSONArray;
5   import net.minidev.json.JSONObject;
6   
7   import javax.json.JsonArrayBuilder;
8   import javax.json.JsonObjectBuilder;
9   import javax.json.JsonValue;
10  import java.math.BigDecimal;
11  import java.math.BigInteger;
12  import java.util.Map;
13  
14  /**
15   * Translates Nimbus (org.minidev.json) objects into JSR 353.
16   */
17  class NimbusJsr353Translator {
18      static JsonValue nimbusToJsr353(Object nimbusObject) {
19          if (nimbusObject instanceof JSONObject) {
20              return convertObject((JSONObject) nimbusObject);
21          } else if (nimbusObject instanceof JSONArray) {
22              return convertArray((JSONArray) nimbusObject);
23          } else if (nimbusObject instanceof Boolean) {
24              return ((Boolean) nimbusObject) ? JsonValue.TRUE : JsonValue.FALSE;
25          } else if (nimbusObject == null) {
26              return JsonValue.NULL;
27          } else if (nimbusObject instanceof String) {
28              throw new RuntimeException("Cannot convert string values at the root level");
29          } else if (nimbusObject instanceof Number) {
30              throw new RuntimeException("Cannot convert numeric values at the root level");
31          } else {
32              throw new IllegalStateException("Broken JSON, type " + nimbusObject.getClass());
33          }
34      }
35  
36      private static JsonValue convertArray(JSONArray nimbusObject) {
37          JsonArrayBuilder arrayBuilder = Json.provider().createArrayBuilder();
38          for (Object arrayItem : nimbusObject) {
39              if (arrayItem instanceof String) {
40                  arrayBuilder.add((String) arrayItem);
41              } else if (arrayItem instanceof Integer) {
42                  arrayBuilder.add((Integer) arrayItem);
43              } else if (arrayItem instanceof Long) {
44                  arrayBuilder.add((Long) arrayItem);
45              } else if (arrayItem instanceof Double) {
46                  arrayBuilder.add((Double) arrayItem);
47              } else if (arrayItem instanceof BigInteger) {
48                  arrayBuilder.add((BigInteger) arrayItem);
49              } else if (arrayItem instanceof BigDecimal) {
50                  arrayBuilder.add((BigDecimal) arrayItem);
51              } else { // all the other types can be converted recursively
52                  arrayBuilder.add(nimbusToJsr353(arrayItem));
53              }
54          }
55          return arrayBuilder.build();
56      }
57  
58      private static JsonValue convertObject(JSONObject nimbusObject) {
59          JsonObjectBuilder objectBuilder = Json.provider().createObjectBuilder();
60          for (Map.Entry<String, Object> entry : nimbusObject.entrySet()) {
61              if (entry.getValue() instanceof String) {
62                  objectBuilder.add(entry.getKey(), (String) entry.getValue());
63              } else if (entry.getValue() instanceof Integer) {
64                  objectBuilder.add(entry.getKey(), (Integer) entry.getValue());
65              } else if (entry.getValue() instanceof Long) {
66                  objectBuilder.add(entry.getKey(), (Long) entry.getValue());
67              } else if (entry.getValue() instanceof BigInteger) {
68                  objectBuilder.add(entry.getKey(), (BigInteger) entry.getValue());
69              } else if (entry.getValue() instanceof BigDecimal) {
70                  objectBuilder.add(entry.getKey(), (BigDecimal) entry.getValue());
71              } else { // all the other types can be converted recursively
72                  objectBuilder.add(entry.getKey(), nimbusToJsr353(entry.getValue()));
73              }
74          }
75          return objectBuilder.build();
76      }
77  }