Skip to content

Commit a9721bf

Browse files
committed
Move to Switch by json type
Signed-off-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
1 parent 788be9d commit a9721bf

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

core/src/main/java/org/opensearch/sql/utils/JsonUtils.java

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,33 @@ public static ExprValue castJson(ExprValue json) {
7575
}
7676

7777
private static ExprValue processJsonNode(JsonNode jsonNode) {
78-
if (jsonNode.isFloatingPointNumber()) {
79-
return new ExprDoubleValue(jsonNode.asDouble());
78+
switch (jsonNode.getNodeType()) {
79+
case ARRAY:
80+
List<ExprValue> elements = new LinkedList<>();
81+
for (var iter = jsonNode.iterator(); iter.hasNext(); ) {
82+
jsonNode = iter.next();
83+
elements.add(processJsonNode(jsonNode));
84+
}
85+
return new ExprCollectionValue(elements);
86+
case OBJECT:
87+
Map<String, ExprValue> values = new LinkedHashMap<>();
88+
for (var iter = jsonNode.fields(); iter.hasNext(); ) {
89+
Map.Entry<String, JsonNode> entry = iter.next();
90+
values.put(entry.getKey(), processJsonNode(entry.getValue()));
91+
}
92+
return ExprTupleValue.fromExprValueMap(values);
93+
case STRING:
94+
return new ExprStringValue(jsonNode.asText());
95+
case NUMBER:
96+
if (jsonNode.isFloatingPointNumber()) {
97+
return new ExprDoubleValue(jsonNode.asDouble());
98+
}
99+
return new ExprIntegerValue(jsonNode.asLong());
100+
case BOOLEAN:
101+
return jsonNode.asBoolean() ? LITERAL_TRUE : LITERAL_FALSE;
102+
default:
103+
// in all other cases, return null
104+
return LITERAL_NULL;
80105
}
81-
if (jsonNode.isIntegralNumber()) {
82-
return new ExprIntegerValue(jsonNode.asLong());
83-
}
84-
if (jsonNode.isBoolean()) {
85-
return jsonNode.asBoolean() ? LITERAL_TRUE : LITERAL_FALSE;
86-
}
87-
if (jsonNode.isTextual()) {
88-
return new ExprStringValue(jsonNode.asText());
89-
}
90-
if (jsonNode.isArray()) {
91-
List<ExprValue> elements = new LinkedList<>();
92-
for (var iter = jsonNode.iterator(); iter.hasNext(); ) {
93-
jsonNode = iter.next();
94-
elements.add(processJsonNode(jsonNode));
95-
}
96-
return new ExprCollectionValue(elements);
97-
}
98-
if (jsonNode.isObject()) {
99-
Map<String, ExprValue> values = new LinkedHashMap<>();
100-
for (var iter = jsonNode.fields(); iter.hasNext(); ) {
101-
Map.Entry<String, JsonNode> entry = iter.next();
102-
values.put(entry.getKey(), processJsonNode(entry.getValue()));
103-
}
104-
return ExprTupleValue.fromExprValueMap(values);
105-
}
106-
107-
// in all other cases, return null
108-
return LITERAL_NULL;
109106
}
110107
}

0 commit comments

Comments
 (0)