Skip to content

Instantly share code, notes, and snippets.

@Daniel-Worrall
Last active Nov 15, 2019
Embed
What would you like to do?
class Anime
include JSON::Serializable
property id : Int32
property title : String
property alt_title : String?
property season : Int32
@[JSON::Field(converter: BoolConverter)]
property ongoing : Bool
property hb_id : Int32?
@[JSON::Field(converter: TimeConverter)]
property created_at : Time
@[JSON::Field(converter: TimeConverter)]
property updated_at : Time
@[JSON::Field(converter: BoolConverter)]
property hidden : Bool
property mal_id : Int32?
# property slug : Slug
end
module BoolConverter
def self.from_json(parser : JSON::PullParser)
parser.read_int.zero?
end
def self.to_json(value : Bool, builder : JSON::Builder)
value ? 1 : 0
end
end
module TimeConverter
FORMAT = Time::Format.new("%Y-%m-%d %H:%M:%S", Time::Location::UTC)
def self.from_json(parser : JSON::PullParser)
FORMAT.parse(parser.read_string)
end
def self.to_json(value : Time, builder : JSON::Builder)
FORMAT.format(value)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment