Skip to content

Commit 2e2255d

Browse files
authored
Feature/json array add methods (#301)
* [json-node] Add extract() methods to assist filtering and mapping * [json-node] Add helper add() methods to JsonArray
1 parent 212f113 commit 2e2255d

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

json-node/src/main/java/io/avaje/json/node/JsonArray.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,32 @@ public JsonArray add(JsonNode element) {
8383
return this;
8484
}
8585

86+
/**
87+
* Add a String value.
88+
*/
89+
public JsonArray add(String value) {
90+
return add(JsonString.of(value));
91+
}
92+
93+
/**
94+
* Add a boolean value.
95+
*/
96+
public JsonArray add(boolean value) {
97+
return add(JsonBoolean.of(value));
98+
}
99+
100+
/**
101+
* Add a int value.
102+
*/
103+
public JsonArray add(int value) {
104+
return add(JsonInteger.of(value));
105+
}
106+
107+
/**
108+
* Add a long value.
109+
*/
110+
public JsonArray add(long value) {
111+
return add(JsonLong.of(value));
112+
}
113+
86114
}

json-node/src/test/java/io/avaje/json/node/JsonArrayTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,22 @@ void text() {
6969
assertThat(JsonArray.create().text()).isEqualTo("[]");
7070
assertThat(basicArray.text()).isEqualTo("[42, foo]");
7171
}
72+
73+
@Test
74+
void add() {
75+
JsonArray array = JsonArray.create()
76+
.add("string")
77+
.add(1).add(99L)
78+
.add(true)
79+
.add(JsonObject.create());
80+
81+
List<JsonNode> elements = array.elements();
82+
assertThat(elements).hasSize(5);
83+
assertThat(elements.get(0)).isInstanceOf(JsonString.class);
84+
assertThat(elements.get(1)).isInstanceOf(JsonInteger.class);
85+
assertThat(elements.get(2)).isInstanceOf(JsonLong.class);
86+
assertThat(elements.get(3)).isInstanceOf(JsonBoolean.class);
87+
assertThat(elements.get(4)).isInstanceOf(JsonObject.class);
88+
}
89+
7290
}

json-node/src/test/java/io/avaje/json/node/adapter/JsonNodeAdaptersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ void create_JsonString_expect_sameInstance() {
9898
@Test
9999
void arrayCreateOfMixed_defaultStream() {
100100
JsonArray jsonArray = JsonArray.create()
101-
.add(JsonInteger.of(42))
102-
.add(JsonString.of("foo"));
101+
.add(42)
102+
.add("foo");
103103

104104
var asJson = node.toJson(jsonArray);
105105
assertThat(asJson).isEqualTo("[42,\"foo\"]");

0 commit comments

Comments
 (0)