Skip to content

Commit

Permalink
[json-node] Make JsonNode Serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Dec 16, 2024
1 parent d532632 commit 6d81286
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
public final class JsonArray implements JsonNode {

private static final long serialVersionUID = 1L;

private static final JsonArray EMPTY = new JsonArray(Collections.emptyList());

private final List<JsonNode> children;
Expand Down
2 changes: 2 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public final /*value*/ class JsonBoolean implements JsonNode {

private static final long serialVersionUID = 1L;

private final boolean value;

public static JsonBoolean of(boolean value) {
Expand Down
2 changes: 2 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

public final /*value*/ class JsonDecimal implements JsonNumber {

private static final long serialVersionUID = 1L;

private final BigDecimal value;

public static JsonDecimal of(BigDecimal value) {
Expand Down
2 changes: 2 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

public final /*value*/ class JsonDouble implements JsonNumber {

private static final long serialVersionUID = 1L;

private final double value;

public static JsonDouble of(double value) {
Expand Down
2 changes: 2 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

public final /*value*/ class JsonInteger implements JsonNumber {

private static final long serialVersionUID = 1L;

private final int value;

public static JsonInteger of(int value) {
Expand Down
2 changes: 2 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

public final /*value*/ class JsonLong implements JsonNumber {

private static final long serialVersionUID = 1L;

private final long value;

public static JsonLong of(long value) {
Expand Down
4 changes: 3 additions & 1 deletion json-node/src/main/java/io/avaje/json/node/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.jspecify.annotations.Nullable;

import java.io.Serializable;

/**
* Represents the core JSON types.
*/
public /*sealed*/ interface JsonNode
public /*sealed*/ interface JsonNode extends Serializable
/*permits JsonArray, JsonObject, JsonBoolean, JsonString, JsonNumber*/ {

/**
Expand Down
1 change: 1 addition & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
public final class JsonObject implements JsonNode {

private static final long serialVersionUID = 1L;
private static final JsonObject EMPTY = new JsonObject(Collections.emptyMap());
private static final Pattern PATH_PATTERN = Pattern.compile("\\.");

Expand Down
2 changes: 2 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonString.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public final /*value*/ class JsonString implements JsonNode {

private static final long serialVersionUID = 1L;

private final String value;

public static JsonString of(String value) {
Expand Down
37 changes: 37 additions & 0 deletions json-node/src/test/java/io/avaje/json/node/SerializeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.avaje.json.node;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.math.BigDecimal;
import static org.assertj.core.api.Assertions.assertThat;

class SerializeTest {

@Test
void test() throws IOException, ClassNotFoundException {

Boolean.valueOf(true);
final var source = JsonArray.create()
.add("foo")
.add(JsonObject.create()
.add("aStr", "a")
.add("aBool", true)
.add("aInt", 42)
.add("aLong", 420L)
.add("aDouble", JsonDouble.of(4204.3D))
.add("aDecimal", JsonDecimal.of(BigDecimal.TEN))
);

var baos = new ByteArrayOutputStream();
try (var oos = new ObjectOutputStream(baos)) {
oos.writeObject(source);
}

var ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
var readO = ois.readObject();

assertThat(readO).isInstanceOf(JsonArray.class);
assertThat(readO).isEqualTo(source);
}
}

0 comments on commit 6d81286

Please sign in to comment.