Skip to content

Commit

Permalink
[json-core] Add helper toJson() fromJson() methods that use JsonReade…
Browse files Browse the repository at this point in the history
…r JsonWriter

This makes it more trivially obvious when using SimpleMapper or JsonNodeMapper to implement a JsonAdapter [where the API uses JsonReader and JsonWriter]
  • Loading branch information
rbygrave committed Dec 17, 2024
1 parent c30df3b commit 32e5b07
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 1 deletion.
22 changes: 22 additions & 0 deletions json-core/src/main/java/io/avaje/json/simple/DSimpleMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.avaje.json.simple;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.PropertyNames;
import io.avaje.json.core.CoreTypes;
import io.avaje.json.stream.JsonStream;
Expand Down Expand Up @@ -52,11 +54,26 @@ public String toJson(Object object) {
return objectType.toJson(object);
}

@Override
public void toJson(Object object, JsonWriter jsonWriter) {
objectType.toJson(object, jsonWriter);
}

@Override
public Object fromJson(String json) {
return objectType.fromJson(json);
}

@Override
public Object fromJson(JsonReader jsonReader) {
return objectType.fromJson(jsonReader);
}

@Override
public Map<String, Object> fromJsonObject(JsonReader jsonReader) {
return mapType.fromJson(jsonReader);
}

@Override
public Map<String,Object> fromJsonObject(String json) {
return mapType.fromJson(json);
Expand All @@ -66,4 +83,9 @@ public Map<String,Object> fromJsonObject(String json) {
public List<Object> fromJsonArray(String json) {
return listType.fromJson(json);
}

@Override
public List<Object> fromJsonArray(JsonReader jsonReader) {
return listType.fromJson(jsonReader);
}
}
27 changes: 27 additions & 0 deletions json-core/src/main/java/io/avaje/json/simple/SimpleMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,52 @@ static Builder builder() {
*/
String toJson(Object object);

/**
* Write the object to JsonWriter.
* <p>
* For options to write json content to OutputStream, Writer etc
* use {@link Type}.
*/
void toJson(Object object, JsonWriter jsonWriter);

/**
* Read the object from JSON string.
*/
Object fromJson(String json);

/**
* Read the object from JSON.
*/
Object fromJson(JsonReader jsonReader);

/**
* Read a Map from JSON OBJECT string.
* <p>
* Use {@link #map()} for more reading options.
*/
Map<String, Object> fromJsonObject(String json);

/**
* Read a Map from JSON OBJECT.
* <p>
* Use {@link #map()} for more reading options.
*/
Map<String, Object> fromJsonObject(JsonReader jsonReader);

/**
* Read a List from JSON ARRAY string.
* <p>
* Use {@link #list()} for more reading options.
*/
List<Object> fromJsonArray(String json);

/**
* Read a List from JSON ARRAY.
* <p>
* Use {@link #list()} for more reading options.
*/
List<Object> fromJsonArray(JsonReader jsonReader);

/**
* Return the property names as PropertyNames.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.avaje.json.simple;


import io.avaje.json.JsonReader;
import io.avaje.json.stream.BufferedJsonWriter;
import io.avaje.json.stream.JsonStream;
import org.junit.jupiter.api.Test;

import java.util.LinkedHashMap;
Expand Down Expand Up @@ -30,14 +33,52 @@ void mapToJsonFromJson() {

Map<String, Object> mapFromJson2 = simpleMapper.map().fromJson(asJson);
assertThat(mapFromJson2).isEqualTo(mapFromJson);

JsonStream jsonStream = JsonStream.builder().build();
try (JsonReader reader = jsonStream.reader(asJson)) {
Map<String, Object> mapFromJson3 = simpleMapper.fromJsonObject(reader);
assertThat(mapFromJson3).isEqualTo(mapFromJson);
}
}

@Test
void toJsonWriter_scalar() {
JsonStream jsonStream = JsonStream.builder().build();
BufferedJsonWriter writer0 = jsonStream.bufferedWriter();
simpleMapper.toJson("hi", writer0);
assertThat(writer0.result()).isEqualTo("\"hi\"");
}

@Test
void toJsonWriter_map() {
JsonStream jsonStream = JsonStream.builder().build();
BufferedJsonWriter writer0 = jsonStream.bufferedWriter();
simpleMapper.toJson(Map.of("key", 0), writer0);
assertThat(writer0.result()).isEqualTo("{\"key\":0}");
}

@Test
void toJsonWriter_list() {
JsonStream jsonStream = JsonStream.builder().build();
BufferedJsonWriter writer0 = jsonStream.bufferedWriter();
simpleMapper.toJson(List.of("a", 0), writer0);
assertThat(writer0.result()).isEqualTo("[\"a\",0]");
}

@Test
void nullDirectly() {
var mapFromJson = simpleMapper.fromJson("null");
var mapFromJson = simpleMapper.fromJson("null");
assertThat(mapFromJson).isNull();
}

@Test
void objectJsonReader() {
try (var reader = JsonStream.builder().build().reader("\"hi\"")) {
var fromJson = simpleMapper.fromJson(reader);
assertThat(fromJson).isEqualTo("hi");
}
}

@Test
void mapWithNull() {
Map<String, Object> mapFromJson = simpleMapper.fromJsonObject("{\"one\":1,\"two\":null,\"three\":3}");
Expand All @@ -58,6 +99,16 @@ void listWithNull() {
assertThat(simpleMapper.toJson(listFromJson)).isEqualTo("[1,3]");
}

@Test
void listWithReader() {
try (JsonReader reader = JsonStream.builder().build().reader("[1,2]")) {
List<Object> listFromJson = simpleMapper.fromJsonArray(reader);

assertThat(listFromJson).hasSize(2);
assertThat(simpleMapper.toJson(listFromJson)).isEqualTo("[1,2]");
}
}

@Test
void arrayToJsonFromJson() {

Expand Down
38 changes: 38 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonNodeMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.avaje.json.node;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.PropertyNames;
import io.avaje.json.node.adapter.NodeAdapterBuilder;
import io.avaje.json.simple.SimpleMapper;
Expand Down Expand Up @@ -87,6 +89,17 @@ static Builder builder() {
*/
String toJson(JsonNode node);

/**
* Write the node to JSON.
* <p>
* For options to write json content to OutputStream, Writer etc
* use {@link #nodeMapper()}.
*
* @see SimpleMapper.Type#toJson(Object, OutputStream)
* @see SimpleMapper.Type#toJson(Object, Writer)
*/
void toJson(JsonNode node, JsonWriter jsonWriter);

/**
* Read any json content returning a JsonNode.
* <p>
Expand All @@ -104,6 +117,17 @@ static Builder builder() {
*/
JsonNode fromJson(String json);

/**
* Read any json content returning a JsonNode.
* <p>
* For options to read json content from InputStream, Reader etc
* use the fromJson methods on {@link SimpleMapper.Type}.
*
* @see SimpleMapper.Type#fromJson(Reader)
* @see SimpleMapper.Type#fromJson(InputStream)
*/
JsonNode fromJson(JsonReader jsonReader);

/**
* Read a JsonObject from json string content.
* <p>
Expand All @@ -114,6 +138,13 @@ static Builder builder() {
*/
JsonObject fromJsonObject(String json);

/**
* Read a JsonObject from json string content.
* <p>
* Use this when we know that the json content is a JsonObject.
*/
JsonObject fromJsonObject(JsonReader jsonReader);

/**
* Read a JsonArray from json string content.
* <p>
Expand All @@ -124,6 +155,13 @@ static Builder builder() {
*/
JsonArray fromJsonArray(String json);

/**
* Read a JsonArray from json string content.
* <p>
* Use this when we know that the json content is a JsonArray.
*/
JsonArray fromJsonArray(JsonReader jsonReader);

/**
* Helper method to read JSON with an expected JsonNode type.
*
Expand Down
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.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.PropertyNames;
import io.avaje.json.node.*;
import io.avaje.json.simple.SimpleMapper;
Expand Down Expand Up @@ -63,6 +64,11 @@ public String toJson(JsonNode node) {
return writer.result();
}

@Override
public void toJson(JsonNode node, JsonWriter jsonWriter) {
nodeAdapter.toJson(jsonWriter, node);
}

@Override
public JsonNode fromJson(String json) {
try (JsonReader reader = jsonStream.reader(json)) {
Expand All @@ -84,6 +90,21 @@ public JsonArray fromJsonArray(String json) {
}
}

@Override
public JsonNode fromJson(JsonReader jsonReader) {
return nodeAdapter.fromJson(jsonReader);
}

@Override
public JsonObject fromJsonObject(JsonReader jsonReader) {
return objectAdapter.fromJson(jsonReader);
}

@Override
public JsonArray fromJsonArray(JsonReader jsonReader) {
return arrayAdapter.fromJson(jsonReader);
}

@Override
public <T extends JsonNode> T fromJson(Class<T> type, String json) {
JsonAdapter<T> adapter = adapter(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.avaje.json.JsonReader;
import io.avaje.json.node.*;
import io.avaje.json.simple.SimpleMapper;
import io.avaje.json.stream.BufferedJsonWriter;
import io.avaje.json.stream.JsonStream;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -102,6 +103,36 @@ void create_JsonString_expect_sameInstance() {
assertThat(jsonAdapter).isSameAs(adapter);
}

@Test
void toJsonWriter() {
BufferedJsonWriter writer = stream.bufferedWriter();
mapper.toJson(JsonArray.create().add(1).add(2), writer);
assertThat(writer.result()).isEqualTo("[1,2]");
}

@Test
void fromJson_usingReader() {
try (var reader = stream.reader("[42, \"foo\"]")) {
JsonNode node = mapper.fromJson(reader);
assertThat(node).isEqualTo(JsonArray.create().add(42L).add("foo"));
}
}

@Test
void fromJsonArray_usingReader() {
try (var reader = stream.reader("[42, \"foo\"]")) {
JsonArray node = mapper.fromJsonArray(reader);
assertThat(node).isEqualTo(JsonArray.create().add(42L).add("foo"));
}
}

@Test
void fromJsonObject_usingReader() {
try (var reader = stream.reader("{\"a\":1,\"b\":2}")) {
JsonObject node = mapper.fromJsonObject(reader);
assertThat(node).isEqualTo(JsonObject.create().add("a", 1L).add("b", 2L));
}
}

@Test
void arrayCreateOfMixed_defaultStream() {
Expand Down

0 comments on commit 32e5b07

Please sign in to comment.