From 652cbfee69c8c86c3b47c37dc2cf1435f7aa4d8e Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 16 Dec 2024 18:02:19 +1300 Subject: [PATCH] [json-node] Use SimpleMapper.Type interface to replace NodeMapper --- .../io/avaje/json/node/JsonNodeMapper.java | 19 ++--- .../java/io/avaje/json/node/NodeMapper.java | 78 ------------------- .../json/node/adapter/DJsonNodeMapper.java | 9 ++- .../io/avaje/json/node/adapter/DMapper.java | 5 +- .../io/avaje/json/node/CustomAdapterTest.java | 5 +- .../io/avaje/json/node/NodeMapperTest.java | 7 +- 6 files changed, 24 insertions(+), 99 deletions(-) delete mode 100644 json-node/src/main/java/io/avaje/json/node/NodeMapper.java diff --git a/json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java b/json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java index 69c003ed..66ccf1c5 100644 --- a/json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java +++ b/json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java @@ -2,6 +2,7 @@ import io.avaje.json.JsonAdapter; import io.avaje.json.node.adapter.NodeAdapterBuilder; +import io.avaje.json.simple.SimpleMapper; import io.avaje.json.stream.JsonStream; import java.io.InputStream; @@ -47,7 +48,7 @@ static Builder builder() { * @param The specific custom type being mapped. * @return The type specific NodeMapper. */ - NodeMapper mapper(JsonAdapter customAdapter); + SimpleMapper.Type mapper(JsonAdapter customAdapter); /** * Return a NodeMapper for ANY json content. @@ -55,7 +56,7 @@ static Builder builder() { * The NodeMapper provides support for all reading and writing options * such as InputStream, OutputStream, Reader, Writer etc. */ - NodeMapper nodeMapper(); + SimpleMapper.Type nodeMapper(); /** * Return a NodeMapper for json OBJECT content. @@ -63,7 +64,7 @@ static Builder builder() { * The NodeMapper provides support for all reading and writing options * such as InputStream, OutputStream, Reader, Writer etc. */ - NodeMapper objectMapper(); + SimpleMapper.Type objectMapper(); /** * Return a NodeMapper for json ARRAY content. @@ -71,7 +72,7 @@ static Builder builder() { * The NodeMapper provides support for all reading and writing options * such as InputStream, OutputStream, Reader, Writer etc. */ - NodeMapper arrayMapper(); + SimpleMapper.Type arrayMapper(); /** * Write the node to JSON string. @@ -89,8 +90,8 @@ static Builder builder() { * var asJson = mapper.toJson(jsonArray); * } * - * @see NodeMapper#toJson(Object, OutputStream) - * @see NodeMapper#toJson(Object, Writer) + * @see SimpleMapper.Type#toJson(Object, OutputStream) + * @see SimpleMapper.Type#toJson(Object, Writer) */ String toJson(JsonNode node); @@ -98,7 +99,7 @@ static Builder builder() { * Read any json content returning a JsonNode. *

* For options to read json content from InputStream, Reader etc - * use the fromJson methods on {@link NodeMapper}. + * use the fromJson methods on {@link SimpleMapper.Type}. * *

{@code
    * static final JsonNodeMapper mapper = JsonNodeMapper.builder().build();
@@ -106,8 +107,8 @@ static Builder builder() {
    * JsonNode nodeFromJson = mapper.fromJson(jsonContent);
    * }
* - * @see NodeMapper#fromJson(Reader) - * @see NodeMapper#fromJson(InputStream) + * @see SimpleMapper.Type#fromJson(Reader) + * @see SimpleMapper.Type#fromJson(InputStream) */ JsonNode fromJson(String json); diff --git a/json-node/src/main/java/io/avaje/json/node/NodeMapper.java b/json-node/src/main/java/io/avaje/json/node/NodeMapper.java deleted file mode 100644 index e2ecd527..00000000 --- a/json-node/src/main/java/io/avaje/json/node/NodeMapper.java +++ /dev/null @@ -1,78 +0,0 @@ -package io.avaje.json.node; - -import io.avaje.json.JsonReader; -import io.avaje.json.JsonWriter; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; -import java.io.Writer; - -/** - * Mappers for JsonNode, JsonArray and JsonObject. - *

- * This supports more options for reading and writing json content - * such as InputStream, OutputStream, Reader, Writer etc. - * - * @see JsonNodeMapper#arrayMapper() - * @see JsonNodeMapper#objectMapper() - * @see JsonNodeMapper#nodeMapper() - */ -public interface NodeMapper { - - /** - * Read the return the value from the json content. - */ - T fromJson(String content); - - /** - * Read the return the value from the reader. - */ - T fromJson(JsonReader reader); - - /** - * Read the return the value from the json content. - */ - T fromJson(byte[] content); - - /** - * Read the return the value from the reader. - */ - T fromJson(Reader reader); - - /** - * Read the return the value from the inputStream. - */ - T fromJson(InputStream inputStream); - - /** - * Return as json string. - */ - String toJson(T value); - - /** - * Return as json string in pretty format. - */ - String toJsonPretty(T value); - - /** - * Return the value as json content in bytes form. - */ - byte[] toJsonBytes(T value); - - /** - * Write to the given writer. - */ - void toJson(T value, JsonWriter writer); - - /** - * Write to the given writer. - */ - void toJson(T value, Writer writer); - - /** - * Write to the given outputStream. - */ - void toJson(T value, OutputStream outputStream); - -} diff --git a/json-node/src/main/java/io/avaje/json/node/adapter/DJsonNodeMapper.java b/json-node/src/main/java/io/avaje/json/node/adapter/DJsonNodeMapper.java index 5a07bb5d..ead34bb8 100644 --- a/json-node/src/main/java/io/avaje/json/node/adapter/DJsonNodeMapper.java +++ b/json-node/src/main/java/io/avaje/json/node/adapter/DJsonNodeMapper.java @@ -3,6 +3,7 @@ import io.avaje.json.JsonAdapter; import io.avaje.json.JsonReader; import io.avaje.json.node.*; +import io.avaje.json.simple.SimpleMapper; import io.avaje.json.stream.JsonStream; import java.lang.reflect.Type; @@ -30,22 +31,22 @@ final class DJsonNodeMapper implements JsonNodeMapper { } @Override - public NodeMapper mapper(JsonAdapter customAdapter) { + public SimpleMapper.Type mapper(JsonAdapter customAdapter) { return new DMapper<>(customAdapter, jsonStream); } @Override - public NodeMapper nodeMapper() { + public SimpleMapper.Type nodeMapper() { return new DMapper<>(nodeAdapter, jsonStream); } @Override - public NodeMapper objectMapper() { + public SimpleMapper.Type objectMapper() { return new DMapper<>(objectAdapter, jsonStream); } @Override - public NodeMapper arrayMapper() { + public SimpleMapper.Type arrayMapper() { return new DMapper<>(arrayAdapter, jsonStream); } diff --git a/json-node/src/main/java/io/avaje/json/node/adapter/DMapper.java b/json-node/src/main/java/io/avaje/json/node/adapter/DMapper.java index 0119c4cd..172b127a 100644 --- a/json-node/src/main/java/io/avaje/json/node/adapter/DMapper.java +++ b/json-node/src/main/java/io/avaje/json/node/adapter/DMapper.java @@ -4,15 +4,14 @@ import io.avaje.json.JsonException; import io.avaje.json.JsonReader; import io.avaje.json.JsonWriter; -import io.avaje.json.node.JsonNode; -import io.avaje.json.node.NodeMapper; +import io.avaje.json.simple.SimpleMapper; import io.avaje.json.stream.BufferedJsonWriter; import io.avaje.json.stream.BytesJsonWriter; import io.avaje.json.stream.JsonStream; import java.io.*; -final class DMapper implements NodeMapper { +final class DMapper implements SimpleMapper.Type { private final JsonAdapter adapter; private final JsonStream jsonStream; diff --git a/json-node/src/test/java/io/avaje/json/node/CustomAdapterTest.java b/json-node/src/test/java/io/avaje/json/node/CustomAdapterTest.java index d00b60a1..d2d60e96 100644 --- a/json-node/src/test/java/io/avaje/json/node/CustomAdapterTest.java +++ b/json-node/src/test/java/io/avaje/json/node/CustomAdapterTest.java @@ -3,6 +3,7 @@ import io.avaje.json.JsonAdapter; import io.avaje.json.JsonReader; import io.avaje.json.JsonWriter; +import io.avaje.json.simple.SimpleMapper; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -16,7 +17,7 @@ void mapUsingCustomAdapter() { MyAdapter myAdapter = new MyAdapter(mapper); - NodeMapper typeMapper = mapper.mapper(myAdapter); + SimpleMapper.Type typeMapper = mapper.mapper(myAdapter); MyCustomType source = new MyCustomType(); source.foo = "hi"; @@ -36,7 +37,7 @@ static class MyCustomType { static class MyAdapter implements JsonAdapter { - final NodeMapper objectMapper; + final SimpleMapper.Type objectMapper; public MyAdapter(JsonNodeMapper mapper) { this.objectMapper = mapper.objectMapper(); diff --git a/json-node/src/test/java/io/avaje/json/node/NodeMapperTest.java b/json-node/src/test/java/io/avaje/json/node/NodeMapperTest.java index bc8ce2e5..d7960390 100644 --- a/json-node/src/test/java/io/avaje/json/node/NodeMapperTest.java +++ b/json-node/src/test/java/io/avaje/json/node/NodeMapperTest.java @@ -1,6 +1,7 @@ package io.avaje.json.node; import io.avaje.json.JsonWriter; +import io.avaje.json.simple.SimpleMapper; import io.avaje.json.stream.JsonStream; import org.junit.jupiter.api.Test; @@ -12,7 +13,7 @@ class NodeMapperTest { static final JsonNodeMapper mapper = JsonNodeMapper.builder().build(); - static final NodeMapper nodeMapper = mapper.nodeMapper(); + static final SimpleMapper.Type nodeMapper = mapper.nodeMapper(); @Test void toJson() { @@ -75,7 +76,7 @@ void toJsonUsingJsonWriter() { @Test void objectMapper() { - NodeMapper objectMapper = mapper.objectMapper(); + SimpleMapper.Type objectMapper = mapper.objectMapper(); JsonObject jsonObject = objectMapper.fromJson("{\"greet\":\"hi\"}"); assertThat(jsonObject.toString()).isEqualTo("{greet=hi}"); @@ -83,7 +84,7 @@ void objectMapper() { @Test void arrayMapper() { - NodeMapper arrayMapper = mapper.arrayMapper(); + SimpleMapper.Type arrayMapper = mapper.arrayMapper(); JsonArray jsonArray = arrayMapper.fromJson("[\"a\",\"b\",\"c\"]"); assertThat(jsonArray.toString()).isEqualTo("[a, b, c]");