Skip to content

Commit

Permalink
Fix issues in JsonGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Dec 23, 2023
1 parent 2688837 commit 923b137
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 85 deletions.
102 changes: 49 additions & 53 deletions src/main/java/dev/mccue/json/JsonDecodeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,70 +156,66 @@ private static String indent(String string) {
}

private static String getMessageHelp(JsonDecodeException error, ArrayList<String> 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;
}
}
}

Expand Down
43 changes: 11 additions & 32 deletions src/main/java/dev/mccue/json/stream/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 923b137

Please sign in to comment.