Skip to content

Commit

Permalink
Merge pull request #53 from bowbahdoe/fix-overreading
Browse files Browse the repository at this point in the history
Add Nullability, Collectors, and fix overreading
  • Loading branch information
bowbahdoe authored Jul 26, 2023
2 parents 27b968f + 5e581f8 commit 26b7144
Show file tree
Hide file tree
Showing 30 changed files with 465 additions and 154 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

<properties>
Expand Down Expand Up @@ -42,6 +42,12 @@
</scm>

<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>0.3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -60,8 +66,8 @@
<source>17</source>
<target>17</target>
<compilerArgs>
<arg>-Werror</arg>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
Expand Down
84 changes: 60 additions & 24 deletions src/main/java/dev/mccue/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.mccue.json.internal.*;
import dev.mccue.json.stream.JsonStreamReadOptions;
import dev.mccue.json.stream.JsonValueHandler;
import org.jspecify.annotations.Nullable;

import java.io.*;
import java.math.BigDecimal;
Expand All @@ -11,6 +12,8 @@
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -41,6 +44,8 @@
* Unless otherwise specified, all factory methods in this interface are null-safe and will replace
* any actual nulls with {@link JsonNull}.
* </p>
*
* @author <a href="[email protected]">Ethan McCue</a>
*/
public sealed interface Json
extends Serializable, JsonEncodable
Expand All @@ -52,7 +57,7 @@ public sealed interface Json
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(JsonEncodable value) {
static Json of(@Nullable JsonEncodable value) {
if (value == null) {
return JsonNull.instance();
}
Expand All @@ -73,7 +78,7 @@ static Json of(JsonEncodable value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(BigDecimal value) {
static Json of(@Nullable BigDecimal value) {
return value == null ? JsonNull.instance() : new BigDecimalImpl(value);
}

Expand Down Expand Up @@ -123,7 +128,7 @@ static Json of(int value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(Double value) {
static Json of(@Nullable Double value) {
return value == null ? JsonNull.instance() : new DoubleImpl(value);
}

Expand All @@ -133,7 +138,7 @@ static Json of(Double value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(Long value) {
static Json of(@Nullable Long value) {
return value == null ? JsonNull.instance() : new LongImpl(value);
}

Expand All @@ -143,7 +148,7 @@ static Json of(Long value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(Float value) {
static Json of(@Nullable Float value) {
return value == null ? JsonNull.instance() : new DoubleImpl(value);
}

Expand All @@ -153,7 +158,7 @@ static Json of(Float value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(Integer value) {
static Json of(@Nullable Integer value) {
return value == null ? JsonNull.instance() : new LongImpl(value);
}

Expand All @@ -163,7 +168,7 @@ static Json of(Integer value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(BigInteger value) {
static Json of(@Nullable BigInteger value) {
return value == null ? JsonNull.instance() : new BigIntegerImpl(value);
}

Expand All @@ -173,7 +178,7 @@ static Json of(BigInteger value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(String value) {
static Json of(@Nullable String value) {
return value == null ? JsonNull.instance() : new StringImpl(value);
}

Expand Down Expand Up @@ -218,7 +223,7 @@ static Json of(boolean value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(Boolean value) {
static Json of(@Nullable Boolean value) {
return value == null ? JsonNull.instance() : JsonBoolean.of(value);
}

Expand All @@ -229,7 +234,7 @@ static Json of(Boolean value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(Collection<? extends JsonEncodable> value) {
static Json of(@Nullable Collection<? extends @Nullable JsonEncodable> value) {
return value == null
? JsonNull.instance()
: new ArrayImpl(
Expand All @@ -246,7 +251,10 @@ static Json of(Collection<? extends JsonEncodable> value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static <T> Json of(Collection<? extends T> value, JsonEncoder<T> encoder) {
static <T extends @Nullable Object> Json of(
@Nullable Collection<? extends T> value,
JsonEncoder<T> encoder
) {
return value == null
? JsonNull.instance()
: new ArrayImpl(
Expand Down Expand Up @@ -277,7 +285,7 @@ static <T> Json of(Collection<? extends T> value, JsonEncoder<T> encoder) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static Json of(Map<String, ? extends JsonEncodable> value) {
static Json of(@Nullable Map<String, ? extends @Nullable JsonEncodable> value) {
return value == null
? JsonNull.instance()
: new ObjectImpl(
Expand Down Expand Up @@ -305,7 +313,10 @@ static Json of(Map<String, ? extends JsonEncodable> value) {
* @param value The value to be encoded.
* @return An instance of {@link Json}.
*/
static <T> Json of(Map<String, ? extends T> value, JsonEncoder<T> encoder) {
static <T> Json of(
@Nullable Map<String, ? extends @Nullable T> value,
JsonEncoder<T> encoder
) {
return value == null
? JsonNull.instance()
: new ObjectImpl(
Expand Down Expand Up @@ -372,7 +383,7 @@ static JsonArray.Builder arrayBuilder() {
* @param elements A collection of elements which can be turned into {@link Json}.
* @return A new {@link JsonArray.Builder}.
*/
static JsonArray.Builder arrayBuilder(Collection<? extends JsonEncodable> elements) {
static JsonArray.Builder arrayBuilder(Collection<? extends @Nullable JsonEncodable> elements) {
if (elements instanceof JsonArray o) {
return new ArrayBuilderImpl(new ArrayList<>(o));
}
Expand Down Expand Up @@ -401,6 +412,32 @@ static JsonObject emptyObject() {
return ObjectImpl.EMPTY;
}

/**
* Returns a {@link Collector} which makes a {@link JsonArray} as a terminal {@link java.util.stream.Stream}
* operation.
*
* @return A {@link Collector}.
*/
static Collector<JsonEncodable, ?, JsonArray> arrayCollector() {
return JsonArray.collector();
}

/**
* Returns a {@link Collector} which makes a {@link JsonObject} as a terminal {@link java.util.stream.Stream}
* operation.
*
* @param keyMapper Function to extract a {@link String} from the stream.
* @param valueMapper Function to extract something {@link JsonEncodable} from the stream.
* @return A {@link Collector}.
* @param <T> The type of element stored in the stream.
*/
static <T extends @Nullable Object> Collector<T, ?, JsonObject> objectCollector(
Function<? super T, String> keyMapper,
Function<? super T, ? extends JsonEncodable> valueMapper
) {
return JsonObject.collector(keyMapper, valueMapper);
}

/**
* Vacuous implementation to make methods like {@link Json#of(java.util.Collection)}
* and {@link Json#of(java.util.Map)} convenient to create.
Expand Down Expand Up @@ -432,7 +469,7 @@ static Json readString(CharSequence jsonText) throws JsonReadException {
static Json readString(CharSequence jsonText, JsonReadOptions options) throws JsonReadException {
try {
return JsonReaderMethods.readFullyConsume(new PushbackReader(
new StringReader(jsonText.toString()), JsonReaderMethods.MINIMUM_PUSHBACK_BUFFER_SIZE
new StringReader(jsonText.toString())
), options);
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand All @@ -441,7 +478,7 @@ static Json readString(CharSequence jsonText, JsonReadOptions options) throws Js

static Json read(Reader reader, JsonReadOptions options) throws IOException, JsonReadException {
return JsonReaderMethods.readFullyConsume(
new PushbackReader(reader, JsonReaderMethods.MINIMUM_PUSHBACK_BUFFER_SIZE),
new PushbackReader(reader),
options
);
}
Expand All @@ -452,8 +489,7 @@ static Json read(Reader reader) throws IOException, JsonReadException {

static JsonReader reader(Reader reader, JsonReadOptions options) {
var pushbackReader = new PushbackReader(
reader,
JsonReaderMethods.MINIMUM_PUSHBACK_BUFFER_SIZE
reader
);
return () -> {
try {
Expand All @@ -471,7 +507,7 @@ static JsonReader reader(Reader reader) {

static void readStream(Reader reader, JsonValueHandler handler, JsonStreamReadOptions options) throws IOException, JsonReadException {
JsonReaderMethods.readStream(
new PushbackReader(reader, JsonReaderMethods.MINIMUM_PUSHBACK_BUFFER_SIZE),
new PushbackReader(reader),
false,
options,
handler
Expand All @@ -480,7 +516,7 @@ static void readStream(Reader reader, JsonValueHandler handler, JsonStreamReadOp

static void readStream(Reader reader, JsonValueHandler handler) throws IOException, JsonReadException {
JsonReaderMethods.readStream(
new PushbackReader(reader, JsonReaderMethods.MINIMUM_PUSHBACK_BUFFER_SIZE),
new PushbackReader(reader),
false,
new JsonStreamReadOptions(),
handler
Expand All @@ -501,11 +537,11 @@ private static String writeString(Json json, JsonWriteOptions options) {
return sw.toString();
}

static String writeString(JsonEncodable jsonEncodable) {
static String writeString(@Nullable JsonEncodable jsonEncodable) {
return writeString(Json.of(jsonEncodable));
}

static String writeString(JsonEncodable jsonEncodable, JsonWriteOptions options) {
static String writeString(@Nullable JsonEncodable jsonEncodable, JsonWriteOptions options) {
return writeString(Json.of(jsonEncodable), options);
}

Expand All @@ -517,11 +553,11 @@ private static void write(Json json, Writer writer) throws IOException {
new JsonWriter().write(json, writer, new JsonWriteOptions());
}

static void write(JsonEncodable jsonEncodable, Writer writer, JsonWriteOptions options) throws IOException {
static void write(@Nullable JsonEncodable jsonEncodable, Writer writer, JsonWriteOptions options) throws IOException {
write(Json.of(jsonEncodable), writer, options);
}

static void write(JsonEncodable jsonEncodable, Writer writer) throws IOException {
static void write(@Nullable JsonEncodable jsonEncodable, Writer writer) throws IOException {
write(Json.of(jsonEncodable), writer);
}
}
Loading

0 comments on commit 26b7144

Please sign in to comment.