Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[json-node] Use SimpleMapper.Type interface to replace NodeMapper #319

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,31 +48,31 @@ static Builder builder() {
* @param <T> The specific custom type being mapped.
* @return The type specific NodeMapper.
*/
<T> NodeMapper<T> mapper(JsonAdapter<T> customAdapter);
<T> SimpleMapper.Type<T> mapper(JsonAdapter<T> customAdapter);

/**
* Return a NodeMapper for ANY json content.
* <p>
* The NodeMapper provides support for all reading and writing options
* such as InputStream, OutputStream, Reader, Writer etc.
*/
NodeMapper<JsonNode> nodeMapper();
SimpleMapper.Type<JsonNode> nodeMapper();

/**
* Return a NodeMapper for json OBJECT content.
* <p>
* The NodeMapper provides support for all reading and writing options
* such as InputStream, OutputStream, Reader, Writer etc.
*/
NodeMapper<JsonObject> objectMapper();
SimpleMapper.Type<JsonObject> objectMapper();

/**
* Return a NodeMapper for json ARRAY content.
* <p>
* The NodeMapper provides support for all reading and writing options
* such as InputStream, OutputStream, Reader, Writer etc.
*/
NodeMapper<JsonArray> arrayMapper();
SimpleMapper.Type<JsonArray> arrayMapper();

/**
* Write the node to JSON string.
Expand All @@ -89,25 +90,25 @@ static Builder builder() {
* var asJson = mapper.toJson(jsonArray);
* }</pre>
*
* @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);

/**
* Read any json content returning a JsonNode.
* <p>
* 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}.
*
* <pre>{@code
* static final JsonNodeMapper mapper = JsonNodeMapper.builder().build();
*
* JsonNode nodeFromJson = mapper.fromJson(jsonContent);
* }</pre>
*
* @see NodeMapper#fromJson(Reader)
* @see NodeMapper#fromJson(InputStream)
* @see SimpleMapper.Type#fromJson(Reader)
* @see SimpleMapper.Type#fromJson(InputStream)
*/
JsonNode fromJson(String json);

Expand Down
78 changes: 0 additions & 78 deletions json-node/src/main/java/io/avaje/json/node/NodeMapper.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,22 +31,22 @@ final class DJsonNodeMapper implements JsonNodeMapper {
}

@Override
public <T> NodeMapper<T> mapper(JsonAdapter<T> customAdapter) {
public <T> SimpleMapper.Type<T> mapper(JsonAdapter<T> customAdapter) {
return new DMapper<>(customAdapter, jsonStream);
}

@Override
public NodeMapper<JsonNode> nodeMapper() {
public SimpleMapper.Type<JsonNode> nodeMapper() {
return new DMapper<>(nodeAdapter, jsonStream);
}

@Override
public NodeMapper<JsonObject> objectMapper() {
public SimpleMapper.Type<JsonObject> objectMapper() {
return new DMapper<>(objectAdapter, jsonStream);
}

@Override
public NodeMapper<JsonArray> arrayMapper() {
public SimpleMapper.Type<JsonArray> arrayMapper() {
return new DMapper<>(arrayAdapter, jsonStream);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> implements NodeMapper<T> {
final class DMapper<T> implements SimpleMapper.Type<T> {

private final JsonAdapter<T> adapter;
private final JsonStream jsonStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +17,7 @@ void mapUsingCustomAdapter() {

MyAdapter myAdapter = new MyAdapter(mapper);

NodeMapper<MyCustomType> typeMapper = mapper.mapper(myAdapter);
SimpleMapper.Type<MyCustomType> typeMapper = mapper.mapper(myAdapter);

MyCustomType source = new MyCustomType();
source.foo = "hi";
Expand All @@ -36,7 +37,7 @@ static class MyCustomType {

static class MyAdapter implements JsonAdapter<MyCustomType> {

final NodeMapper<JsonObject> objectMapper;
final SimpleMapper.Type<JsonObject> objectMapper;

public MyAdapter(JsonNodeMapper mapper) {
this.objectMapper = mapper.objectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,7 +13,7 @@
class NodeMapperTest {

static final JsonNodeMapper mapper = JsonNodeMapper.builder().build();
static final NodeMapper<JsonNode> nodeMapper = mapper.nodeMapper();
static final SimpleMapper.Type<JsonNode> nodeMapper = mapper.nodeMapper();

@Test
void toJson() {
Expand Down Expand Up @@ -75,15 +76,15 @@ void toJsonUsingJsonWriter() {

@Test
void objectMapper() {
NodeMapper<JsonObject> objectMapper = mapper.objectMapper();
SimpleMapper.Type<JsonObject> objectMapper = mapper.objectMapper();

JsonObject jsonObject = objectMapper.fromJson("{\"greet\":\"hi\"}");
assertThat(jsonObject.toString()).isEqualTo("{greet=hi}");
}

@Test
void arrayMapper() {
NodeMapper<JsonArray> arrayMapper = mapper.arrayMapper();
SimpleMapper.Type<JsonArray> arrayMapper = mapper.arrayMapper();

JsonArray jsonArray = arrayMapper.fromJson("[\"a\",\"b\",\"c\"]");
assertThat(jsonArray.toString()).isEqualTo("[a, b, c]");
Expand Down
Loading