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

Converting between JSON and YAML #7232

Merged

Conversation

straight-shoota
Copy link
Member

This adds YAML::Any#to_json(builder) and JSON::Any#to_yaml(builder) to convert arbitrary data between both formats.

There are many uses cases for such a conversion, one example is storing arbitrary YAML input data in a database using a JSON type.

The conversion can almost entirely be implemented by delegating to raw.to_yaml(builder) and this is works quite nicely out of the box.

Since YAML is a superset of JSON, JSON::Any#to_yaml is always valid and fully reversible.

All YAML raw types except Bytes implement both #to_yaml and #to_json, so it's not a huge issue.
I've implemted Serializing YAML::Any#to_json so that it raises when raw is a Slice.
We could also consider serializing Bytes as Base64 encoded string like in YAML.
JSON serializes some data types which are distinct in YAML, such as Time as strings, thus converting from YAML to JSON is not fully reversible.

@RX14 RX14 added this to the 0.27.1 milestone Dec 29, 2018
@RX14 RX14 merged commit d09d543 into crystal-lang:master Dec 29, 2018
@straight-shoota straight-shoota deleted the jm/feature/convert-json-yaml branch December 29, 2018 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants