Skip to content

Commit

Permalink
Bump to java 21
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Dec 19, 2023
1 parent c46c4af commit 3d20c79
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 453 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A Java JSON Library intended to be easy to learn and simple to teach.

Requires Java 17+.
Requires Java 21+.

## Dependency Information

Expand All @@ -16,15 +16,15 @@ Requires Java 17+.
<dependency>
<groupId>dev.mccue</groupId>
<artifactId>json</artifactId>
<version>0.2.4</version>
<version>0.3.0</version>
</dependency>
```

### Gradle

```
dependencies {
implementation("dev.mccue:json:0.2.4")
implementation("dev.mccue:json:0.3.0")
}
```

Expand Down
13 changes: 6 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

<groupId>dev.mccue</groupId>
<artifactId>json</artifactId>
<version>0.2.4</version>
<version>0.3.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>

<name>json</name>
<description>Dead simple Java JSON API based on sealed interfaces.</description>
<description>A Java JSON Library intended to be easy to learn and simple to teach.</description>
<url>https://github.com/bowbahdoe/json</url>

<developers>
Expand Down Expand Up @@ -63,10 +63,9 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<source>21</source>
<target>21</target>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/dev/mccue/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static Json of(@Nullable JsonEncodable value) {
* @return An instance of {@link Json}.
*/
static Json of(@Nullable BigDecimal value) {
return value == null ? JsonNull.instance() : new BigDecimalImpl(value);
return value == null ? JsonNull.instance() : JsonNumber.of(value);
}

/**
Expand All @@ -89,7 +89,7 @@ static Json of(@Nullable BigDecimal value) {
* @return An instance of {@link Json}.
*/
static Json of(double value) {
return new DoubleImpl(value);
return JsonNumber.of(value);
}

/**
Expand All @@ -99,7 +99,7 @@ static Json of(double value) {
* @return An instance of {@link Json}.
*/
static Json of(long value) {
return new LongImpl(value);
return JsonNumber.of(value);
}

/**
Expand All @@ -109,7 +109,7 @@ static Json of(long value) {
* @return An instance of {@link Json}.
*/
static Json of(float value) {
return new DoubleImpl(value);
return JsonNumber.of(value);
}

/**
Expand All @@ -119,7 +119,7 @@ static Json of(float value) {
* @return An instance of {@link Json}.
*/
static Json of(int value) {
return new LongImpl(value);
return JsonNumber.of(value);
}

/**
Expand All @@ -129,7 +129,7 @@ static Json of(int value) {
* @return An instance of {@link Json}.
*/
static Json of(@Nullable Double value) {
return value == null ? JsonNull.instance() : new DoubleImpl(value);
return value == null ? JsonNull.instance() : of((double) value);
}

/**
Expand All @@ -139,7 +139,7 @@ static Json of(@Nullable Double value) {
* @return An instance of {@link Json}.
*/
static Json of(@Nullable Long value) {
return value == null ? JsonNull.instance() : new LongImpl(value);
return value == null ? JsonNull.instance() : of((long) value);
}

/**
Expand All @@ -149,7 +149,7 @@ static Json of(@Nullable Long value) {
* @return An instance of {@link Json}.
*/
static Json of(@Nullable Float value) {
return value == null ? JsonNull.instance() : new DoubleImpl(value);
return value == null ? JsonNull.instance() : of((float) value);
}

/**
Expand All @@ -159,7 +159,7 @@ static Json of(@Nullable Float value) {
* @return An instance of {@link Json}.
*/
static Json of(@Nullable Integer value) {
return value == null ? JsonNull.instance() : new LongImpl(value);
return value == null ? JsonNull.instance() : of((int) value);
}

/**
Expand All @@ -169,7 +169,7 @@ static Json of(@Nullable Integer value) {
* @return An instance of {@link Json}.
*/
static Json of(@Nullable BigInteger value) {
return value == null ? JsonNull.instance() : new BigIntegerImpl(value);
return value == null ? JsonNull.instance() : JsonNumber.of(value);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/mccue/json/JsonDecimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.mccue.json;

import dev.mccue.json.internal.JsonDecimalImpl;

public sealed abstract class JsonDecimal
extends JsonNumber
permits JsonDecimalImpl {
}
8 changes: 8 additions & 0 deletions src/main/java/dev/mccue/json/JsonInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.mccue.json;

import dev.mccue.json.internal.JsonIntegerImpl;

public sealed abstract class JsonInteger
extends JsonNumber
permits JsonIntegerImpl {
}
45 changes: 27 additions & 18 deletions src/main/java/dev/mccue/json/JsonNumber.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package dev.mccue.json;

import dev.mccue.json.internal.BigDecimalImpl;
import dev.mccue.json.internal.BigIntegerImpl;
import dev.mccue.json.internal.DoubleImpl;
import dev.mccue.json.internal.LongImpl;
import dev.mccue.json.internal.JsonDecimalImpl;
import dev.mccue.json.internal.JsonIntegerImpl;
import dev.mccue.json.stream.JsonGenerator;

import java.io.Serial;
Expand All @@ -16,8 +14,7 @@
*/
public sealed abstract class JsonNumber
extends Number
implements Json
permits BigDecimalImpl, DoubleImpl, LongImpl, BigIntegerImpl {
implements Json permits JsonDecimal, JsonInteger {
@Serial
private static final long serialVersionUID = 1L;

Expand All @@ -35,28 +32,40 @@ protected JsonNumber() {}

public abstract boolean isIntegral();

public static JsonNumber of(BigDecimal value) {
return new BigDecimalImpl(value);
public static JsonDecimal of(BigDecimal value) {
return new JsonDecimalImpl(value.toString());
}

public static JsonNumber of(double value) {
return new DoubleImpl(value);
public static JsonDecimal of(double value) {
if (Double.isInfinite(value)) {
throw new IllegalArgumentException("JSON cannot encode an infinite value");
}
if (Double.isNaN(value)) {
throw new IllegalArgumentException("JSON cannot encode a NaN");
}
return new JsonDecimalImpl(BigDecimal.valueOf(value).toString());
}

public static JsonNumber of(long value) {
return new LongImpl(value);
public static JsonInteger of(long value) {
return new JsonIntegerImpl(BigDecimal.valueOf(value).toString());
}

public static JsonNumber of(float value) {
return new DoubleImpl(value);
public static JsonDecimal of(float value) {
if (Float.isInfinite(value)) {
throw new IllegalArgumentException("JSON cannot encode an infinite value");
}
if (Float.isNaN(value)) {
throw new IllegalArgumentException("JSON cannot encode a NaN");
}
return new JsonDecimalImpl(BigDecimal.valueOf(value).toString());
}

public static JsonNumber of(int value) {
return new LongImpl(value);
public static JsonInteger of(int value) {
return new JsonIntegerImpl(BigDecimal.valueOf(value).toString());
}

public static JsonNumber of(java.math.BigInteger value) {
return new BigIntegerImpl(value);
public static JsonInteger of(java.math.BigInteger value) {
return new JsonIntegerImpl(new BigDecimal(value).toString());
}

@Override
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/dev/mccue/json/JsonReadOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@
* Options for customizing the process of reading Json.
*
* @param eofBehavior What to do if an attempted read reaches an EOF without any Json being read.
* @param useBigDecimals Whether to use BigDecimals when reading decimal numbers.
*
* @author <a href="ethan@mccue.dev">Ethan McCue</a>
*/
public record JsonReadOptions(
EOFBehavior eofBehavior,
boolean useBigDecimals
EOFBehavior eofBehavior
) {
public JsonReadOptions {
Objects.requireNonNull(eofBehavior, "eofBehavior must not be null");
}

public JsonReadOptions() {
this(EOFBehavior.THROW_EXCEPTION, false);
this(EOFBehavior.THROW_EXCEPTION);
}

public JsonReadOptions withEOFBehavior(EOFBehavior eofBehavior) {
return new JsonReadOptions(eofBehavior, useBigDecimals);
}

public JsonReadOptions withUseBigDecimals(boolean useBigDecimals) {
return new JsonReadOptions(eofBehavior, useBigDecimals);
return new JsonReadOptions(eofBehavior);
}

/**
Expand Down
102 changes: 0 additions & 102 deletions src/main/java/dev/mccue/json/internal/BigDecimalImpl.java

This file was deleted.

Loading

0 comments on commit 3d20c79

Please sign in to comment.