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] Make JsonNode Serializable #316

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
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);
}
}
Loading