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 JSON::Any#to_a and JSON::Any#to_h #3883

Conversation

makenowjust
Copy link
Contributor

It keeps values as JSON::Any. Refs #3881

For example, we can use Hash#each with JSON::Any values:

require "json"

json = JSON.parse %({"foo": 1, "bar": [2, 3]})
json.to_h.each do |key, value|
  pp key
  pp typeof(key)
  pp value
  pp typeof(value)
end

It keeps values as `JSON::Any`. Refs crystal-lang#3881
@makenowjust
Copy link
Contributor Author

I think it is useful in some case, for example we want to apply Array#zip with JSON array.

@asterite
Copy link
Member

Thank you, but this is slow because it allocates new arrays and hashes.

JSON::Any is supposed to be used to quickly get some value out of a big complex JSON, and not as the preferred way to deal with JSON. In most cases JSON.mapping can be used. If not, you'll have to use casting because JSON data is not typed.

@asterite asterite closed this Jan 13, 2017
@makenowjust makenowjust deleted the feature/json-any/to-h-to-a branch January 13, 2017 12:38
@makenowjust
Copy link
Contributor Author

@asterite Yes. I think so too.

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.

2 participants