Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ballerina-platform/module-ballerina…
Browse files Browse the repository at this point in the history
…-data.jsondata into java21
  • Loading branch information
HindujaB committed Nov 18, 2024
2 parents 41d6524 + f587a27 commit f55a5bd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
82 changes: 60 additions & 22 deletions ballerina/tests/to_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,13 @@ type TestRecord4 record {|
function testToJsonWithCyclicValues() {
json[] v1 = [];
v1.push(v1);
json|error r1 = trap toJsonWithCyclicValues(v1);
test:assertTrue(r1 is error);
error r1Err = <error> r1;
test:assertEquals("the value has a cyclic reference", r1Err.message());
json|error r1 = toJsonWithCyclicValues(v1);
assertCyclicReferenceError(r1);

map<json> v2 = {};
v2["val"] = v2;
json|error r2 = trap toJsonWithCyclicValues(v2);
test:assertTrue(r2 is error);
error r2Err = <error> r2;
test:assertEquals("the value has a cyclic reference", r2Err.message());
json|error r2 = toJsonWithCyclicValues(v2);
assertCyclicReferenceError(r2);

TestRecord4 v3 = {
a: "a-v",
Expand All @@ -242,21 +238,17 @@ function testToJsonWithCyclicValues() {
d: []
};
v3.d.push(v3);
json|error r3 = trap toJsonWithCyclicValues(v3);
test:assertTrue(r3 is error);
error r3Err = <error> r3;
test:assertEquals("the value has a cyclic reference", r3Err.message());
json|error r3 = toJsonWithCyclicValues(v3);
assertCyclicReferenceError(r3);

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());
json|error r4 = toJsonWithCyclicValues(v4);
assertCyclicReferenceError(r4);
}

@test:Config
Expand Down Expand Up @@ -285,6 +277,48 @@ function testToJsonWithoutCyclicValuesWithRepeatedSimpleValueMembers() {
test:assertNotExactEquals(jsonVal, jsonRes);
}

type GreetingConf record {|
readonly int id;
record {| string message; |} greeting;
record {| int count; int interval; |} repetition;
|};

@test:Config
function testToJsonWithoutCyclicValuesWithRepeatedStructuredMembers() {
map<json> greetingConf = {greeting: {message: "hello world"}, repetition: {count: 2, interval: 5}};
map<json>[] greetingConfs = [greetingConf, greetingConf];
json arrJson = toJson(greetingConfs);
test:assertEquals(greetingConfs, arrJson);
test:assertNotExactEquals(greetingConfs, arrJson);

GreetingConf gc = {
id: 1001,
greeting: {message: "hello world"},
repetition: {count: 2, interval: 5}
};
table<GreetingConf> tab = table [];
tab.put(gc);
tab.put({id: 1002, greeting: {message: "hello!"}, repetition: {count: 1, interval: 30}});
tab.put(gc);
json tabJson = toJson(tab);
test:assertEquals(<json[]> [
gc,
{id: 1002, greeting: {message: "hello!"}, repetition: {count: 1, interval: 30}},
gc
], tabJson);

int[] arr = [1, 2, 3];
map<int[]> mp = {
a: arr,
b: [],
c: [3, 5, 6, 7],
d: arr
};
json mapJson = toJson(mp);
test:assertEquals(mp, mapJson);
test:assertNotExactEquals(mp, mapJson);
}

@test:Config
function testToJsonWithCyclicValuesWithOtherSimpleValueMembers() {
byte byteVal = 3;
Expand All @@ -307,12 +341,16 @@ function testToJsonWithCyclicValuesWithOtherSimpleValueMembers() {
"p": false
};
jsonVal["q"] = jsonVal;
json|error r1 = trap toJsonWithCyclicValues(jsonVal);
test:assertTrue(r1 is error);
error r1Err = <error> r1;
test:assertEquals("the value has a cyclic reference", r1Err.message());
json|error r1 = toJsonWithCyclicValues(jsonVal);
assertCyclicReferenceError(r1);
}

function toJsonWithCyclicValues(anydata val) returns json|error {
return trap toJson(val);
}

function toJsonWithCyclicValues(anydata val) returns json {
return toJson(val);
function assertCyclicReferenceError(json|error res) {
test:assertTrue(res is error);
error err = <error> res;
test:assertEquals("the value has a cyclic reference", err.message());
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public static Object toJson(Object value, Set<Object> visitedValues) {
int length = (int) listValue.getLength();
Object[] convertedValues = new Object[length];
for (int i = 0; i < length; i++) {
convertedValues[i] = toJson(listValue.get(i), visitedValues);
Object memValue = listValue.get(i);
convertedValues[i] = toJson(memValue, visitedValues);
visitedValues.remove(memValue);
}
return ValueCreator.createArrayValue(convertedValues, PredefinedTypes.TYPE_JSON_ARRAY);
}
Expand All @@ -124,6 +126,7 @@ public static Object toJson(Object value, Set<Object> visitedValues) {
for (BString entryKey : mapValue.getKeys()) {
Object entryValue = mapValue.get(entryKey);
jsonObject.put(getNameAnnotation(mapValue, entryKey), toJson(entryValue, visitedValues));
visitedValues.remove(entryValue);
}

return jsonObject;
Expand All @@ -136,6 +139,7 @@ public static Object toJson(Object value, Set<Object> visitedValues) {
int index = 0;
for (Object tableMember : tableValue.values()) {
convertedValues[index++] = toJson(tableMember, visitedValues);
visitedValues.remove(tableMember);
}
return ValueCreator.createArrayValue(convertedValues, PredefinedTypes.TYPE_JSON_ARRAY);
}
Expand Down

0 comments on commit f55a5bd

Please sign in to comment.