class Date
def self.json_create(object)
def self.json_create(object) civil(*object.values_at('y', 'm', 'd', 'sg')) end
def as_json(*)
# => #
Date.json_create(x)
\Method +JSON.create+ deserializes such a hash, returning a \Date object:
# => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
x = Date.today.as_json
require 'json/add/date'
returning a 2-element hash representing +self+:
\Method Date#as_json serializes +self+,
see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
to serialize and deserialize a \Date object;
Methods Date#as_json and +Date.json_create+ may be used
def as_json(*) { JSON.create_id => self.class.name, 'y' => year, 'm' => month, 'd' => day, 'sg' => start, } end
def to_json(*args)
{"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
Output:
puts Date.today.to_json
require 'json/add/date'
Returns a JSON string representing +self+:
def to_json(*args) as_json.to_json(*args) end