This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
begin
|
||||
require 'bigdecimal'
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
class BigDecimal
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
BigDecimal._load object['b']
|
||||
end
|
||||
|
||||
# Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used
|
||||
# to serialize and deserialize a \BigDecimal object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>BigDecimal#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/bigdecimal'
|
||||
# x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
|
||||
# y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
|
||||
# z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object:
|
||||
#
|
||||
# BigDecimal.json_create(x) # => 0.2e1
|
||||
# BigDecimal.json_create(y) # => 0.2e1
|
||||
# BigDecimal.json_create(z) # => 0.2e1
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'b' => _dump.force_encoding(Encoding::UTF_8),
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/bigdecimal'
|
||||
# puts BigDecimal(2).to_json
|
||||
# puts BigDecimal(2.0, 4).to_json
|
||||
# puts BigDecimal(Complex(2, 0)).to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"BigDecimal","b":"27:0.2e1"}
|
||||
# {"json_class":"BigDecimal","b":"36:0.2e1"}
|
||||
# {"json_class":"BigDecimal","b":"27:0.2e1"}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end if defined?(::BigDecimal)
|
||||
@@ -0,0 +1,51 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Complex
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
Complex(object['r'], object['i'])
|
||||
end
|
||||
|
||||
# Methods <tt>Complex#as_json</tt> and +Complex.json_create+ may be used
|
||||
# to serialize and deserialize a \Complex object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Complex#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/complex'
|
||||
# x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0}
|
||||
# y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Complex object:
|
||||
#
|
||||
# Complex.json_create(x) # => (2+0i)
|
||||
# Complex.json_create(y) # => (2.0+4i)
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'r' => real,
|
||||
'i' => imag,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/complex'
|
||||
# puts Complex(2).to_json
|
||||
# puts Complex(2.0, 4).to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Complex","r":2,"i":0}
|
||||
# {"json_class":"Complex","r":2.0,"i":4}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
# This file requires the implementations of ruby core's custom objects for
|
||||
# serialisation/deserialisation.
|
||||
|
||||
require 'json/add/date'
|
||||
require 'json/add/date_time'
|
||||
require 'json/add/exception'
|
||||
require 'json/add/range'
|
||||
require 'json/add/regexp'
|
||||
require 'json/add/struct'
|
||||
require 'json/add/symbol'
|
||||
require 'json/add/time'
|
||||
@@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
require 'date'
|
||||
|
||||
class Date
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
civil(*object.values_at('y', 'm', 'd', 'sg'))
|
||||
end
|
||||
|
||||
alias start sg unless method_defined?(:start)
|
||||
|
||||
# Methods <tt>Date#as_json</tt> and +Date.json_create+ may be used
|
||||
# to serialize and deserialize a \Date object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Date#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# 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:
|
||||
#
|
||||
# Date.json_create(x)
|
||||
# # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'y' => year,
|
||||
'm' => month,
|
||||
'd' => day,
|
||||
'sg' => start,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/date'
|
||||
# puts Date.today.to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,67 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
require 'date'
|
||||
|
||||
class DateTime
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
|
||||
of_a, of_b = object['of'].split('/')
|
||||
if of_b and of_b != '0'
|
||||
args << Rational(of_a.to_i, of_b.to_i)
|
||||
else
|
||||
args << of_a
|
||||
end
|
||||
args << object['sg']
|
||||
civil(*args)
|
||||
end
|
||||
|
||||
alias start sg unless method_defined?(:start)
|
||||
|
||||
# Methods <tt>DateTime#as_json</tt> and +DateTime.json_create+ may be used
|
||||
# to serialize and deserialize a \DateTime object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>DateTime#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/datetime'
|
||||
# x = DateTime.now.as_json
|
||||
# # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \DateTime object:
|
||||
#
|
||||
# DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'y' => year,
|
||||
'm' => month,
|
||||
'd' => day,
|
||||
'H' => hour,
|
||||
'M' => min,
|
||||
'S' => sec,
|
||||
'of' => offset.to_s,
|
||||
'sg' => start,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/datetime'
|
||||
# puts DateTime.now.to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Exception
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
result = new(object['m'])
|
||||
result.set_backtrace object['b']
|
||||
result
|
||||
end
|
||||
|
||||
# Methods <tt>Exception#as_json</tt> and +Exception.json_create+ may be used
|
||||
# to serialize and deserialize a \Exception object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Exception#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/exception'
|
||||
# x = Exception.new('Foo').as_json # => {"json_class"=>"Exception", "m"=>"Foo", "b"=>nil}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Exception object:
|
||||
#
|
||||
# Exception.json_create(x) # => #<Exception: Foo>
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'm' => message,
|
||||
'b' => backtrace,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/exception'
|
||||
# puts Exception.new('Foo').to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Exception","m":"Foo","b":null}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
begin
|
||||
require 'ostruct'
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
class OpenStruct
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
new(object['t'] || object[:t])
|
||||
end
|
||||
|
||||
# Methods <tt>OpenStruct#as_json</tt> and +OpenStruct.json_create+ may be used
|
||||
# to serialize and deserialize a \OpenStruct object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>OpenStruct#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/ostruct'
|
||||
# x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json
|
||||
# # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \OpenStruct object:
|
||||
#
|
||||
# OpenStruct.json_create(x)
|
||||
# # => #<OpenStruct name='Rowdy', age=nil>
|
||||
#
|
||||
def as_json(*)
|
||||
klass = self.class.name
|
||||
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
||||
{
|
||||
JSON.create_id => klass,
|
||||
't' => table,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/ostruct'
|
||||
# puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end if defined?(::OpenStruct)
|
||||
@@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Range
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
new(*object['a'])
|
||||
end
|
||||
|
||||
# Methods <tt>Range#as_json</tt> and +Range.json_create+ may be used
|
||||
# to serialize and deserialize a \Range object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Range#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/range'
|
||||
# x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
|
||||
# y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
|
||||
# z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Range object:
|
||||
#
|
||||
# Range.json_create(x) # => 1..4
|
||||
# Range.json_create(y) # => 1...4
|
||||
# Range.json_create(z) # => "a".."d"
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'a' => [ first, last, exclude_end? ]
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/range'
|
||||
# puts (1..4).to_json
|
||||
# puts (1...4).to_json
|
||||
# puts ('a'..'d').to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Range","a":[1,4,false]}
|
||||
# {"json_class":"Range","a":[1,4,true]}
|
||||
# {"json_class":"Range","a":["a","d",false]}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,49 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Rational
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
Rational(object['n'], object['d'])
|
||||
end
|
||||
|
||||
# Methods <tt>Rational#as_json</tt> and +Rational.json_create+ may be used
|
||||
# to serialize and deserialize a \Rational object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Rational#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/rational'
|
||||
# x = Rational(2, 3).as_json
|
||||
# # => {"json_class"=>"Rational", "n"=>2, "d"=>3}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Rational object:
|
||||
#
|
||||
# Rational.json_create(x)
|
||||
# # => (2/3)
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'n' => numerator,
|
||||
'd' => denominator,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/rational'
|
||||
# puts Rational(2, 3).to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Rational","n":2,"d":3}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,48 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Regexp
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
new(object['s'], object['o'])
|
||||
end
|
||||
|
||||
# Methods <tt>Regexp#as_json</tt> and +Regexp.json_create+ may be used
|
||||
# to serialize and deserialize a \Regexp object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Regexp#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/regexp'
|
||||
# x = /foo/.as_json
|
||||
# # => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Regexp object:
|
||||
#
|
||||
# Regexp.json_create(x) # => /foo/
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'o' => options,
|
||||
's' => source,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/regexp'
|
||||
# puts /foo/.to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Regexp","o":0,"s":"foo"}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,48 @@
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
defined?(::Set) or require 'set'
|
||||
|
||||
class Set
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
new object['a']
|
||||
end
|
||||
|
||||
# Methods <tt>Set#as_json</tt> and +Set.json_create+ may be used
|
||||
# to serialize and deserialize a \Set object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Set#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/set'
|
||||
# x = Set.new(%w/foo bar baz/).as_json
|
||||
# # => {"json_class"=>"Set", "a"=>["foo", "bar", "baz"]}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Set object:
|
||||
#
|
||||
# Set.json_create(x) # => #<Set: {"foo", "bar", "baz"}>
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
'a' => to_a,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/set'
|
||||
# puts Set.new(%w/foo bar baz/).to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Set","a":["foo","bar","baz"]}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Struct
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
new(*object['v'])
|
||||
end
|
||||
|
||||
# Methods <tt>Struct#as_json</tt> and +Struct.json_create+ may be used
|
||||
# to serialize and deserialize a \Struct object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Struct#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/struct'
|
||||
# Customer = Struct.new('Customer', :name, :address, :zip)
|
||||
# x = Struct::Customer.new.as_json
|
||||
# # => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Struct object:
|
||||
#
|
||||
# Struct::Customer.json_create(x)
|
||||
# # => #<struct Struct::Customer name=nil, address=nil, zip=nil>
|
||||
#
|
||||
def as_json(*)
|
||||
klass = self.class.name
|
||||
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
||||
{
|
||||
JSON.create_id => klass,
|
||||
'v' => values,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/struct'
|
||||
# Customer = Struct.new('Customer', :name, :address, :zip)
|
||||
# puts Struct::Customer.new.to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Symbol
|
||||
|
||||
# Methods <tt>Symbol#as_json</tt> and +Symbol.json_create+ may be used
|
||||
# to serialize and deserialize a \Symbol object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Symbol#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/symbol'
|
||||
# x = :foo.as_json
|
||||
# # => {"json_class"=>"Symbol", "s"=>"foo"}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Symbol object:
|
||||
#
|
||||
# Symbol.json_create(x) # => :foo
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
's' => to_s,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/symbol'
|
||||
# puts :foo.to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# # {"json_class":"Symbol","s":"foo"}
|
||||
#
|
||||
def to_json(state = nil, *a)
|
||||
state = ::JSON::State.from_state(state)
|
||||
if state.strict?
|
||||
super
|
||||
else
|
||||
as_json.to_json(state, *a)
|
||||
end
|
||||
end
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(o)
|
||||
o['s'].to_sym
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||
require 'json'
|
||||
end
|
||||
|
||||
class Time
|
||||
|
||||
# See #as_json.
|
||||
def self.json_create(object)
|
||||
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
|
||||
object['n'] = usec * 1000
|
||||
end
|
||||
at(object['s'], Rational(object['n'], 1000))
|
||||
end
|
||||
|
||||
# Methods <tt>Time#as_json</tt> and +Time.json_create+ may be used
|
||||
# to serialize and deserialize a \Time object;
|
||||
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
||||
#
|
||||
# \Method <tt>Time#as_json</tt> serializes +self+,
|
||||
# returning a 2-element hash representing +self+:
|
||||
#
|
||||
# require 'json/add/time'
|
||||
# x = Time.now.as_json
|
||||
# # => {"json_class"=>"Time", "s"=>1700931656, "n"=>472846644}
|
||||
#
|
||||
# \Method +JSON.create+ deserializes such a hash, returning a \Time object:
|
||||
#
|
||||
# Time.json_create(x)
|
||||
# # => 2023-11-25 11:00:56.472846644 -0600
|
||||
#
|
||||
def as_json(*)
|
||||
{
|
||||
JSON.create_id => self.class.name,
|
||||
's' => tv_sec,
|
||||
'n' => tv_nsec,
|
||||
}
|
||||
end
|
||||
|
||||
# Returns a JSON string representing +self+:
|
||||
#
|
||||
# require 'json/add/time'
|
||||
# puts Time.now.to_json
|
||||
#
|
||||
# Output:
|
||||
#
|
||||
# {"json_class":"Time","s":1700931678,"n":980650786}
|
||||
#
|
||||
def to_json(*args)
|
||||
as_json.to_json(*args)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user