Skip to content

Commit

Permalink
disallow JsonNull for boolean fields
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Aug 29, 2024
1 parent e3e202d commit ad942f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
35 changes: 10 additions & 25 deletions src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private static Function parseFunctionUnchecked(TypeEnum type, JsonObject functio
}

private static Event<?> parseEventUnchecked(JsonObject event, int flags) {
final JsonArray inputs = getArray(event, INPUTS);
final JsonArray inputs = event.getAsJsonArray(INPUTS);
if (inputs != null) {
final boolean[] indexed = new boolean[inputs.size()];
return new Event<>(
Expand Down Expand Up @@ -228,7 +228,7 @@ private static TupleType<Tuple> parseTupleType(final JsonArray array, final bool
}

private static TupleType<?> parseTupleType(JsonObject object, String arrayKey, int flags) {
return parseTupleType(getArray(object, arrayKey), null, flags);
return parseTupleType(object.getAsJsonArray(arrayKey), null, flags);
}

private static ABIType<?> parseType(JsonObject object, int flags) {
Expand Down Expand Up @@ -392,48 +392,33 @@ static JsonArray parseArray(String json) {
return parseElement(json).getAsJsonArray();
}

static JsonArray getArray(JsonObject object, String key) {
private static String getString(JsonObject object, String key) {
final JsonElement element = object.get(key);
if (isNull(element)) {
if (element == null || element.isJsonNull()) {
return null;
}
return element.getAsJsonArray();
}

private static String getString(JsonObject object, String key) {
final JsonElement element = object.get(key);
if (element instanceof JsonPrimitive && ((JsonPrimitive) element).isString()) {
if (((JsonPrimitive) element).isString()) {
return element.getAsString();
}
if (isNull(element)) {
return null;
}
throw new IllegalArgumentException(key + " is not a string");
}

private static boolean getAnonymous(JsonObject object) {
final JsonElement element = object.get(ANONYMOUS);
if (element instanceof JsonPrimitive) {
return element.getAsBoolean();
}
if (isNull(element)) {
if (element == null) {
return false;
}
if (((JsonPrimitive) element).isBoolean()) {
return element.getAsBoolean();
}
throw new IllegalArgumentException(ANONYMOUS + " is not a boolean");
}

private static boolean getIndexed(JsonObject object) {
final JsonElement element = object.get(INDEXED);
if (element instanceof JsonPrimitive) {
if (((JsonPrimitive) element).isBoolean()) {
return element.getAsBoolean();
}
if (isNull(element)) {
throw new NullPointerException(INDEXED + " is null");
}
throw new IllegalArgumentException(INDEXED + " is not a boolean");
}

private static boolean isNull(JsonElement element) {
return element == null || element.isJsonNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ static ABITestCase forKey(String key) {
throw new RuntimeException(key + " not found");
}

JsonArray args = ABIJSON.getArray(jsonObject, "args");
JsonArray args = jsonObject.getAsJsonArray("args");
String result = jsonObject.get("result").getAsString();
JsonArray types = ABIJSON.getArray(jsonObject, "types");
JsonArray types = jsonObject.getAsJsonArray("types");

final int size = types.size();
final ABIType<?>[] arr = new ABIType<?>[size];
Expand Down

0 comments on commit ad942f9

Please sign in to comment.