From 5bc3d3508f9f6b2dbb6aa6bacada7c6a122f2534 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 16 Jan 2025 14:41:30 +0530 Subject: [PATCH 1/4] Fix JSON serialize for tuples and records --- .../main/java/io/ballerina/runtime/internal/JsonGenerator.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonGenerator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonGenerator.java index b602c2595f5d..882f618c0692 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonGenerator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/JsonGenerator.java @@ -288,6 +288,7 @@ public void serialize(Object json) throws IOException { switch (TypeUtils.getImpliedType(TypeChecker.getType(json)).getTag()) { case TypeTags.ARRAY_TAG: + case TypeTags.TUPLE_TAG: if (json instanceof StreamingJsonValue) { ((StreamingJsonValue) json).serialize(this); break; @@ -316,6 +317,7 @@ public void serialize(Object json) throws IOException { break; case TypeTags.MAP_TAG: case TypeTags.JSON_TAG: + case TypeTags.RECORD_TYPE_TAG: this.startObject(); for (Entry entry : ((MapValueImpl) json).entrySet()) { this.writeFieldName(entry.getKey().getValue()); From 024101e6172e4f8933b0a4753ee6c6c3e2c39af7 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 16 Jan 2025 14:41:43 +0530 Subject: [PATCH 2/4] Add unit test --- .../jvm/runtime/api/tests/JsonValues.java | 16 +++++++++++- .../test/runtime/api/RuntimeAPITest.java | 18 ++++++------- .../test-src/runtime/api/json/main.bal | 25 +++++++++++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java index 0d602ce171a5..56a79e290dec 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java @@ -26,13 +26,15 @@ import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; +import io.ballerina.runtime.internal.JsonGenerator; import io.ballerina.runtime.internal.values.ErrorValue; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.StringReader; import java.nio.charset.StandardCharsets; - +import java.io.IOException; import static io.ballerina.runtime.api.utils.JsonUtils.convertJSON; import static io.ballerina.runtime.api.utils.JsonUtils.convertJSONToRecord; @@ -99,4 +101,16 @@ public static Object testParsingNullString(BString str) { public static Object testBStringParsingWithProcessingMode(BString str) { return JsonUtils.parse(str, JsonUtils.NonStringValueProcessingMode.FROM_JSON_STRING); } + + public static Object testJsonSerializeExtern(Object jsonObject) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (JsonGenerator gen = new JsonGenerator(out)) { + gen.serialize(jsonObject); + gen.flush(); + out.close(); + } catch (IOException e) { + throw new ErrorValue(StringUtils.fromString(e.getMessage()), e); + } + return StringUtils.fromString(out.toString()); + } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java index 63f34a1bdc40..8ef3f1520641 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java @@ -48,15 +48,15 @@ public void testRuntimeAPIs(String packageName) { @DataProvider public Object[] packageNameProvider() { return new String[]{ - "values", - "errors", - "types", - "invalid_values", - "async", - "utils", - "identifier_utils", - "environment", - "stream", +// "values", +// "errors", +// "types", +// "invalid_values", +// "async", +// "utils", +// "identifier_utils", +// "environment", +// "stream", "json" }; } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/json/main.bal b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/json/main.bal index 74b642a7b656..f07f63c49312 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/json/main.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/json/main.bal @@ -44,8 +44,33 @@ public function main() { result = testStringParsingWithProcessingMode(jsonStr); test:assertTrue(result is json, "Invalid json"); + + testJsonSerialize(); +} + +const string[] CONST_ROLES = ["Admin"]; + +type Profile record {| + string name; + int age; +|}; + +function testJsonSerialize() { + Profile profile = {name: "John", age: 30}; + json j = { + roles: CONST_ROLES, + ID: "User1", + profile: profile + }; + string expected = "{\"roles\":[\"Admin\"], \"ID\":\"User1\", \"profile\":{\"name\":\"John\", \"age\":30}}"; + string result = checkpanic testJsonSerializeExtern(j); + test:assertEquals(result, expected, "Invalid json serialization"); } +public isolated function testJsonSerializeExtern(json j) returns string|error = @java:Method { + 'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.JsonValues" +} external; + public isolated function testParsingWrongCharset(string str) returns json|error = @java:Method { 'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.JsonValues" } external; From 991bb2b3c2fd01817269debd320efbe1d1bdc901 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 16 Jan 2025 20:08:03 +0530 Subject: [PATCH 3/4] Fix checkStyle error --- .../test/runtime/api/RuntimeAPITest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java index 8ef3f1520641..63f34a1bdc40 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java @@ -48,15 +48,15 @@ public void testRuntimeAPIs(String packageName) { @DataProvider public Object[] packageNameProvider() { return new String[]{ -// "values", -// "errors", -// "types", -// "invalid_values", -// "async", -// "utils", -// "identifier_utils", -// "environment", -// "stream", + "values", + "errors", + "types", + "invalid_values", + "async", + "utils", + "identifier_utils", + "environment", + "stream", "json" }; } From 2f06273ba7d280c07406137493938bc29f8e2848 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 22 Jan 2025 10:46:48 +0530 Subject: [PATCH 4/4] Fix checkStyle error --- .../nativeimpl/jvm/runtime/api/tests/JsonValues.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java index 56a79e290dec..255b414a177e 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java @@ -31,10 +31,11 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.nio.charset.StandardCharsets; -import java.io.IOException; + import static io.ballerina.runtime.api.utils.JsonUtils.convertJSON; import static io.ballerina.runtime.api.utils.JsonUtils.convertJSONToRecord;