From 923b137f8893b63da92f49c5232691d2f78a390a Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Sat, 23 Dec 2023 13:43:15 -0500 Subject: [PATCH] Fix issues in JsonGenerator --- .../dev/mccue/json/JsonDecodeException.java | 102 +++++++++--------- .../dev/mccue/json/stream/JsonGenerator.java | 43 ++------ 2 files changed, 60 insertions(+), 85 deletions(-) diff --git a/src/main/java/dev/mccue/json/JsonDecodeException.java b/src/main/java/dev/mccue/json/JsonDecodeException.java index 69e16ff..454f464 100644 --- a/src/main/java/dev/mccue/json/JsonDecodeException.java +++ b/src/main/java/dev/mccue/json/JsonDecodeException.java @@ -156,70 +156,66 @@ private static String indent(String string) { } private static String getMessageHelp(JsonDecodeException error, ArrayList context) { - if (error instanceof AtField atField) { - var fieldName = atField.fieldName(); - var err = atField.error(); - - boolean isSimple; - if (fieldName.isEmpty()) { - isSimple = false; - } - else { - isSimple = Character.isAlphabetic(fieldName.charAt(0)); - for (int i = 1; i < fieldName.length(); i++) { - isSimple = isSimple && (Character.isAlphabetic(fieldName.charAt(i)) || Character.isDigit(fieldName.charAt(i))); + switch (error) { + case AtField atField -> { + var fieldName = atField.fieldName(); + var err = atField.error(); + + boolean isSimple; + if (fieldName.isEmpty()) { + isSimple = false; + } else { + isSimple = Character.isAlphabetic(fieldName.charAt(0)); + for (int i = 1; i < fieldName.length(); i++) { + isSimple = isSimple && (Character.isAlphabetic(fieldName.charAt(i)) || Character.isDigit(fieldName.charAt(i))); + } } - } - fieldName = isSimple ? "." + fieldName : "[" + fieldName + "]"; + fieldName = isSimple ? "." + fieldName : "[" + fieldName + "]"; - context.add(fieldName); + context.add(fieldName); - return getMessageHelp(err, context); - } - else if (error instanceof AtIndex atIndex) { - var indexName = "[" + atIndex.index() + "]"; - context.add(indexName); - return getMessageHelp(atIndex.error(), context); - } - else if (error instanceof OneOf oneOf) { - if (oneOf.errors().isEmpty()) { - return "Ran into oneOf with no possibilities" + (context.isEmpty() ? "!" : " at json" + String.join("", context)); + return getMessageHelp(err, context); } - else if (oneOf.errors().size() == 1) { - return getMessageHelp(oneOf.errors().get(0), context); + case AtIndex atIndex -> { + var indexName = "[" + atIndex.index() + "]"; + context.add(indexName); + return getMessageHelp(atIndex.error(), context); } - 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"); + 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); + } 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"); + } } - } - return msg.toString(); + return msg.toString(); + } } - } - else if (error instanceof Failure failure) { - var msg = failure.getMessage(); - var json = failure.value; + case Failure failure -> { + 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 " - ); + 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 { - throw new IllegalStateException(); + return introduction + indent(Json.writeString(json, new JsonWriteOptions().withIndentation(4))) + "\n\n" + msg; + } } } diff --git a/src/main/java/dev/mccue/json/stream/JsonGenerator.java b/src/main/java/dev/mccue/json/stream/JsonGenerator.java index a569cce..d6aaec9 100644 --- a/src/main/java/dev/mccue/json/stream/JsonGenerator.java +++ b/src/main/java/dev/mccue/json/stream/JsonGenerator.java @@ -27,38 +27,17 @@ public interface JsonGenerator { void writeNull(); default void write(JsonEvent event) { - if (event instanceof JsonEvent.ObjectStart) { - this.writeObjectStart(); - } - else if (event instanceof JsonEvent.ObjectEnd) { - this.writeObjectEnd(); - } - else if (event instanceof JsonEvent.ArrayStart) { - this.writeArrayStart(); - } - else if (event instanceof JsonEvent.ArrayEnd) { - this.writeArrayEnd(); - } - else if (event instanceof JsonEvent.Field field) { - this.writeFieldName(field.name()); - } - else if (event instanceof JsonEvent.String) { - this.writeNull(); - } - else if (event instanceof JsonEvent.Number) { - this.writeNull(); - } - else if (event instanceof JsonEvent.True) { - this.writeTrue(); - } - else if (event instanceof JsonEvent.False) { - this.writeFalse(); - } - else if (event instanceof JsonEvent.Null) { - this.writeNull(); - } - else { - throw new IllegalStateException(); + switch (event) { + case JsonEvent.ObjectStart __ -> this.writeObjectStart(); + case JsonEvent.ObjectEnd __ -> this.writeObjectEnd(); + case JsonEvent.ArrayStart __ -> this.writeArrayStart(); + case JsonEvent.ArrayEnd __ -> this.writeArrayEnd(); + case JsonEvent.Field field -> this.writeFieldName(field.name()); + case JsonEvent.String string -> this.writeString(string.value()); + case JsonEvent.Number number -> this.writeNumber(number.value()); + case JsonEvent.True __ -> this.writeTrue(); + case JsonEvent.False __ -> this.writeFalse(); + case JsonEvent.Null __ -> this.writeNull(); } }