https://play.crystal-lang.org/#/r/8u3w
require "json"
require "yaml"
yaml = <<-YAML
---
id: 1
YAML
json = <<-JSON
{"id":1}
JSON
record Test, id : Int32, active : Bool? = false do
include JSON::Serializable
include YAML::Serializable
end
pp Test.from_yaml yaml # => Test(@active=nil, @id=1)
pp Test.from_json json # => Test(@active=nil, @id=1)
I would expect this to set active to false if the property is missing, however it gets set to nil. Similarly, if the type of the property is not nillable, an exception saying the property is missing is raised versus just using the default value.
It does respect default values, just that record macro sets these default values in the constructor, not in the getter definitions, so they do not get "picked up" by ::Serializable.
So this is just a record feature request.
Most helpful comment
It does respect default values, just that
recordmacro sets these default values in the constructor, not in thegetterdefinitions, so they do not get "picked up" by::Serializable.So this is just a
recordfeature request.