The toJson method is allowed in anydata types where a non-json types are converted to a respective json compatible types according to the method definition:
# This does a deep copy of parameter `v` converting values that do
# not belong to json into values that do.
# A value of type `xml` is converted into a string as if
# by the `toString` function.
# A value of type `table` is converted into a list of
# mappings one for each row.
# The inherent type of arrays in the return value will be
# `json[]` and of mappings will be `map<json>`.
# A new copy is made of all structural values, including
# immutable values.
But the regexp:RegExp with toJson method panics at runtime:
error: {ballerina/lang.value}ConversionError {"message":"'anydata & readonly' value cannot be converted to 'json'"}
at ballerina.lang.value.0:toJson(value.bal:262)
sample:main(sample.bal:7)
The method description should be updated if this is the correct behaviour or the method should return a json value
public function main() {
io:println(regExp.toJson());
}
- Regular expression as a record field and record with `toJson`
```bal
import ballerina/io;
import ballerina/lang.regexp;
regexp:RegExp regExp = re `a`;
type Rec record {
xml xmlValue;
table<map<string>> tableValue;
};
public function main() {
Rec rec = {
xmlValue: xml `<name>John</name>`,
tableValue: table [{"key": "value"}]
};
io:println(rec.toJson());
rec["regExpValue"] = regExp;
io:println(rec.toJson());
}
Description
The
toJson
method is allowed inanydata
types where a non-json types are converted to a respective json compatible types according to the method definition:But the
regexp:RegExp
withtoJson
method panics at runtime:The method description should be updated if this is the correct behaviour or the method should return a json value
Steps to Reproduce
toJson
regexp:RegExp regExp = re
a
;public function main() { io:println(regExp.toJson()); }
Affected Version(s)
Ballerina SwanLake Update 10(2201.10.x)
OS, DB, other environment details and versions
No response
Related area
-> Runtime
Related issue(s) (optional)
No response
Suggested label(s) (optional)
No response
Suggested assignee(s) (optional)
No response