From c3ee274bbd0f047aa304949db7599d495c868b53 Mon Sep 17 00:00:00 2001 From: dw <307665930@qq.com> Date: Sun, 28 Apr 2024 15:23:42 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9Ahandle=20different=20solc=20compile?= =?UTF-8?q?=20result=20raw=20json=20for=20abi=20bin=20and=20doc=20=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: use only one source code copy to support mutiple solc version included 0.4.25 0.5.2 0,6.10 0.8.11 * fix: format code by google code format plugin. * fix: The 0.8.11 solc return abi and doc as json object but 0.6.10 0.5.2 0.4.25 solc return abi and doc as string, so the parse logic should handle it。 * fix: auto fix by google code format --------- Co-authored-by: dwzhan --- .../solc/compiler/CompilationResult.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/fisco/solc/compiler/CompilationResult.java b/src/main/java/org/fisco/solc/compiler/CompilationResult.java index e07b581..4318c8d 100644 --- a/src/main/java/org/fisco/solc/compiler/CompilationResult.java +++ b/src/main/java/org/fisco/solc/compiler/CompilationResult.java @@ -46,16 +46,19 @@ public static CompilationResult parse(String rawJson) throws IOException { JsonObject contractJsonObject = asJsonObject.get(contract.toString()).getAsJsonObject(); JsonObject abiObject = new JsonObject(); - abiObject.addProperty("abi", contractJsonObject.get("abi").toString()); - abiObject.addProperty("bin", contractJsonObject.get("bin").getAsString()); - abiObject.addProperty("metadata", contractJsonObject.get("metadata").getAsString()); + abiObject.addProperty("abi", getJsonValueAsString(contractJsonObject, "abi")); + abiObject.addProperty("bin", getJsonValueAsString(contractJsonObject, "bin")); + abiObject.addProperty( + "metadata", getJsonValueAsString(contractJsonObject, "metadata")); if (contractJsonObject.get("userdoc") != null) { - abiObject.addProperty("userdoc", contractJsonObject.get("userdoc").toString()); + abiObject.addProperty( + "userdoc", getJsonValueAsString(contractJsonObject, "userdoc")); } if (contractJsonObject.get("devdoc") != null) { - abiObject.addProperty("devdoc", contractJsonObject.get("devdoc").toString()); + abiObject.addProperty( + "devdoc", getJsonValueAsString(contractJsonObject, "devdoc")); } contractObject.add(contract.toString(), abiObject); } @@ -65,6 +68,18 @@ public static CompilationResult parse(String rawJson) throws IOException { } } + private static String getJsonValueAsString(JsonObject jsonObject, String key) { + if (jsonObject == null || jsonObject.get(key) == null || jsonObject.get(key).isJsonNull()) { + return null; + } + + if (jsonObject.get(key).isJsonPrimitive()) { + return jsonObject.get(key).getAsString(); + } + + return jsonObject.get(key).toString(); + } + /** * @param contractName The contract name * @return the first contract found for a given contract name; use {@link #getContract(Path,