Skip to content

Commit c3bf890

Browse files
authored
nullsafe json node (#455)
1 parent 01d50c1 commit c3bf890

File tree

4 files changed

+119
-9
lines changed

4 files changed

+119
-9
lines changed

json-node/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
<dependency>
3333
<groupId>io.avaje</groupId>
3434
<artifactId>avaje-json-core</artifactId>
35-
<version>3.9-SNAPSHOT</version>
35+
<version>${project.version}</version>
3636
</dependency>
3737

3838
<dependency>
3939
<groupId>io.avaje</groupId>
4040
<artifactId>avaje-jsonb</artifactId>
41-
<version>3.0-RC1</version>
41+
<version>${project.version}</version>
4242
<optional>true</optional>
4343
</dependency>
4444

@@ -48,7 +48,6 @@
4848
<version>1.6</version>
4949
<scope>test</scope>
5050
</dependency>
51-
5251
</dependencies>
5352

5453
</project>

json-node/src/main/java/io/avaje/json/node/adapter/DJsonNodeMapper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ final class DJsonNodeMapper implements JsonNodeMapper {
2222
static final NumberAdapter NUMBER_ADAPTER = new NumberAdapter();
2323

2424
private final JsonStream jsonStream;
25-
private final NodeAdapter nodeAdapter;
26-
private final ObjectAdapter objectAdapter;
27-
private final ArrayAdapter arrayAdapter;
25+
private final JsonAdapter<JsonNode> nodeAdapter;
26+
private final JsonAdapter<JsonObject> objectAdapter;
27+
private final JsonAdapter<JsonArray> arrayAdapter;
2828

2929
DJsonNodeMapper(JsonStream jsonStream, NodeAdapter nodeAdapter, ObjectAdapter objectAdapter, ArrayAdapter arrayAdapter) {
3030
this.jsonStream = jsonStream;
31-
this.nodeAdapter = nodeAdapter;
32-
this.objectAdapter = objectAdapter;
33-
this.arrayAdapter = arrayAdapter;
31+
this.nodeAdapter = nodeAdapter.nullSafe();
32+
this.objectAdapter = objectAdapter.nullSafe();
33+
this.arrayAdapter = arrayAdapter.nullSafe();
3434
}
3535

3636
@Override
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.avaje.json.node.adapter;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import io.avaje.json.node.JsonObject;
9+
import io.avaje.jsonb.Jsonb;
10+
11+
class NullJsonObjectTest {
12+
static class WrapJsonObject {
13+
public JsonObject json;
14+
}
15+
16+
@Test
17+
void test() {
18+
var b = Jsonb.builder();
19+
new JsonNodeComponent().register(b);
20+
var type =
21+
b.add(WrapJsonObject.class, WrapJsonObjectJsonAdapter::new)
22+
.build()
23+
.type(WrapJsonObject.class);
24+
WrapJsonObject fromJson = type.fromJson("{\"json\":null}");
25+
assertNull(fromJson.json);
26+
assertEquals("{}", type.toJson(fromJson));
27+
}
28+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.avaje.json.node.adapter;
2+
3+
import java.lang.invoke.MethodHandle;
4+
5+
import io.avaje.json.JsonAdapter;
6+
import io.avaje.json.JsonReader;
7+
import io.avaje.json.JsonWriter;
8+
import io.avaje.json.PropertyNames;
9+
import io.avaje.json.node.JsonObject;
10+
import io.avaje.json.node.adapter.NullJsonObjectTest.WrapJsonObject;
11+
import io.avaje.json.view.ViewBuilder;
12+
import io.avaje.json.view.ViewBuilderAware;
13+
import io.avaje.jsonb.Jsonb;
14+
import io.avaje.jsonb.spi.Generated;
15+
16+
@Generated("io.avaje.jsonb.generator")
17+
public final class WrapJsonObjectJsonAdapter implements JsonAdapter<WrapJsonObject>, ViewBuilderAware {
18+
19+
// naming convention Match
20+
// json [io.avaje.json.node.JsonObject] name:json publicField
21+
22+
private final JsonAdapter<JsonObject> jsonObjectJsonAdapter;
23+
private final PropertyNames names;
24+
25+
public WrapJsonObjectJsonAdapter(Jsonb jsonb) {
26+
this.jsonObjectJsonAdapter = jsonb.adapter(JsonObject.class);
27+
this.names = jsonb.properties("json");
28+
}
29+
30+
@Override
31+
public void toJson(JsonWriter writer, WrapJsonObject _wrapJsonObject) {
32+
writer.beginObject(names);
33+
writer.name(0);
34+
jsonObjectJsonAdapter.toJson(writer, _wrapJsonObject.json);
35+
writer.endObject();
36+
}
37+
38+
@Override
39+
public WrapJsonObject fromJson(JsonReader reader) {
40+
// variables to read json values into, constructor params don't need _set$ flags
41+
JsonObject _val$json = null; boolean _set$json = false;
42+
43+
// read json
44+
reader.beginObject(names);
45+
while (reader.hasNextField()) {
46+
final String fieldName = reader.nextField();
47+
switch (fieldName) {
48+
case "json":
49+
_val$json = jsonObjectJsonAdapter.fromJson(reader);
50+
_set$json = true;
51+
break;
52+
53+
default:
54+
reader.unmappedField(fieldName);
55+
reader.skipValue();
56+
}
57+
}
58+
reader.endObject();
59+
60+
// build and return WrapJsonObject
61+
WrapJsonObject _$wrapJsonObject = new WrapJsonObject();
62+
if (_set$json) _$wrapJsonObject.json = _val$json;
63+
return _$wrapJsonObject;
64+
}
65+
66+
@SuppressWarnings("unchecked")
67+
@Override
68+
public boolean isViewBuilderAware() {
69+
return true;
70+
}
71+
72+
@Override
73+
public ViewBuilderAware viewBuild() {
74+
return this;
75+
}
76+
77+
@Override
78+
public void build(ViewBuilder builder, String name, MethodHandle handle) {
79+
builder.beginObject(name, handle);
80+
builder.add("json", jsonObjectJsonAdapter, builder.field(WrapJsonObject.class, "json"));
81+
builder.endObject();
82+
}
83+
}

0 commit comments

Comments
 (0)