Skip to content

Commit

Permalink
Make more convenient for non-nominal usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Nov 17, 2024
1 parent 7677ea2 commit 002f053
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Requires Java 21+.
<dependency>
<groupId>dev.mccue</groupId>
<artifactId>json</artifactId>
<version>2023.12.23</version>
<version>2024.11.17</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion 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>2023.12.23</version>
<version>2024.11.17</version>
<packaging>jar</packaging>

<properties>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/dev/mccue/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
Expand Down
74 changes: 53 additions & 21 deletions src/main/java/dev/mccue/json/JsonDecodeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private static String indent(String string) {
return String.join("\n ", string.split("\n"));
}

private static String getMessageHelp(JsonDecodeException error, ArrayList<String> context) {
private static String getMessageHelp(JsonDecodeException error, ArrayList<String> context, boolean deep) {
switch (error) {
case AtField atField -> {
var fieldName = atField.fieldName();
Expand All @@ -175,31 +175,47 @@ private static String getMessageHelp(JsonDecodeException error, ArrayList<String

context.add(fieldName);

return getMessageHelp(err, context);
return getMessageHelp(err, context, deep);
}
case AtIndex atIndex -> {
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();
}
Expand All @@ -208,18 +224,34 @@ private static String getMessageHelp(JsonDecodeException error, ArrayList<String
var msg = failure.getMessage();
var json = failure.value;

var introduction = (
context.isEmpty()
? "Problem with the given value:\n\n "
: "Problem with the value at json" + String.join("", context) + ":\n\n "
);

return introduction + indent(Json.writeString(json, new JsonWriteOptions().withIndentation(4))) + "\n\n" + msg;

if (deep) {
var introduction = (
context.isEmpty()
? "Problem with the given value:\n\n "
: "Problem with the value at json" + String.join("", context) + ":\n\n "
);

return introduction + indent(Json.writeString(json, new JsonWriteOptions().withIndentation(4))) + "\n\n" + msg;
}
else {
var introduction = (
context.isEmpty()
? "Problem with the given value: "
: "Problem with the value at json" + String.join("", context) + ": "
);
return introduction + failure.getMessage();
}
}
}
}

public String getDetailedMessage() {
return getMessageHelp(this, new ArrayList<>(), true);
}

protected static String getMessage(JsonDecodeException error) {
return getMessageHelp(error, new ArrayList<>());
return getMessageHelp(error, new ArrayList<>(), false);
}
}
46 changes: 45 additions & 1 deletion src/main/java/dev/mccue/json/JsonDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ static java.lang.String string(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<String> string() {
return JsonDecoder::string;
}

static boolean boolean_(Json json) throws JsonDecodeException {
if (!(json instanceof JsonBoolean jsonBoolean)) {
throw JsonDecodeException.of(
Expand All @@ -68,6 +72,10 @@ static boolean boolean_(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<Boolean> boolean_() {
return JsonDecoder::boolean_;
}

static int int_(Json json) throws JsonDecodeException {
if (!(json instanceof JsonNumber jsonNumber)) {
throw JsonDecodeException.of(
Expand All @@ -91,6 +99,10 @@ static int int_(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<Integer> int_() {
return JsonDecoder::int_;
}

static long long_(Json json) throws JsonDecodeException {
if (!(json instanceof JsonNumber jsonNumber)) {
throw JsonDecodeException.of(
Expand All @@ -114,6 +126,10 @@ static long long_(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<Long> long_() {
return JsonDecoder::long_;
}

static float float_(Json json) throws JsonDecodeException {
if (!(json instanceof JsonNumber jsonNumber)) {
throw JsonDecodeException.of(
Expand All @@ -125,6 +141,10 @@ static float float_(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<Float> float_() {
return JsonDecoder::float_;
}

static double double_(Json json) throws JsonDecodeException {
if (!(json instanceof JsonNumber jsonNumber)) {
throw JsonDecodeException.of(
Expand All @@ -136,6 +156,10 @@ static double double_(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<Double> double_() {
return JsonDecoder::double_;
}

static BigInteger bigInteger(Json json) throws JsonDecodeException {
if (!(json instanceof JsonNumber jsonNumber)) {
throw JsonDecodeException.of(
Expand All @@ -154,6 +178,10 @@ static BigInteger bigInteger(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<BigInteger> bigInteger() {
return JsonDecoder::bigInteger;
}

static BigDecimal bigDecimal(Json json) throws JsonDecodeException {
if (!(json instanceof JsonNumber jsonNumber)) {
throw JsonDecodeException.of(
Expand All @@ -165,7 +193,11 @@ static BigDecimal bigDecimal(Json json) throws JsonDecodeException {
}
}

static <@Nullable T> T null_(Json json) throws JsonDecodeException {
static JsonDecoder<BigDecimal> bigDecimal() {
return JsonDecoder::bigDecimal;
}

static <T extends @Nullable Object> T null_(Json json) throws JsonDecodeException {
if (!(json instanceof JsonNull)) {
throw JsonDecodeException.of(
"expected null",
Expand All @@ -176,6 +208,10 @@ static BigDecimal bigDecimal(Json json) throws JsonDecodeException {
}
}

static <T extends @Nullable Object> JsonDecoder<T> null_() {
return JsonDecoder::null_;
}

static <T extends @Nullable Object> T null_(Json json, T value) throws JsonDecodeException {
if (!(json instanceof JsonNull)) {
throw JsonDecodeException.of(
Expand Down Expand Up @@ -204,6 +240,10 @@ static JsonArray array(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<JsonArray> array() {
return JsonDecoder::array;
}

static <T extends @Nullable Object> List<T> array(Json json, JsonDecoder<? extends T> itemDecoder) throws JsonDecodeException {
if (!(json instanceof JsonArray jsonArray)) {
throw JsonDecodeException.of(
Expand Down Expand Up @@ -237,6 +277,10 @@ static JsonObject object(Json json) throws JsonDecodeException {
}
}

static JsonDecoder<JsonObject> object() {
return JsonDecoder::object;
}

static <T extends @Nullable Object> JsonDecoder<Map<String, T>> object(JsonDecoder<? extends T> valueDecoder) throws JsonDecodeException {
return json -> object(json, valueDecoder);
}
Expand Down

0 comments on commit 002f053

Please sign in to comment.