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 YAML::Any method to_json #4217

Closed

Conversation

icyleaf
Copy link
Contributor

@icyleaf icyleaf commented Mar 31, 2017

Add method .to_json for YAML::Any

doc = YAML.parse(%(---
        string: hello crystal
        integer: 10
        float: 1.0
        true: true
        false: false
        array:
          - item1
          - item2
        hash:
          key1: value1
          key2: value2
        empty_value:
      ))

puts doc.to_json

output:

{"string":"hello crystal","integer":10,"float":1.0,"true":true,"false":false,"array":["item1","item2"],"hash":{"key1":"value1","key2":"value2"},"empty_value":null}

Is this can be add to std method, and how about to_yaml for JSON::Any add too?

when Nil
json.null
when /^[+-]?([0-9]*[.])?[0-9]+$/
object.includes?(".") ? json.number(object.to_f) : json.number(object.to_i)
Copy link
Member

Choose a reason for hiding this comment

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

This place doesn't feel quite right for this kind of conversions (the when "true" and when "false") too, shouldn't YAML.parse do them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

YAML only accepts string, array and hash, see details in parser of yaml and http://www.yaml.org/spec/1.2/spec.html#id2805071

Copy link
Member

Choose a reason for hiding this comment

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

Then maybe we shouldn't do this at all, YAML can only emit strings so any YAML being converted JSON can only return JSON of the types that are valid in YAML. The conversion of JSON -> YAML -> JSON simply is lossy by the fact that type wise YAML is a subset of JSON.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about rewrite a basic object for json and yaml, because many same methods both in json and yaml ?

@icyleaf icyleaf closed this May 3, 2017
@RX14
Copy link
Contributor

RX14 commented May 3, 2017

Why was this closed?

@icyleaf
Copy link
Contributor Author

icyleaf commented May 3, 2017

agree with @jhass, i will try to think how about define a super class to json and yaml which it may be not a struct.

@icyleaf icyleaf deleted the support-yaml-any-to-json branch July 6, 2018 08:07
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.

3 participants