This repository was archived by the owner on Nov 5, 2019. It is now read-only.
forked from socketio/socket.io-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSupport.java
More file actions
60 lines (53 loc) · 2.23 KB
/
Copy pathJsonSupport.java
File metadata and controls
60 lines (53 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package de.comroid.util.json;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
public final class JsonSupport {
private JsonSupport() {
throw new UnsupportedOperationException("Cannot instantiate JsonSupport");
}
public static JsonNode nodeOf(Object of) {
if (of == null) {
return JsonNodeFactory.instance.nullNode();
} else if (of instanceof JsonNode) {
return (JsonNode) of;
} else if (of instanceof Collection) {
return arrayNode(of);
} else if (of instanceof Stream) {
return arrayNode(((Stream) of).toArray());
} else if (of instanceof Integer) {
return JsonNodeFactory.instance.numberNode((Integer) of);
} else if (of instanceof Long) {
return JsonNodeFactory.instance.numberNode((Long) of);
} else if (of instanceof Double) {
return JsonNodeFactory.instance.numberNode((Double) of);
} else if (of instanceof String) {
return JsonNodeFactory.instance.textNode((String) of);
} else if (of instanceof Boolean) {
return JsonNodeFactory.instance.booleanNode((Boolean) of);
} else {
return JsonNodeFactory.instance.textNode(of.toString());
}
}
public static <T, N> ArrayNode arrayNode(List<T> items, Function<T, N> mapper) {
ArrayNode node = JsonNodeFactory.instance.arrayNode(items.size());
for (T item : items) node.add(nodeOf(mapper.apply(item)));
return node;
}
public static <T> ArrayNode arrayNode(List<T> items) {
ArrayNode node = JsonNodeFactory.instance.arrayNode(items.size());
for (T item : items) node.add(nodeOf(item));
return node;
}
public static ArrayNode arrayNode(Object... items) {
ArrayNode node = JsonNodeFactory.instance.arrayNode(items.length);
for (Object item : items) node.add(nodeOf(item));
return node;
}
}