File tree Expand file tree Collapse file tree 11 files changed +168
-7
lines changed
main/java/io/avaje/json/node
test/java/io/avaje/json/node Expand file tree Collapse file tree 11 files changed +168
-7
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,24 @@ public String toString() {
37
37
return text ();
38
38
}
39
39
40
+ @ Override
41
+ public JsonArray unmodifiable () {
42
+ final var newList = new ArrayList <JsonNode >(children .size ());
43
+ for (JsonNode child : children ) {
44
+ newList .add (child .unmodifiable ());
45
+ }
46
+ return of (newList );
47
+ }
48
+
49
+ @ Override
50
+ public JsonArray copy () {
51
+ final var newList = new ArrayList <JsonNode >(children .size ());
52
+ for (JsonNode child : children ) {
53
+ newList .add (child .copy ());
54
+ }
55
+ return new JsonArray (newList );
56
+ }
57
+
40
58
@ Override
41
59
public Type type () {
42
60
return Type .ARRAY ;
Original file line number Diff line number Diff line change 1
1
package io .avaje .json .node ;
2
2
3
- public final class JsonBoolean implements JsonNode {
3
+ public final /*value*/ class JsonBoolean implements JsonNode {
4
4
5
5
private final boolean value ;
6
6
@@ -17,6 +17,16 @@ public String toString() {
17
17
return text ();
18
18
}
19
19
20
+ @ Override
21
+ public JsonBoolean unmodifiable () {
22
+ return this ;
23
+ }
24
+
25
+ @ Override
26
+ public JsonBoolean copy () {
27
+ return this ;
28
+ }
29
+
20
30
@ Override
21
31
public Type type () {
22
32
return Type .BOOLEAN ;
Original file line number Diff line number Diff line change 4
4
5
5
import java .math .BigDecimal ;
6
6
7
- public final class JsonDecimal implements JsonNumber {
7
+ public final /*value*/ class JsonDecimal implements JsonNumber {
8
8
9
9
private final BigDecimal value ;
10
10
@@ -21,6 +21,16 @@ public String toString() {
21
21
return text ();
22
22
}
23
23
24
+ @ Override
25
+ public JsonDecimal unmodifiable () {
26
+ return this ;
27
+ }
28
+
29
+ @ Override
30
+ public JsonDecimal copy () {
31
+ return this ;
32
+ }
33
+
24
34
@ Override
25
35
public Type type () {
26
36
return Type .NUMBER ;
Original file line number Diff line number Diff line change 4
4
5
5
import java .math .BigDecimal ;
6
6
7
- public final class JsonDouble implements JsonNumber {
7
+ public final /*value*/ class JsonDouble implements JsonNumber {
8
8
9
9
private final double value ;
10
10
@@ -21,6 +21,16 @@ public String toString() {
21
21
return text ();
22
22
}
23
23
24
+ @ Override
25
+ public JsonDouble unmodifiable () {
26
+ return this ;
27
+ }
28
+
29
+ @ Override
30
+ public JsonDouble copy () {
31
+ return this ;
32
+ }
33
+
24
34
@ Override
25
35
public Type type () {
26
36
return Type .NUMBER ;
Original file line number Diff line number Diff line change 4
4
5
5
import java .math .BigDecimal ;
6
6
7
- public final class JsonInteger implements JsonNumber {
7
+ public final /*value*/ class JsonInteger implements JsonNumber {
8
8
9
9
private final int value ;
10
10
@@ -21,6 +21,16 @@ public String toString() {
21
21
return text ();
22
22
}
23
23
24
+ @ Override
25
+ public JsonInteger unmodifiable () {
26
+ return this ;
27
+ }
28
+
29
+ @ Override
30
+ public JsonInteger copy () {
31
+ return this ;
32
+ }
33
+
24
34
@ Override
25
35
public Type type () {
26
36
return Type .NUMBER ;
Original file line number Diff line number Diff line change 4
4
5
5
import java .math .BigDecimal ;
6
6
7
- public final class JsonLong implements JsonNumber {
7
+ public final /*value*/ class JsonLong implements JsonNumber {
8
8
9
9
private final long value ;
10
10
@@ -21,6 +21,16 @@ public String toString() {
21
21
return text ();
22
22
}
23
23
24
+ @ Override
25
+ public JsonLong unmodifiable () {
26
+ return this ;
27
+ }
28
+
29
+ @ Override
30
+ public JsonLong copy () {
31
+ return this ;
32
+ }
33
+
24
34
@ Override
25
35
public Type type () {
26
36
return Type .NUMBER ;
Original file line number Diff line number Diff line change @@ -72,6 +72,16 @@ public boolean isObject() {
72
72
*/
73
73
String text ();
74
74
75
+ /**
76
+ * Return an unmodifiable deep copy of the JsonNode.
77
+ */
78
+ JsonNode unmodifiable ();
79
+
80
+ /**
81
+ * Return a mutable deep copy of the JsonNode.
82
+ */
83
+ JsonNode copy ();
84
+
75
85
/**
76
86
* Find a node given a path using dot notation.
77
87
* @param path The path in dot notation
Original file line number Diff line number Diff line change @@ -51,6 +51,24 @@ public String text() {
51
51
return children .toString ();
52
52
}
53
53
54
+ @ Override
55
+ public JsonObject unmodifiable () {
56
+ final var mapCopy = new LinkedHashMap <String ,JsonNode >();
57
+ for (Map .Entry <String , JsonNode > entry : children .entrySet ()) {
58
+ mapCopy .put (entry .getKey (), entry .getValue ().unmodifiable ());
59
+ }
60
+ return JsonObject .of (mapCopy );
61
+ }
62
+
63
+ @ Override
64
+ public JsonObject copy () {
65
+ final var mapCopy = new LinkedHashMap <String ,JsonNode >();
66
+ for (Map .Entry <String , JsonNode > entry : children .entrySet ()) {
67
+ mapCopy .put (entry .getKey (), entry .getValue ().copy ());
68
+ }
69
+ return new JsonObject (mapCopy );
70
+ }
71
+
54
72
/**
55
73
* Return true if the json object contains no elements.
56
74
*/
Original file line number Diff line number Diff line change 1
1
package io .avaje .json .node ;
2
2
3
- public final class JsonString implements JsonNode {
3
+ public final /*value*/ class JsonString implements JsonNode {
4
4
5
5
private final String value ;
6
6
@@ -17,6 +17,16 @@ public String toString() {
17
17
return text ();
18
18
}
19
19
20
+ @ Override
21
+ public JsonString unmodifiable () {
22
+ return this ;
23
+ }
24
+
25
+ @ Override
26
+ public JsonString copy () {
27
+ return this ;
28
+ }
29
+
20
30
@ Override
21
31
public Type type () {
22
32
return Type .STRING ;
Original file line number Diff line number Diff line change 6
6
import java .util .stream .Collectors ;
7
7
8
8
import static org .assertj .core .api .Assertions .assertThat ;
9
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
9
10
10
11
class JsonArrayTest {
11
12
@@ -87,4 +88,31 @@ void add() {
87
88
assertThat (elements .get (4 )).isInstanceOf (JsonObject .class );
88
89
}
89
90
91
+ @ Test
92
+ void copy () {
93
+ final JsonArray source = JsonArray .create ()
94
+ .add ("foo" )
95
+ .add (JsonObject .create ().add ("b" , 42 ));
96
+
97
+ JsonArray copy = source .copy ();
98
+ assertThat (copy .toString ()).isEqualTo (source .toString ());
99
+
100
+ copy .add ("canMutate" );
101
+ assertThat (copy .size ()).isEqualTo (3 );
102
+ assertThat (source .size ()).isEqualTo (2 );
103
+ }
104
+
105
+ @ Test
106
+ void unmodifiable () {
107
+ final JsonArray source = JsonArray .create ()
108
+ .add ("foo" )
109
+ .add (JsonObject .create ().add ("b" , 42 ));
110
+
111
+ JsonArray copy = source .unmodifiable ();
112
+ assertThat (copy .toString ()).isEqualTo (source .toString ());
113
+
114
+ assertThatThrownBy (() -> copy .add ("canMutate" ))
115
+ .isInstanceOf (UnsupportedOperationException .class );
116
+ }
117
+
90
118
}
You can’t perform that action at this time.
0 commit comments