From 44a5bf936bab64a354b05af72a663d1324d34b95 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 16 Jan 2025 14:41:30 +0530 Subject: [PATCH 1/5] Fix JSON serialize for tuples and records --- .../java/io/ballerina/runtime/internal/json/JsonGenerator.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/json/JsonGenerator.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/json/JsonGenerator.java index 1cce229bcef8..4b6b05bf395d 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/json/JsonGenerator.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/json/JsonGenerator.java @@ -289,6 +289,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) { streamingJsonValue.serialize(this); break; @@ -317,6 +318,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 2c8c3a0724c49631c1f73fddc7638271b7e13a40 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 16 Jan 2025 14:41:43 +0530 Subject: [PATCH 2/5] 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 c79f1193afb0..37604438a28a 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; @@ -102,4 +104,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 7b46e574ce4b..7a68b896d9d1 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 @@ -49,15 +49,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 42749466a038818456555908bb5db07754ca97e3 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 16 Jan 2025 20:05:40 +0530 Subject: [PATCH 3/5] 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 7a68b896d9d1..7b46e574ce4b 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 @@ -49,15 +49,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 74dc8abf108dc656aec56b05a30c05b690a0f9dd Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 22 Jan 2025 10:56:24 +0530 Subject: [PATCH 4/5] Fix API error --- .../nativeimpl/jvm/runtime/api/tests/JsonValues.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 37604438a28a..77615dd8cf9a 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,7 +26,7 @@ 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.json.JsonGenerator; import io.ballerina.runtime.internal.values.ErrorValue; import java.io.ByteArrayInputStream; @@ -112,7 +112,7 @@ public static Object testJsonSerializeExtern(Object jsonObject) { gen.flush(); out.close(); } catch (IOException e) { - throw new ErrorValue(StringUtils.fromString(e.getMessage()), e); + throw new ErrorValue(StringUtils.fromString(e.getMessage())); } return StringUtils.fromString(out.toString()); } From d6b45838e99e6f1979171246074424980175b03a Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 22 Jan 2025 12:28:05 +0530 Subject: [PATCH 5/5] 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 77615dd8cf9a..f5251aaffcce 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;