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

Hash(...)#from_json; NULL results in empty-hash. #7052

Closed
tuplebunny opened this issue Nov 9, 2018 · 4 comments
Closed

Hash(...)#from_json; NULL results in empty-hash. #7052

tuplebunny opened this issue Nov 9, 2018 · 4 comments

Comments

@tuplebunny
Copy link

tuplebunny commented Nov 9, 2018

Environment:

$ crystal -v
Crystal 0.27.0 (2018-11-04)

LLVM: 6.0.1
Default target: x86_64-apple-macosx

Bug:

require "json"

hash = Hash(String, String | Nil).from_json %({"a":null})

puts hash # => {}

Bug-in-action:

https://play.crystal-lang.org/#/r/5gw4

@makenowjust
Copy link
Contributor

makenowjust commented Nov 9, 2018

@asterite Four years ago you implemented skipping null in Hash values (5312eed). However I think it is too implicit behavior and makes users confused (like this issue). Could you explain the reason?

makenowjust added a commit to makenowjust/crystal that referenced this issue Nov 9, 2018
@straight-shoota
Copy link
Member

I'm the spec example, the value type is Nil, so you can't add nil to the hash. I'd suppose it's okay for this use case to remove JSON nulls when the hash value is non-nilable.
But it should add nil if the value type includes Nil.

@asterite
Copy link
Member

asterite commented Nov 9, 2018

@makenowjust good finding! You can revert that commit. If V is a union it will now work fine. My change was before we could do union from json.

@makenowjust
Copy link
Contributor

My change was before we could do union from json.

It is clear. Thank you!

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

No branches or pull requests

4 participants