From 9f9acbd7f524a73cd531b6770ce188a2851e1032 Mon Sep 17 00:00:00 2001 From: Xincheng Huang <60057611+ying-hua@users.noreply.github.com> Date: Mon, 2 Sep 2024 10:49:08 +0800 Subject: [PATCH] [INLONG-10957][SDK] Improve some code structures (#10958) --- .../sdk/transform/decode/JsonSourceData.java | 24 ++++---- .../transform/decode/JsonSourceDecoder.java | 55 ++++++++++--------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceData.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceData.java index 5f92c34965c..539f4b06dc5 100644 --- a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceData.java +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceData.java @@ -111,20 +111,22 @@ public String getField(int rowNum, String fieldName) { // error data return ""; } + // node is not array if (!node.isArray()) { current = newElement; - } else { - if (!newElement.isJsonArray()) { - // error data - return ""; - } - JsonArray newArray = newElement.getAsJsonArray(); - if (node.getArrayIndex() >= newArray.size()) { - // error data - return ""; - } - current = newArray.get(node.getArrayIndex()); + continue; + } + // node is an array + if (!newElement.isJsonArray()) { + // error data + return ""; + } + JsonArray newArray = newElement.getAsJsonArray(); + if (node.getArrayIndex() >= newArray.size()) { + // error data + return ""; } + current = newArray.get(node.getArrayIndex()); } return current.getAsString(); } catch (Exception e) { diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceDecoder.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceDecoder.java index 2d16d92bc1a..426ae167a28 100644 --- a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceDecoder.java +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/decode/JsonSourceDecoder.java @@ -85,39 +85,40 @@ public SourceData decode(byte[] srcBytes, Context context) { public SourceData decode(String srcString, Context context) { JsonObject root = gson.fromJson(srcString, JsonObject.class); JsonArray childRoot = null; - if (CollectionUtils.isNotEmpty(childNodes)) { - JsonElement current = root; - for (JsonNode node : childNodes) { - if (!current.isJsonObject()) { + if (CollectionUtils.isEmpty(childNodes)) { + return new JsonSourceData(root, null); + } + JsonElement current = root; + for (JsonNode node : childNodes) { + if (!current.isJsonObject()) { + // error data + return new JsonSourceData(root, null); + } + JsonElement newElement = current.getAsJsonObject().get(node.getName()); + if (newElement == null) { + // error data + return new JsonSourceData(root, null); + } + if (!node.isArray()) { + current = newElement; + } else { + if (!newElement.isJsonArray()) { // error data - return new JsonSourceData(root, childRoot); + return new JsonSourceData(root, null); } - JsonElement newElement = current.getAsJsonObject().get(node.getName()); - if (newElement == null) { + JsonArray newArray = newElement.getAsJsonArray(); + if (node.getArrayIndex() >= newArray.size()) { // error data - return new JsonSourceData(root, childRoot); + return new JsonSourceData(root, null); } - if (!node.isArray()) { - current = newElement; - } else { - if (!newElement.isJsonArray()) { - // error data - return new JsonSourceData(root, childRoot); - } - JsonArray newArray = newElement.getAsJsonArray(); - if (node.getArrayIndex() >= newArray.size()) { - // error data - return new JsonSourceData(root, childRoot); - } - current = newArray.get(node.getArrayIndex()); - } - } - if (!current.isJsonArray()) { - // error data - return new JsonSourceData(root, childRoot); + current = newArray.get(node.getArrayIndex()); } - childRoot = current.getAsJsonArray(); } + if (!current.isJsonArray()) { + // error data + return new JsonSourceData(root, null); + } + childRoot = current.getAsJsonArray(); return new JsonSourceData(root, childRoot); } }