Skip to content

Commit 3d20c79

Browse files
committed
Bump to java 21
1 parent c46c4af commit 3d20c79

File tree

16 files changed

+223
-453
lines changed

16 files changed

+223
-453
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

9-
Requires Java 17+.
9+
Requires Java 21+.
1010

1111
## Dependency Information
1212

@@ -16,15 +16,15 @@ Requires Java 17+.
1616
<dependency>
1717
<groupId>dev.mccue</groupId>
1818
<artifactId>json</artifactId>
19-
<version>0.2.4</version>
19+
<version>0.3.0</version>
2020
</dependency>
2121
```
2222

2323
### Gradle
2424

2525
```
2626
dependencies {
27-
implementation("dev.mccue:json:0.2.4")
27+
implementation("dev.mccue:json:0.3.0")
2828
}
2929
```
3030

pom.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
<groupId>dev.mccue</groupId>
88
<artifactId>json</artifactId>
9-
<version>0.2.4</version>
9+
<version>0.3.0</version>
1010
<packaging>jar</packaging>
1111

1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14-
<maven.compiler.source>17</maven.compiler.source>
15-
<maven.compiler.target>17</maven.compiler.target>
14+
<maven.compiler.source>21</maven.compiler.source>
15+
<maven.compiler.target>21</maven.compiler.target>
1616
</properties>
1717

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

2222
<developers>
@@ -63,10 +63,9 @@
6363
<artifactId>maven-compiler-plugin</artifactId>
6464
<version>3.8.1</version>
6565
<configuration>
66-
<source>17</source>
67-
<target>17</target>
66+
<source>21</source>
67+
<target>21</target>
6868
<compilerArgs>
69-
<arg>-Xlint:all</arg>
7069
<arg>-Werror</arg>
7170
</compilerArgs>
7271
</configuration>

src/main/java/dev/mccue/json/Json.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static Json of(@Nullable JsonEncodable value) {
7979
* @return An instance of {@link Json}.
8080
*/
8181
static Json of(@Nullable BigDecimal value) {
82-
return value == null ? JsonNull.instance() : new BigDecimalImpl(value);
82+
return value == null ? JsonNull.instance() : JsonNumber.of(value);
8383
}
8484

8585
/**
@@ -89,7 +89,7 @@ static Json of(@Nullable BigDecimal value) {
8989
* @return An instance of {@link Json}.
9090
*/
9191
static Json of(double value) {
92-
return new DoubleImpl(value);
92+
return JsonNumber.of(value);
9393
}
9494

9595
/**
@@ -99,7 +99,7 @@ static Json of(double value) {
9999
* @return An instance of {@link Json}.
100100
*/
101101
static Json of(long value) {
102-
return new LongImpl(value);
102+
return JsonNumber.of(value);
103103
}
104104

105105
/**
@@ -109,7 +109,7 @@ static Json of(long value) {
109109
* @return An instance of {@link Json}.
110110
*/
111111
static Json of(float value) {
112-
return new DoubleImpl(value);
112+
return JsonNumber.of(value);
113113
}
114114

115115
/**
@@ -119,7 +119,7 @@ static Json of(float value) {
119119
* @return An instance of {@link Json}.
120120
*/
121121
static Json of(int value) {
122-
return new LongImpl(value);
122+
return JsonNumber.of(value);
123123
}
124124

125125
/**
@@ -129,7 +129,7 @@ static Json of(int value) {
129129
* @return An instance of {@link Json}.
130130
*/
131131
static Json of(@Nullable Double value) {
132-
return value == null ? JsonNull.instance() : new DoubleImpl(value);
132+
return value == null ? JsonNull.instance() : of((double) value);
133133
}
134134

135135
/**
@@ -139,7 +139,7 @@ static Json of(@Nullable Double value) {
139139
* @return An instance of {@link Json}.
140140
*/
141141
static Json of(@Nullable Long value) {
142-
return value == null ? JsonNull.instance() : new LongImpl(value);
142+
return value == null ? JsonNull.instance() : of((long) value);
143143
}
144144

145145
/**
@@ -149,7 +149,7 @@ static Json of(@Nullable Long value) {
149149
* @return An instance of {@link Json}.
150150
*/
151151
static Json of(@Nullable Float value) {
152-
return value == null ? JsonNull.instance() : new DoubleImpl(value);
152+
return value == null ? JsonNull.instance() : of((float) value);
153153
}
154154

155155
/**
@@ -159,7 +159,7 @@ static Json of(@Nullable Float value) {
159159
* @return An instance of {@link Json}.
160160
*/
161161
static Json of(@Nullable Integer value) {
162-
return value == null ? JsonNull.instance() : new LongImpl(value);
162+
return value == null ? JsonNull.instance() : of((int) value);
163163
}
164164

165165
/**
@@ -169,7 +169,7 @@ static Json of(@Nullable Integer value) {
169169
* @return An instance of {@link Json}.
170170
*/
171171
static Json of(@Nullable BigInteger value) {
172-
return value == null ? JsonNull.instance() : new BigIntegerImpl(value);
172+
return value == null ? JsonNull.instance() : JsonNumber.of(value);
173173
}
174174

175175
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.mccue.json;
2+
3+
import dev.mccue.json.internal.JsonDecimalImpl;
4+
5+
public sealed abstract class JsonDecimal
6+
extends JsonNumber
7+
permits JsonDecimalImpl {
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.mccue.json;
2+
3+
import dev.mccue.json.internal.JsonIntegerImpl;
4+
5+
public sealed abstract class JsonInteger
6+
extends JsonNumber
7+
permits JsonIntegerImpl {
8+
}

src/main/java/dev/mccue/json/JsonNumber.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package dev.mccue.json;
22

3-
import dev.mccue.json.internal.BigDecimalImpl;
4-
import dev.mccue.json.internal.BigIntegerImpl;
5-
import dev.mccue.json.internal.DoubleImpl;
6-
import dev.mccue.json.internal.LongImpl;
3+
import dev.mccue.json.internal.JsonDecimalImpl;
4+
import dev.mccue.json.internal.JsonIntegerImpl;
75
import dev.mccue.json.stream.JsonGenerator;
86

97
import java.io.Serial;
@@ -16,8 +14,7 @@
1614
*/
1715
public sealed abstract class JsonNumber
1816
extends Number
19-
implements Json
20-
permits BigDecimalImpl, DoubleImpl, LongImpl, BigIntegerImpl {
17+
implements Json permits JsonDecimal, JsonInteger {
2118
@Serial
2219
private static final long serialVersionUID = 1L;
2320

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

3633
public abstract boolean isIntegral();
3734

38-
public static JsonNumber of(BigDecimal value) {
39-
return new BigDecimalImpl(value);
35+
public static JsonDecimal of(BigDecimal value) {
36+
return new JsonDecimalImpl(value.toString());
4037
}
4138

42-
public static JsonNumber of(double value) {
43-
return new DoubleImpl(value);
39+
public static JsonDecimal of(double value) {
40+
if (Double.isInfinite(value)) {
41+
throw new IllegalArgumentException("JSON cannot encode an infinite value");
42+
}
43+
if (Double.isNaN(value)) {
44+
throw new IllegalArgumentException("JSON cannot encode a NaN");
45+
}
46+
return new JsonDecimalImpl(BigDecimal.valueOf(value).toString());
4447
}
4548

46-
public static JsonNumber of(long value) {
47-
return new LongImpl(value);
49+
public static JsonInteger of(long value) {
50+
return new JsonIntegerImpl(BigDecimal.valueOf(value).toString());
4851
}
4952

50-
public static JsonNumber of(float value) {
51-
return new DoubleImpl(value);
53+
public static JsonDecimal of(float value) {
54+
if (Float.isInfinite(value)) {
55+
throw new IllegalArgumentException("JSON cannot encode an infinite value");
56+
}
57+
if (Float.isNaN(value)) {
58+
throw new IllegalArgumentException("JSON cannot encode a NaN");
59+
}
60+
return new JsonDecimalImpl(BigDecimal.valueOf(value).toString());
5261
}
5362

54-
public static JsonNumber of(int value) {
55-
return new LongImpl(value);
63+
public static JsonInteger of(int value) {
64+
return new JsonIntegerImpl(BigDecimal.valueOf(value).toString());
5665
}
5766

58-
public static JsonNumber of(java.math.BigInteger value) {
59-
return new BigIntegerImpl(value);
67+
public static JsonInteger of(java.math.BigInteger value) {
68+
return new JsonIntegerImpl(new BigDecimal(value).toString());
6069
}
6170

6271
@Override

src/main/java/dev/mccue/json/JsonReadOptions.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,22 @@
66
* Options for customizing the process of reading Json.
77
*
88
* @param eofBehavior What to do if an attempted read reaches an EOF without any Json being read.
9-
* @param useBigDecimals Whether to use BigDecimals when reading decimal numbers.
109
*
1110
* @author <a href="ethan@mccue.dev">Ethan McCue</a>
1211
*/
1312
public record JsonReadOptions(
14-
EOFBehavior eofBehavior,
15-
boolean useBigDecimals
13+
EOFBehavior eofBehavior
1614
) {
1715
public JsonReadOptions {
1816
Objects.requireNonNull(eofBehavior, "eofBehavior must not be null");
1917
}
2018

2119
public JsonReadOptions() {
22-
this(EOFBehavior.THROW_EXCEPTION, false);
20+
this(EOFBehavior.THROW_EXCEPTION);
2321
}
2422

2523
public JsonReadOptions withEOFBehavior(EOFBehavior eofBehavior) {
26-
return new JsonReadOptions(eofBehavior, useBigDecimals);
27-
}
28-
29-
public JsonReadOptions withUseBigDecimals(boolean useBigDecimals) {
30-
return new JsonReadOptions(eofBehavior, useBigDecimals);
24+
return new JsonReadOptions(eofBehavior);
3125
}
3226

3327
/**

src/main/java/dev/mccue/json/internal/BigDecimalImpl.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)