Skip to content

Commit

Permalink
Initial add of avaje-json-node
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Dec 10, 2024
1 parent 939428e commit 27b8099
Show file tree
Hide file tree
Showing 28 changed files with 1,265 additions and 0 deletions.
31 changes: 31 additions & 0 deletions json-node/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-parent</artifactId>
<version>3.0-RC1</version>
</parent>

<artifactId>avaje-json-node</artifactId>

<dependencies>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-json</artifactId>
<version>3.0-RC1</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
66 changes: 66 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.avaje.json.node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.Objects.requireNonNull;

/**
* JSON Array type.
*/
public final class JsonArray implements JsonNode {

private final List<JsonNode> children;

/**
* Create a new JsonArray that can be added to.
*/
public static JsonArray create() {
return new JsonArray(new ArrayList<>());
}

/**
* Create an unmodifiable JsonArray with the given elements.
*/
public static JsonArray of(List<JsonNode> children) {
return new JsonArray(Collections.unmodifiableList(children));
}

JsonArray(List<JsonNode> children) {
this.children = requireNonNull(children);
}

@Override
public Type type() {
return Type.ARRAY;
}

@Override
public String text() {
return children.toString();
}

/**
* Return the child elements.
*/
public List<JsonNode> elements() {
return children;
}

/**
* Return true if the json array is empty.
*/
public boolean isEmpty() {
return children.isEmpty();
}

/**
* Add an element to the json array.
*/
public JsonArray add(JsonNode element) {
children.add(element);
return this;
}

}
29 changes: 29 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonBoolean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.avaje.json.node;

public final class JsonBoolean implements JsonNode {

private final boolean value;

public static JsonBoolean of(boolean value) {
return new JsonBoolean(value);
}

private JsonBoolean(boolean value) {
this.value = value;
}

@Override
public Type type() {
return Type.BOOLEAN;
}

@Override
public String text() {
return Boolean.toString(value);
}

public boolean value() {
return value;
}

}
58 changes: 58 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonDecimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.avaje.json.node;

import io.avaje.json.JsonWriter;

import java.math.BigDecimal;

public final class JsonDecimal implements JsonNumber {

private final BigDecimal value;

public static JsonDecimal of(BigDecimal value) {
return new JsonDecimal(value);
}

private JsonDecimal(BigDecimal value) {
this.value = value;
}

@Override
public Type type() {
return Type.NUMBER;
}

@Override
public String text() {
return value.toString();
}

@Override
public int intValue() {
return value.intValueExact();
}

@Override
public long longValue() {
return value.longValueExact();
}

@Override
public double doubleValue() {
return value.doubleValue();
}

@Override
public BigDecimal decimalValue() {
return value;
}

@Override
public Number numberValue() {
return value;
}

@Override
public void toJson(JsonWriter writer) {
writer.value(value);
}
}
58 changes: 58 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonDouble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.avaje.json.node;

import io.avaje.json.JsonWriter;

import java.math.BigDecimal;

public final class JsonDouble implements JsonNumber {

private final double value;

public static JsonDouble of(double value) {
return new JsonDouble(value);
}

private JsonDouble(double value) {
this.value = value;
}

@Override
public Type type() {
return Type.NUMBER;
}

@Override
public String text() {
return Double.toString(value);
}

@Override
public int intValue() {
return (int)value;
}

@Override
public long longValue() {
return (long)value;
}

@Override
public double doubleValue() {
return value;
}

@Override
public BigDecimal decimalValue() {
return BigDecimal.valueOf(this.value);
}

@Override
public Number numberValue() {
return value;
}

@Override
public void toJson(JsonWriter writer) {
writer.value(value);
}
}
58 changes: 58 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.avaje.json.node;

import io.avaje.json.JsonWriter;

import java.math.BigDecimal;

public final class JsonInteger implements JsonNumber {

private final int value;

public static JsonInteger of(int value) {
return new JsonInteger(value);
}

private JsonInteger(int value) {
this.value = value;
}

@Override
public Type type() {
return Type.NUMBER;
}

@Override
public String text() {
return Long.toString(value);
}

@Override
public int intValue() {
return value;
}

@Override
public long longValue() {
return value;
}

@Override
public double doubleValue() {
return value;
}

@Override
public BigDecimal decimalValue() {
return BigDecimal.valueOf(value);
}

@Override
public Number numberValue() {
return value;
}

@Override
public void toJson(JsonWriter writer) {
writer.value(value);
}
}
58 changes: 58 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonLong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.avaje.json.node;

import io.avaje.json.JsonWriter;

import java.math.BigDecimal;

public final class JsonLong implements JsonNumber {

private final long value;

public static JsonLong of(long value) {
return new JsonLong(value);
}

private JsonLong(long value) {
this.value = value;
}

@Override
public Type type() {
return Type.NUMBER;
}

@Override
public String text() {
return Long.toString(value);
}

@Override
public int intValue() {
return (int)value;
}

@Override
public long longValue() {
return value;
}

@Override
public double doubleValue() {
return value;
}

@Override
public BigDecimal decimalValue() {
return BigDecimal.valueOf(value);
}

@Override
public Number numberValue() {
return value;
}

@Override
public void toJson(JsonWriter writer) {
writer.value(value);
}
}
26 changes: 26 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.avaje.json.node;

public interface JsonNode {

/**
* Return the type of the node.
*/
Type type();

String text();

/**
* The types for JsonNode.
*/
enum Type {
NULL,
ARRAY,
OBJECT,
BOOLEAN,
STRING,
NUMBER,
// BINARY,
// MISSING,
// POJO
}
}
Loading

0 comments on commit 27b8099

Please sign in to comment.