|
40 | 40 | import com.fasterxml.jackson.databind.JsonNode;
|
41 | 41 | import com.fasterxml.jackson.databind.ObjectMapper;
|
42 | 42 | import com.fasterxml.jackson.databind.node.ArrayNode;
|
| 43 | +import com.fasterxml.jackson.databind.node.BaseJsonNode; |
43 | 44 | import com.fasterxml.jackson.databind.node.DoubleNode;
|
44 | 45 | import com.fasterxml.jackson.databind.node.JsonNodeType;
|
45 | 46 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
| 47 | +import com.fasterxml.jackson.databind.node.ValueNode; |
46 | 48 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
47 | 49 |
|
| 50 | +import elemental.json.Json; |
| 51 | +import elemental.json.JsonArray; |
| 52 | +import elemental.json.JsonNull; |
| 53 | +import elemental.json.JsonObject; |
| 54 | +import elemental.json.JsonValue; |
| 55 | + |
48 | 56 | /**
|
49 | 57 | * Helpers for using <code>jackson</code>.
|
50 | 58 | * <p>
|
@@ -87,6 +95,83 @@ public static ArrayNode createArrayNode() {
|
87 | 95 | return objectMapper.createArrayNode();
|
88 | 96 | }
|
89 | 97 |
|
| 98 | + /** |
| 99 | + * Create a nullNode for null value. |
| 100 | + * |
| 101 | + * @return NullNode |
| 102 | + */ |
| 103 | + public static ValueNode nullNode() { |
| 104 | + return (ValueNode) objectMapper.nullNode(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Map JsonObject to ObjectNode. |
| 109 | + * |
| 110 | + * @param jsonObject |
| 111 | + * JsonObject to change |
| 112 | + * @return ObjectNode of elemental json object |
| 113 | + */ |
| 114 | + public static ObjectNode mapElemental(JsonObject jsonObject) { |
| 115 | + try { |
| 116 | + return (ObjectNode) objectMapper.readTree(jsonObject.toJson()); |
| 117 | + } catch (JsonProcessingException e) { |
| 118 | + throw new RuntimeException(e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Map JsonValue to ObjectNode. |
| 124 | + * |
| 125 | + * @param jsonValue |
| 126 | + * JsonValue to change |
| 127 | + * @return ObjectNode of elemental json value |
| 128 | + */ |
| 129 | + public static BaseJsonNode mapElemental(JsonValue jsonValue) { |
| 130 | + if (jsonValue == null || jsonValue instanceof JsonNull) { |
| 131 | + return nullNode(); |
| 132 | + } |
| 133 | + return objectMapper.valueToTree(jsonValue.asString()); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Convert the contents of an ArrayNode into a JsonArray. This is mostly |
| 138 | + * needed for arrays that may contain arrays and values. |
| 139 | + * |
| 140 | + * @param jsonNodes |
| 141 | + * ArrayNode to convert |
| 142 | + * @return JsonArray of ArrayNode content |
| 143 | + */ |
| 144 | + public static JsonArray createElementalArray(ArrayNode jsonNodes) { |
| 145 | + return (JsonArray) parseNode(jsonNodes); |
| 146 | + } |
| 147 | + |
| 148 | + private static JsonValue parseNode(JsonNode node) { |
| 149 | + if (node instanceof ArrayNode) { |
| 150 | + JsonArray jsonArray = Json.createArray(); |
| 151 | + node.forEach(arrayNode -> parseArrayNode(arrayNode, jsonArray)); |
| 152 | + return jsonArray; |
| 153 | + } |
| 154 | + return Json.parse(node.toString()); |
| 155 | + } |
| 156 | + |
| 157 | + private static void parseArrayNode(JsonNode node, JsonArray jsonArray) { |
| 158 | + if (JsonNodeType.NUMBER.equals(node.getNodeType())) { |
| 159 | + jsonArray.set(jsonArray.length(), Json.create(node.doubleValue())); |
| 160 | + } else if (JsonNodeType.STRING.equals(node.getNodeType())) { |
| 161 | + jsonArray.set(jsonArray.length(), Json.create(node.textValue())); |
| 162 | + } else if (JsonNodeType.ARRAY.equals(node.getNodeType())) { |
| 163 | + JsonArray array = Json.createArray(); |
| 164 | + node.forEach(arrayNode -> parseArrayNode(arrayNode, array)); |
| 165 | + jsonArray.set(jsonArray.length(), array); |
| 166 | + } else if (JsonNodeType.BOOLEAN.equals(node.getNodeType())) { |
| 167 | + jsonArray.set(jsonArray.length(), Json.create(node.booleanValue())); |
| 168 | + } else if (JsonNodeType.NULL.equals(node.getNodeType())) { |
| 169 | + jsonArray.set(jsonArray.length(), Json.createNull()); |
| 170 | + } else { |
| 171 | + jsonArray.set(jsonArray.length(), Json.parse(node.toString())); |
| 172 | + } |
| 173 | + } |
| 174 | + |
90 | 175 | /**
|
91 | 176 | * Read Json string to JsonNode.
|
92 | 177 | *
|
|
0 commit comments