diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java index 52a54c7099b..224a2ddc610 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java @@ -16,7 +16,9 @@ package software.amazon.smithy.typescript.codegen; import java.util.ArrayList; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.jmespath.ExpressionVisitor; import software.amazon.smithy.jmespath.JmespathExpression; @@ -61,35 +63,17 @@ class TypeScriptJmesPathVisitor implements ExpressionVisitor { } private String serializeObject(Map obj) { - StringBuilder builder = new StringBuilder(); - builder.append("{"); - boolean first = true; // first key-value pair - for (Map.Entry entry: obj.entrySet()) { - if (!first) { - builder.append(","); - } - builder.append("\"").append(entry.getKey()).append("\":"); - // recursively serialize value (could be primitive, obj, array) - builder.append(serializeValue(entry.getValue())); - first = false; - } - builder.append("}"); - return builder.toString(); + return "{" + obj.entrySet().stream() + .map(entry -> "\"" + entry.getKey() + "\":" + serializeValue(entry.getValue())) + .collect(Collectors.joining(",")) + + "}"; } - private String serializeArray(ArrayList array) { - StringBuilder builder = new StringBuilder(); - builder.append("["); - boolean first = true; - for (Object value: array) { - if (!first) { - builder.append(","); - } - builder.append(serializeValue(value)); - first = false; - } - builder.append("]"); - return builder.toString(); + private String serializeArray(List array) { + return "[" + array.stream() + .map(this::serializeValue) + .collect(Collectors.joining(",")) + + "]"; } @SuppressWarnings("unchecked") @@ -103,7 +87,7 @@ private String serializeValue(Object value) { } else if (value instanceof Map) { return serializeObject((Map) value); } else if (value instanceof ArrayList) { - return serializeArray((ArrayList) value); + return serializeArray((List) value); } throw new CodegenException("Unsupported literal type: " + value.getClass()); } @@ -245,14 +229,10 @@ public Void visitLiteral(LiteralExpression expression) { executionContext = "\"" + expression.getValue().toString() + "\""; break; case OBJECT: - @SuppressWarnings("unchecked") - Map objValue = (Map) expression.getValue(); - executionContext = serializeObject(objValue); + executionContext = serializeObject(expression.expectObjectValue()); break; case ARRAY: - @SuppressWarnings("unchecked") - ArrayList arrayValue = (ArrayList) expression.getValue(); - executionContext = serializeArray(arrayValue); + executionContext = serializeArray(expression.expectArrayValue()); break; default: // All other options are already valid js literials.