ballerina-platform / ballerina-spec

Ballerina Language and Platform Specifications
Other
167 stars 54 forks source link

`value:toJson` has not mentioned on how to handle tagged data values #1288

Open Nadeeshan96 opened 9 months ago

Nadeeshan96 commented 9 months ago

Description:

Though value:toJson has mentioned about table and xml type anydata values, it does not mention how to handle tagged data values such as values of type lang.regexp:RegExp. https://github.com/ballerina-platform/ballerina-spec/blob/c88701b369d1266eb2a31708ec046f56aa8ca9da/lang/lib/value.bal#L117-L132

The spec has defined https://ballerina.io/spec/lang/master/#ToString for tagged data types. Do we need to just call value:toString when value:toJson is called on tagged data values or what should be done here?

Suggested Labels:

spec/undefined

Code sample that shows issue:

import ballerina/lang.regexp;
import ballerina/io;

public function main() {
    regexp:RegExp regExp = re `AB+C*D{1,4}`;
    json jsonVal = regExp.toJson(); 
    io:println(jsonVal);
}

currently gives

Running executable

error: {ballerina/lang.value}ConversionError {"message":"'anydata & readonly' value cannot be converted to 'json'"}
        at ballerina.lang.value.0:toJson(value.bal:262)
           reg:main(reg.bal:6)

Related Issues:

https://github.com/ballerina-platform/ballerina-lang/issues/42070

jclark commented 9 months ago

Tagged data values should be output as strings. The other way round should work too.