Skip to content

Instantly share code, notes, and snippets.

@Daniel-Worrall
Last active November 15, 2019 00:36
Show Gist options
  • Save Daniel-Worrall/99092463a1f611a43241adf515e57b98 to your computer and use it in GitHub Desktop.
Save Daniel-Worrall/99092463a1f611a43241adf515e57b98 to your computer and use it in GitHub Desktop.
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