Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Name annotation not being used when using toJson with tables #52

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion ballerina/tests/to_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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<Employee> 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, <json> [
{id: 1001, name: "Mary", dept: "legal"},
{id: 1002, name: "John", dept: "finance"}
]);
}

type TestRecord3 record {|
@Name {
value: "a-o"
Expand Down Expand Up @@ -116,7 +136,7 @@ type NestedRecord3 record {
};

@test:Config
function testToJsonWithNameANnotation() {
function testToJsonWithNameAnnotation() {
TestRecord3 r = {
a: "name",
b: "b name",
Expand Down Expand Up @@ -172,6 +192,18 @@ function testToJsonWithNameANnotation() {
json|Error j2 = toJson(n);
test:assertTrue(j2 is json);
test:assertEquals(j2, out2);

table<TestRecord3> 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 {|
Expand Down Expand Up @@ -214,6 +246,17 @@ function testToJsonWithCyclicValues() {
test:assertTrue(r3 is error);
error r3Err = <error> r3;
test:assertEquals("the value has a cyclic reference", r3Err.message());

table<record {readonly int id; string name; record {} details;}> 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 = <error> r4;
test:assertEquals("the value has a cyclic reference", r4Err.message());
}

function toJsonWithCyclicValues(anydata val) returns json {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,6 +122,17 @@ public static Object toJson(Object value, Set<Object> 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);
}

Expand Down
Loading