-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[json-node] Make avaje-json-node an optional component for JsonB
That is, we need to explicitly include avaje-json-node in the classpath/module path. When JsonB starts with avaje-json-node present, it will register with JsonB thus support the JsonNode types in JsonB.
- Loading branch information
Showing
7 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
blackbox-test/src/test/java/org/example/other/JsonNodeAdaptersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.example.other; | ||
|
||
import io.avaje.json.node.JsonNode; | ||
import io.avaje.json.node.JsonObject; | ||
import io.avaje.jsonb.JsonType; | ||
import io.avaje.jsonb.Jsonb; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class JsonNodeAdaptersTest { | ||
|
||
static final Jsonb jsonb = Jsonb.builder().build(); | ||
static final JsonType<JsonNode> nodeType = jsonb.type(JsonNode.class); | ||
|
||
@Test | ||
void test() { | ||
var obj = JsonObject.create().add("name", "foo").add("val", 42); | ||
|
||
String asJson = nodeType.toJson(obj); | ||
assertThat(asJson).isEqualTo("{\"name\":\"foo\",\"val\":42}"); | ||
|
||
String asJson2 = jsonb.toJson(obj); | ||
assertThat(asJson2).isEqualTo(asJson); | ||
|
||
JsonNode fromJson = nodeType.fromJson(asJson); | ||
assertThat(fromJson).isInstanceOf(JsonObject.class); | ||
|
||
JsonObject objFromJson = (JsonObject) fromJson; | ||
assertThat(objFromJson.elements()).containsKeys("name", "val"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
json-node/src/main/java/io/avaje/json/node/adapter/JsonNodeComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.avaje.json.node.adapter; | ||
|
||
import io.avaje.json.JsonAdapter; | ||
import io.avaje.json.node.JsonNodeAdapter; | ||
import io.avaje.jsonb.AdapterFactory; | ||
import io.avaje.jsonb.Jsonb; | ||
import io.avaje.jsonb.spi.JsonbComponent; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Register with JsonB to support the JsonAdapters for JsonNode types. | ||
*/ | ||
public final class JsonNodeComponent implements JsonbComponent { | ||
|
||
@Override | ||
public void register(Jsonb.Builder builder) { | ||
builder.add(new JsonBFactory(JsonNodeAdapter.builder().build())); | ||
} | ||
|
||
private static final class JsonBFactory implements AdapterFactory { | ||
|
||
private final JsonNodeAdapter nodeAdapter; | ||
|
||
JsonBFactory(JsonNodeAdapter nodeAdapter) { | ||
this.nodeAdapter = nodeAdapter; | ||
} | ||
|
||
@Override | ||
public JsonAdapter<?> create(Type type, Jsonb jsonb) { | ||
return nodeAdapter.create(type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
json-node/src/main/resources/META-INF/services/io.avaje.jsonb.spi.JsonbExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.avaje.json.node.adapter.JsonNodeComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters