-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonReverse.java
80 lines (68 loc) · 3.59 KB
/
JsonReverse.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package no.dervis.jsonreverse;
import com.fasterxml.jackson.core.JsonProcessingException;
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.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.Collectors.toMap;
public class JsonReverse {
// algebraic type definitions
sealed interface Node permits StringNode, MapNode, ListNode, IntNode { }
record StringNode(String value) implements Node {}
record IntNode(Integer value) implements Node {}
record MapNode(Map<String, Node> value) implements Node {}
record ListNode(List<Node> value) implements Node {}
// function that makes char array to list of strings
static final Function<char[], List<String>> toListFn = array ->
IntStream
.range(0, array.length)
.map(i -> array[i])
.mapToObj(Character::toString).toList();
// function that makes a string list to a reversed string
static final Function<List<String>, String> reverseFn = c -> c.stream()
.collect(Collector.of(() -> new ArrayDeque<String>(),
ArrayDeque::addFirst,
(a, b) -> a))
.stream().map(Object::toString).collect(Collectors.joining());
// composed function that reverses a string
static final Function<String, String> transformFn = s -> reverseFn.compose(toListFn).apply(s.toCharArray());
// mapping function that, given a string and a mapping function, produces another string
static final BiFunction<String, Function<String, String>, String> transform = String::transform;
// function that reverses a string using a transform function
static final Function<String, String> reverseStringFn = s -> transform.apply(s, transformFn);
// custom deserializer to map to our own types
public static Node parseNode(JsonNode node) {
return switch (node) {
case ArrayNode a ->
new ListNode(StreamSupport
.stream(spliteratorUnknownSize(a.iterator(), Spliterator.ORDERED), false)
.map(JsonReverse::parseNode)
.toList());
case ObjectNode o ->
new MapNode(StreamSupport
.stream(spliteratorUnknownSize(o.fields(), Spliterator.ORDERED), false)
.map(e -> new SimpleImmutableEntry<>(e.getKey(), parseNode(e.getValue())))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y, LinkedHashMap::new)));
case TextNode a -> new StringNode(reverseStringFn.apply(a.textValue()));
case NumericNode a -> new IntNode(a.intValue());
default -> throw new RuntimeException("What is this?! ");
};
}
public static void main(String[] args) throws JsonProcessingException {
JsonNode node = new ObjectMapper().readTree("""
{"a": [1, {"b": [2, {"c": "Hello,"}]}, {"b": [{"c": "world!"}]}]}
""");
System.out.println(parseNode(node));
}
}