Crystal: Allow `record` to support default values when using *::Serializable

Created on 6 Apr 2020  路  1Comment  路  Source: crystal-lang/crystal

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.

bug topicserialization

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RX14 picture RX14  路  3Comments

asterite picture asterite  路  3Comments

cjgajard picture cjgajard  路  3Comments

Sija picture Sija  路  3Comments

asterite picture asterite  路  3Comments