diff --git a/README.md b/README.md index 5607f08..74c7dde 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Requires Java 21+. dev.mccue json - 2023.12.23 + 2024.11.17 ``` diff --git a/pom.xml b/pom.xml index cfe33ce..282d815 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.mccue json - 2023.12.23 + 2024.11.17 jar diff --git a/src/main/java/dev/mccue/json/Json.java b/src/main/java/dev/mccue/json/Json.java index 486f421..d0a78bd 100644 --- a/src/main/java/dev/mccue/json/Json.java +++ b/src/main/java/dev/mccue/json/Json.java @@ -476,6 +476,14 @@ static Json readString(CharSequence jsonText, JsonReadOptions options) throws Js } } + static Json read(CharSequence jsonText) throws JsonReadException { + return readString(jsonText); + } + + static Json read(CharSequence jsonText, JsonReadOptions options) throws JsonReadException { + return readString(jsonText, options); + } + static Json read(Reader reader, JsonReadOptions options) throws IOException, JsonReadException { return JsonReaderMethods.readFullyConsume( new PushbackReader(reader), @@ -545,6 +553,14 @@ static String writeString(@Nullable JsonEncodable jsonEncodable, JsonWriteOption return writeString(Json.of(jsonEncodable), options); } + static String write(@Nullable JsonEncodable jsonEncodable) { + return writeString(jsonEncodable); + } + + static String write(@Nullable JsonEncodable jsonEncodable, JsonWriteOptions options) { + return writeString(jsonEncodable, options); + } + private static void write(Json json, Writer writer, JsonWriteOptions options) throws IOException { new JsonWriter().write(json, writer, options); } diff --git a/src/main/java/dev/mccue/json/JsonDecodeException.java b/src/main/java/dev/mccue/json/JsonDecodeException.java index 454f464..a0089ea 100644 --- a/src/main/java/dev/mccue/json/JsonDecodeException.java +++ b/src/main/java/dev/mccue/json/JsonDecodeException.java @@ -155,7 +155,7 @@ private static String indent(String string) { return String.join("\n ", string.split("\n")); } - private static String getMessageHelp(JsonDecodeException error, ArrayList context) { + private static String getMessageHelp(JsonDecodeException error, ArrayList context, boolean deep) { switch (error) { case AtField atField -> { var fieldName = atField.fieldName(); @@ -175,31 +175,47 @@ private static String getMessageHelp(JsonDecodeException error, ArrayList { var indexName = "[" + atIndex.index() + "]"; context.add(indexName); - return getMessageHelp(atIndex.error(), context); + return getMessageHelp(atIndex.error(), context, deep); } case OneOf oneOf -> { if (oneOf.errors().isEmpty()) { return "Ran into oneOf with no possibilities" + (context.isEmpty() ? "!" : " at json" + String.join("", context)); } else if (oneOf.errors().size() == 1) { - return getMessageHelp(oneOf.errors().get(0), context); + return getMessageHelp(oneOf.errors().get(0), context, deep); } else { - var starter = (context.isEmpty() ? "oneOf" : "oneOf at json" + String.join("", context)); - var introduction = starter + " failed in the following " + oneOf.errors().size() + " ways:"; - var msg = new StringBuilder(introduction + "\n\n"); - for (int i = 0; i < oneOf.errors().size(); i++) { - msg.append("\n\n("); - msg.append(i + 1); - msg.append(") "); - msg.append(indent(getMessage(oneOf.errors().get(i)))); - if (i != oneOf.errors().size() - 1) { - msg.append("\n\n"); + var msg = new StringBuilder(); + if (deep) { + var starter = (context.isEmpty() ? "oneOf" : "oneOf at json" + String.join("", context)); + var introduction = starter + " failed in the following " + oneOf.errors().size() + " ways:"; + msg.append(introduction); + msg.append("\n\n"); + for (int i = 0; i < oneOf.errors().size(); i++) { + msg.append("\n\n("); + msg.append(i + 1); + msg.append(") "); + msg.append(indent(getMessage(oneOf.errors().get(i)))); + if (i != oneOf.errors().size() - 1) { + msg.append("\n\n"); + } } } + else { + var starter = (context.isEmpty() ? "oneOf" : "oneOf at json" + String.join("", context)); + var introduction = starter + " failed in the following " + oneOf.errors().size() + " ways:"; + msg.append(introduction); + for (int i = 0; i < oneOf.errors().size(); i++) { + msg.append("\n - ("); + msg.append(i + 1); + msg.append(") "); + msg.append(getMessage(oneOf.errors().get(i))); + } + } + return msg.toString(); } @@ -208,18 +224,34 @@ private static String getMessageHelp(JsonDecodeException error, ArrayList(), true); + } + protected static String getMessage(JsonDecodeException error) { - return getMessageHelp(error, new ArrayList<>()); + return getMessageHelp(error, new ArrayList<>(), false); } } diff --git a/src/main/java/dev/mccue/json/JsonDecoder.java b/src/main/java/dev/mccue/json/JsonDecoder.java index 9904290..ff668d4 100644 --- a/src/main/java/dev/mccue/json/JsonDecoder.java +++ b/src/main/java/dev/mccue/json/JsonDecoder.java @@ -57,6 +57,10 @@ static java.lang.String string(Json json) throws JsonDecodeException { } } + static JsonDecoder string() { + return JsonDecoder::string; + } + static boolean boolean_(Json json) throws JsonDecodeException { if (!(json instanceof JsonBoolean jsonBoolean)) { throw JsonDecodeException.of( @@ -68,6 +72,10 @@ static boolean boolean_(Json json) throws JsonDecodeException { } } + static JsonDecoder boolean_() { + return JsonDecoder::boolean_; + } + static int int_(Json json) throws JsonDecodeException { if (!(json instanceof JsonNumber jsonNumber)) { throw JsonDecodeException.of( @@ -91,6 +99,10 @@ static int int_(Json json) throws JsonDecodeException { } } + static JsonDecoder int_() { + return JsonDecoder::int_; + } + static long long_(Json json) throws JsonDecodeException { if (!(json instanceof JsonNumber jsonNumber)) { throw JsonDecodeException.of( @@ -114,6 +126,10 @@ static long long_(Json json) throws JsonDecodeException { } } + static JsonDecoder long_() { + return JsonDecoder::long_; + } + static float float_(Json json) throws JsonDecodeException { if (!(json instanceof JsonNumber jsonNumber)) { throw JsonDecodeException.of( @@ -125,6 +141,10 @@ static float float_(Json json) throws JsonDecodeException { } } + static JsonDecoder float_() { + return JsonDecoder::float_; + } + static double double_(Json json) throws JsonDecodeException { if (!(json instanceof JsonNumber jsonNumber)) { throw JsonDecodeException.of( @@ -136,6 +156,10 @@ static double double_(Json json) throws JsonDecodeException { } } + static JsonDecoder double_() { + return JsonDecoder::double_; + } + static BigInteger bigInteger(Json json) throws JsonDecodeException { if (!(json instanceof JsonNumber jsonNumber)) { throw JsonDecodeException.of( @@ -154,6 +178,10 @@ static BigInteger bigInteger(Json json) throws JsonDecodeException { } } + static JsonDecoder bigInteger() { + return JsonDecoder::bigInteger; + } + static BigDecimal bigDecimal(Json json) throws JsonDecodeException { if (!(json instanceof JsonNumber jsonNumber)) { throw JsonDecodeException.of( @@ -165,7 +193,11 @@ static BigDecimal bigDecimal(Json json) throws JsonDecodeException { } } - static <@Nullable T> T null_(Json json) throws JsonDecodeException { + static JsonDecoder bigDecimal() { + return JsonDecoder::bigDecimal; + } + + static T null_(Json json) throws JsonDecodeException { if (!(json instanceof JsonNull)) { throw JsonDecodeException.of( "expected null", @@ -176,6 +208,10 @@ static BigDecimal bigDecimal(Json json) throws JsonDecodeException { } } + static JsonDecoder null_() { + return JsonDecoder::null_; + } + static T null_(Json json, T value) throws JsonDecodeException { if (!(json instanceof JsonNull)) { throw JsonDecodeException.of( @@ -204,6 +240,10 @@ static JsonArray array(Json json) throws JsonDecodeException { } } + static JsonDecoder array() { + return JsonDecoder::array; + } + static List array(Json json, JsonDecoder itemDecoder) throws JsonDecodeException { if (!(json instanceof JsonArray jsonArray)) { throw JsonDecodeException.of( @@ -237,6 +277,10 @@ static JsonObject object(Json json) throws JsonDecodeException { } } + static JsonDecoder object() { + return JsonDecoder::object; + } + static JsonDecoder> object(JsonDecoder valueDecoder) throws JsonDecodeException { return json -> object(json, valueDecoder); }