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

Initial add of avaje-json-node #297

Merged
merged 3 commits into from
Dec 11, 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
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>
78 changes: 78 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,78 @@
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));
}

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

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

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

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

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

/**
* Return the number of elements.
*/
public int size() {
return children.size();
}

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

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

}
34 changes: 34 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,34 @@
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 String toString() {
return text();
}

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

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

public boolean value() {
return value;
}

}
63 changes: 63 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,63 @@
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 String toString() {
return text();
}

@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);
}
}
63 changes: 63 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,63 @@
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 String toString() {
return text();
}

@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);
}
}
63 changes: 63 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,63 @@
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 String toString() {
return text();
}

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