module

Serializable::Object

Constructors

new(*, with_db_result_set rs : DB::ResultSet)
Source
new(*, with_json_pull_parser pull : ::JSON::PullParser)
Source
new(*, with_msgpack_unpacker pull : MessagePack::Unpacker)
Source

Instance methods

to_json(json : ::JSON::Builder)
Source
to_msgpack(packer : MessagePack::Packer)
Source

Macros

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>
Source
use_msgpack_discriminator(field, mapping)
Source

Nested types