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

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

Open
Nadeeshan96 opened this issue Feb 4, 2024 · 1 comment
Open
Labels
Area/LangLib Relates to lang.* libraries Type/Improvement Enhancement to language design

Comments

@Nadeeshan96
Copy link
Contributor

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.

# Converts a value of type `anydata` to `json`.
# This does a deep copy of `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.
#
# + v - anydata value
# + return - representation of `v` as value of type json
# This panics if `v` has cycles.
public isolated function toJson(anydata v) returns json = external;

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:

ballerina-platform/ballerina-lang#42070

@jclark
Copy link
Collaborator

jclark commented Feb 15, 2024

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

@anupama-pathirage anupama-pathirage added Area/LangLib Relates to lang.* libraries Type/Improvement Enhancement to language design labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/LangLib Relates to lang.* libraries Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

3 participants