Skip to content

Commit

Permalink
add optimize(List) and encode(List) to ABIJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Feb 28, 2025
1 parent e1b91b8 commit d355fda
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
55 changes: 39 additions & 16 deletions src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static <T extends ABIObject> List<T> parseABIField(int flags, String obje
}

/**
* Returns a minified version of the argument, optimized for parsing by this class. Accepts JSON array or JSON object.
* Returns a minified version of the argument which is optimized for parsing. Accepts JSON array or JSON object.
*
* @param json Contract ABI JSON array or Function/Event/ContractError JSON object
* @return optimized JSON
Expand All @@ -145,14 +145,51 @@ public static String optimize(String json) {
if (token == JsonToken.BEGIN_OBJECT) {
return toJson(ABIObject.fromJson(json), false, true);
} else if (token == JsonToken.BEGIN_ARRAY) {
return minifyArray(reader);
return encode(true, parseArray(reader, ABIJSON.ALL, ABIType.FLAGS_NONE, Function.newDefaultDigest()));
}
throw new IllegalArgumentException("unexpected token: " + token);
} catch (IOException io) {
throw new IllegalStateException(io);
}
}

/**
* Returns a minified JSON array containing the given elements' encodings which is optimized for parsing.
*
* @param elements the objects to be put into the array
* @return the JSON array
* @param <T> the common supertype of the elements
*/
public static <T extends ABIObject> String optimize(List<T> elements) {
return encode(true, elements);
}

/**
* Returns a JSON array string containing the given elements' encodings.
*
* @param elements the objects to be put into the array
* @return the JSON array
* @param <T> common supertype of the elements
*/
public static <T extends ABIObject> String encode(List<T> elements) {
return encode(false, elements);
}
//----------------------------------------------------------------------------------------------------------------------
private static <T extends ABIObject> String encode(boolean minify, List<T> elements) {
final Writer stringOut = new NonSyncWriter(2048);
try (JsonWriter out = new JsonWriter(stringOut)) {
out.setIndent(minify ? "" : " ");
out.beginArray();
for (ABIObject e : elements) {
writeObject(e, out, minify);
}
out.endArray();
} catch (IOException io) {
throw new IllegalStateException(io);
}
return stringOut.toString();
}

static <T extends ABIObject> List<T> parseArray(final JsonReader reader, Set<TypeEnum> types, int flags, MessageDigest digest) {
final List<T> list = new ArrayList<>();
try (JsonReader ignored = reader) {
Expand Down Expand Up @@ -183,20 +220,6 @@ static String toJson(ABIObject o, boolean pretty, boolean minify) {
return stringOut.toString();
}

private static String minifyArray(JsonReader reader) throws IOException {
final List<ABIObject> elements = parseArray(reader, ABIJSON.ALL, ABIType.FLAGS_NONE, Function.newDefaultDigest());
final Writer stringOut = new NonSyncWriter(2048);
try (JsonWriter out = new JsonWriter(stringOut)) {
out.setIndent("");
out.beginArray();
for (ABIObject e : elements) {
writeObject(e, out, true);
}
out.endArray();
}
return stringOut.toString();
}

private static void writeObject(ABIObject o, JsonWriter out, boolean minify) throws IOException {
out.beginObject();
if (o.isFunction()) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,19 @@ public void optimizeJson() {
assertEquals(normIter.next(), optIter.next());
}
}

final ABIParser p = new ABIParser();
assertEquals(optimized, ABIJSON.optimize(p.parse(CONTRACT_JSON)));
assertEquals(optimized, ABIJSON.optimize(p.parse(optimized)));
assertEquals(optimized, ABIJSON.optimize(optimized));
assertEquals(ABIJSON.optimize(FALLBACK_CONSTRUCTOR_RECEIVE), ABIJSON.optimize(p.parse(FALLBACK_CONSTRUCTOR_RECEIVE)));
assertEquals(ABIJSON.optimize(FALLBACK_CONSTRUCTOR_RECEIVE), ABIJSON.optimize(ABIJSON.optimize(FALLBACK_CONSTRUCTOR_RECEIVE)));
assertEquals(ABIJSON.optimize("[]"), ABIJSON.optimize(p.parse("[]")));
assertEquals(ABIJSON.optimize("[]"), ABIJSON.optimize(ABIJSON.optimize("[]")));

assertEquals(CONTRACT_JSON, ABIJSON.encode(p.parse(CONTRACT_JSON)));
assertEquals(FALLBACK_CONSTRUCTOR_RECEIVE, ABIJSON.encode(p.parse(FALLBACK_CONSTRUCTOR_RECEIVE)));
assertEquals("[]", ABIJSON.encode(p.parse("[]")));
}

@Test
Expand Down

0 comments on commit d355fda

Please sign in to comment.