I am reading the Crystal Release Notes from back(0.1.0) to front this week end.
Really lots of interesting histories and stories.
I saw some code like this in 0.16.0 release note
require "json"
require "big"
require "big/json"
enum Color
Red = 1
Green = 2
Blue = 3
end
class Lollipop
JSON.mapping({
color: Color,
diameter: BigFloat,
})
end
json = %({"color": 2, "diameter": 12.3456789123456789})
lollipop = Lollipop.from_json(json)
p lollipop # => #<Lollipop:0x10c962f30 @color=Green, @diameter=12.34567
Sure above code not works anymore because JSON.mapping
was replaced by JSON::Serializable
, right?
But, the issue is, i could not write the code for the equivalent of the above code use 1.5.0.
Following code raise error.
Unhandled exception: Expected String but was Int at line 1, column 12
parsing Lollipop#color at line 1, column 2 (JSON::SerializableError)
enum Color
Red = 1
Green = 2
Blue = 3
end
class Lollipop
include JSON::Serializable
property color : Color
property diameter : BigFloat
end
json = %({"color": 2, "diameter": 12.3456789123456789})
lollipop = Lollipop.from_json(json)
pp! lollipop
So, how to fix that?
Thank you.