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

Serialize nil as ~ in YAML #6138

Closed
wants to merge 1 commit into from

Conversation

Sija
Copy link
Contributor

@Sija Sija commented May 27, 2018

Result with following code:

puts({"foo" => nil}.to_yaml)

before:

---
foo: 

after:

---
foo: ~

Refs #6137

@asterite
Copy link
Member

But why?

$ irb
irb(main):001:0> require "yaml"
=> true
irb(main):002:0> puts({"foo" => nil}.to_yaml)
---
foo: 
=> nil

Crystal behaves like Ruby.

@asterite
Copy link
Member

If you want, maybe that can be a configuration of a YAML::Builder. But I think this is too much. Empty is null in YAML, as was noted in the comments.

@Sija
Copy link
Contributor Author

Sija commented May 27, 2018

IMO ~ is more expressive - it is something instead of nothing and therefore alleviates confusion about the value type stored there - as seen in the linked issue.

Also, I believe it's not a big trade off in terms of size - just 1 character more for bit of clarity.

@oprypin
Copy link
Member

oprypin commented May 28, 2018

Maybe rename this to "Serialize nil as ~ in YAML". The current PR title really confused me.

Example in description would also help

@Sija Sija changed the title Use ~ character for Nil#to_yaml Serialize nil as ~ in YAML May 28, 2018
@ysbaddaden
Copy link
Contributor

This issue is about personal taste. No solution is better than the other.

My taste follows @asterite here. I prefer having nothing than ~ which I never saw before, despite using YAML for many 10+ years, and I would naively understand it as a "~" string (i.e. it's confusing).

@fgimian
Copy link
Contributor

fgimian commented May 28, 2018

Just to clarify (as I opened the related issue), that issue was mainly due to my lack of knowledge on how null could be represented in YAML. Coming from Python's PyYAML, I was used to seeing the word null for null values, but it turned out that this was only one of the many possible values that the YAML spec offers. i personally have never seen ~ until it was mentioned in that issue either, and I work with YAML heaps!

I personally believe the current behaviour is the most appropriate. Not only does it follow Ruby's style but also printing any nil value in Crystal prints empty output which makes the YAML behaviour consistent.

Users who want different behaviour can very easily patch this as follows:

struct Nil
  def to_yaml(yaml : YAML::Nodes::Builder)
    yaml.scalar "~"
  end
end

@straight-shoota
Copy link
Member

While I initially brought up the suggestions to use ~, I wouldn't recommend a change unless there is some real evidence for improvement.

The only (minor) case where a change might perhaps be useful is nil.to_yaml - currently it produces --- \n...n which looks a bit strange to people not familiar with the ... document end identifier. Here, a bit more expressiveness like --- \nnull\n...\n could be nice.

@jkthorne
Copy link
Contributor

I would prefer null over ~ but I prefer `` over the other ones. I would just not recognize ~. Seems like the spec if flexible though on what can be used.

@ysbaddaden
Copy link
Contributor

I believe we'll stick with the current behavior. Closing.

@ysbaddaden ysbaddaden closed this Jun 12, 2018
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.

None yet

7 participants