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

Add helpers to easily read and write a serializable type to JSON #25464

Merged
merged 7 commits into from
Jul 10, 2024

Conversation

DanilaFe
Copy link
Contributor

@DanilaFe DanilaFe commented Jul 9, 2024

Resolves #8278.

This PR adds methods to convert types to and from JSON strings. These serve as convenience wrappers for the existing JSON (de)serialization functionality implemented by @benharsh in #22437.

use IO, JSON;

record myPair : serializable {
  var x: int;
  var y: string;
}

var myValue = new myPair(1, "hello");
var myValueJson = toJson(myValue);

writeln(myValueJson);
writeln(fromJson(myValueJson, myPair));

Prints:

{"x":1, "y":"hello"}
(x = 1, y = hello)

Reviewed by @jeremiah-corrado -- thanks!

Testing

  • paratest

Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
@jeremiah-corrado jeremiah-corrado self-requested a review July 9, 2024 20:02
Copy link
Contributor

@jeremiah-corrado jeremiah-corrado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

}

var myValue = new myPair(1, "hello");
var myValueJson = toJson(myValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these values are unused, should they be removed from this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, they should be.

Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
@DanilaFe DanilaFe merged commit f3dff0c into chapel-lang:main Jul 10, 2024
7 checks passed
@bradcray
Copy link
Member

I don't know much about JSON, but this seems very cool and productive from a distance, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify the construction of objects from JSON strings
3 participants