Serializable::Object
Constructors
new(*, with_db_result_set rs : DB::ResultSet)
Sourcenew(*, with_json_pull_parser pull : ::JSON::PullParser)
Sourcenew(*, with_msgpack_unpacker pull : MessagePack::Unpacker)
SourceInstance methods
to_json(json : ::JSON::Builder)
Sourceto_msgpack(packer : MessagePack::Packer)
SourceMacros
use_json_discriminator(field, mapping)
Tells this class to decode JSON by using a field as a discriminator.
- field must be the field name to use as a discriminator
- mapping must be a hash or named tuple where each key-value pair maps a discriminator value to a class to deserialize
For example:
require "json"
abstract class Shape
include JSON::Serializable
use_json_discriminator "type", {point: Point, circle: Circle}
property type : String
end
class Point < Shape
property x : Int32
property y : Int32
end
class Circle < Shape
property x : Int32
property y : Int32
property radius : Int32
end
Shape.from_json(%({"type": "point", "x": 1, "y": 2})) # => #<Point:0x10373ae20 @type="point", @x=1, @y=2>
Shape.from_json(%({"type": "circle", "x": 1, "y": 2, "radius": 3})) # => #<Circle:0x106a4cea0 @type="circle", @x=1, @y=2, @radius=3>
use_msgpack_discriminator(field, mapping)
Source