From 4bf064cb38ada814516b60bb09454802df197b67 Mon Sep 17 00:00:00 2001 From: MaryamZi Date: Tue, 12 Nov 2024 09:46:29 +0530 Subject: [PATCH 1/2] Fix toJson not using the Name annotation with tables --- .../io/ballerina/lib/data/jsondata/json/Native.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/native/src/main/java/io/ballerina/lib/data/jsondata/json/Native.java b/native/src/main/java/io/ballerina/lib/data/jsondata/json/Native.java index e8bdd9e..2bef7e9 100644 --- a/native/src/main/java/io/ballerina/lib/data/jsondata/json/Native.java +++ b/native/src/main/java/io/ballerina/lib/data/jsondata/json/Native.java @@ -37,6 +37,7 @@ import io.ballerina.runtime.api.values.BObject; import io.ballerina.runtime.api.values.BStream; import io.ballerina.runtime.api.values.BString; +import io.ballerina.runtime.api.values.BTable; import io.ballerina.runtime.api.values.BTypedesc; import java.io.ByteArrayInputStream; @@ -121,6 +122,17 @@ public static Object toJson(Object value, Set visitedValues) { return jsonObject; } + if (value instanceof BTable tableValue) { + int length = tableValue.size(); + Object[] convertedValues = new Object[length]; + + int index = 0; + for (Object tableMember : tableValue.values()) { + convertedValues[index++] = toJson(tableMember, visitedValues); + } + return ValueCreator.createArrayValue(convertedValues, PredefinedTypes.TYPE_JSON_ARRAY); + } + return JsonUtils.convertToJson(value); } From 07d366f937e9fe36768af675fc0c00993ecf5a19 Mon Sep 17 00:00:00 2001 From: MaryamZi Date: Tue, 12 Nov 2024 10:59:23 +0530 Subject: [PATCH 2/2] Add tests --- ballerina/tests/to_json_test.bal | 45 +++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/ballerina/tests/to_json_test.bal b/ballerina/tests/to_json_test.bal index 538951d..b630a0d 100644 --- a/ballerina/tests/to_json_test.bal +++ b/ballerina/tests/to_json_test.bal @@ -81,6 +81,26 @@ function testToJsonWithXML() { test:assertEquals(j, x1.toString()); } +type Employee record {| + readonly int id; + string name; + string dept; +|}; + +@test:Config +function testToJsonWithTables() { + table key (id) tb = table [ + {id: 1001, name: "Mary", dept: "legal"}, + {id: 1002, name: "John", dept: "finance"} + ]; + json tbJson = toJson(tb); + test:assertTrue(tbJson is json[]); + test:assertEquals(tbJson, [ + {id: 1001, name: "Mary", dept: "legal"}, + {id: 1002, name: "John", dept: "finance"} + ]); +} + type TestRecord3 record {| @Name { value: "a-o" @@ -116,7 +136,7 @@ type NestedRecord3 record { }; @test:Config -function testToJsonWithNameANnotation() { +function testToJsonWithNameAnnotation() { TestRecord3 r = { a: "name", b: "b name", @@ -172,6 +192,18 @@ function testToJsonWithNameANnotation() { json|Error j2 = toJson(n); test:assertTrue(j2 is json); test:assertEquals(j2, out2); + + table tb = table [ + {a: "a value", b: "b value", c: 1001}, + {a: "a value 2", b: "b value 2", c: 1002} + ]; + json j3 = toJson(tb); + test:assertTrue(j3 is json[]); + json[] out3 = [ + {"a-o": "a value", "b-o": "b value", c: 1001}, + {"a-o": "a value 2", "b-o": "b value 2", c: 1002} + ]; + test:assertEquals(j3, out3); } type TestRecord4 record {| @@ -214,6 +246,17 @@ function testToJsonWithCyclicValues() { test:assertTrue(r3 is error); error r3Err = r3; test:assertEquals("the value has a cyclic reference", r3Err.message()); + + table key (id) v4 = + table [ + {id: 1023, name: "Joy", details: {}} + ]; + record {} details = v4.get(1023).details; + details["tb"] = v4; + json|error r4 = trap toJsonWithCyclicValues(v4); + test:assertTrue(r4 is error); + error r4Err = r4; + test:assertEquals("the value has a cyclic reference", r4Err.message()); } function toJsonWithCyclicValues(anydata val) returns json {