Skip to content

Commit

Permalink
[INLONG-10957][SDK] Improve some code structures (#10958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-hua authored Sep 2, 2024
1 parent de98601 commit 9f9acbd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 9f9acbd

Please sign in to comment.