Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Date#as_json

(*untyped) -> ::Hash[::String, ::String | ::Integer | ::Float]
as_json(*)

Methods Date#as_json and Date.json_create may be used to serialize and deserialize a Date object; see Marshal.

Method Date#as_json serializes self, returning a 2-element hash representing self:

Example
require 'json/add/date'
x = Date.today.as_json
# => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}

Method JSON.create deserializes such a hash, returning a Date object:

Example
Date.json_create(x)
# => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
See also #asctime #ctime #amjd #cwday also in 4.0 · 3.3 · 3.2 · 3.1 · 3.0