summaryrefslogtreecommitdiff
path: root/ext/json
diff options
context:
space:
mode:
authorBurdette Lamar <[email protected]>2023-12-04 18:59:48 -0600
committerHiroshi SHIBATA <[email protected]>2023-12-05 12:04:09 +0900
commitc8faaf4c7edf67a19786cb9ba53805e5b813b918 ()
tree79c5ee7934f8dabe7d185a63b1cfc148e729811f /ext/json
parent70740deea793274f6e38a7b7fc3688aa709fd1d8 (diff)
[flori/json] [DOC] RDoc for additions
(https://.com/flori/json/pull/557) * RDoc for additions * Update lib/json/add/time.rb Co-authored-by: Hiroshi SHIBATA <[email protected]> --------- https://.com/flori/json/commit/3f2efd60f7 Co-authored-by: Hiroshi SHIBATA <[email protected]>
-rw-r--r--ext/json/lib/json/add/bigdecimal.rb38
-rw-r--r--ext/json/lib/json/add/complex.rb33
-rw-r--r--ext/json/lib/json/add/date.rb32
-rw-r--r--ext/json/lib/json/add/date_time.rb33
-rw-r--r--ext/json/lib/json/add/exception.rb30
-rw-r--r--ext/json/lib/json/add/ostruct.rb32
-rw-r--r--ext/json/lib/json/add/range.rb40
-rw-r--r--ext/json/lib/json/add/rational.rb32
-rw-r--r--ext/json/lib/json/add/regexp.rb32
-rw-r--r--ext/json/lib/json/add/set.rb31
-rw-r--r--ext/json/lib/json/add/struct.rb34
-rw-r--r--ext/json/lib/json/add/symbol.rb31
-rw-r--r--ext/json/lib/json/add/time.rb31
13 files changed, 344 insertions, 85 deletions
@@ -8,16 +8,30 @@ rescue LoadError
end
class BigDecimal
- # Import a JSON Marshalled object.
- #
- # method used for JSON marshalling support.
def self.json_create(object)
BigDecimal._load object['b']
end
- # Marshal the object to JSON.
#
- # method used for JSON marshalling support.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -25,7 +39,19 @@ class BigDecimal
}
end
- # return the JSON value
def to_json(*args)
as_json.to_json(*args)
end
@@ -5,14 +5,27 @@ end
class Complex
- # Deserializes JSON string by converting Real value <tt>r</tt>, imaginary
- # value <tt>i</tt>, to a Complex object.
def self.json_create(object)
Complex(object['r'], object['i'])
end
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -21,7 +34,17 @@ class Complex
}
end
- # Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
def to_json(*args)
as_json.to_json(*args)
end
@@ -6,16 +6,29 @@ require 'date'
class Date
- # Deserializes JSON string by converting Julian year <tt>y</tt>, month
- # <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
def self.json_create(object)
civil(*object.values_at('y', 'm', 'd', 'sg'))
end
alias start sg unless method_defined?(:start)
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -26,8 +39,15 @@ class Date
}
end
- # Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
- # <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
def to_json(*args)
as_json.to_json(*args)
end
@@ -6,9 +6,7 @@ require 'date'
class DateTime
- # Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
- # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
- # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
def self.json_create(object)
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
of_a, of_b = object['of'].split('/')
@@ -23,8 +21,21 @@ class DateTime
alias start sg unless method_defined?(:start)
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -39,9 +50,15 @@ class DateTime
}
end
- # Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
- # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
- # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
def to_json(*args)
as_json.to_json(*args)
end
@@ -5,16 +5,27 @@ end
class Exception
- # Deserializes JSON string by constructing new Exception object with message
- # <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
def self.json_create(object)
result = new(object['m'])
result.set_backtrace object['b']
result
end
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -23,8 +34,15 @@ class Exception
}
end
- # Stores class name (Exception) with message <tt>m</tt> and backtrace array
- # <tt>b</tt> as JSON string
def to_json(*args)
as_json.to_json(*args)
end
@@ -6,14 +6,27 @@ require 'ostruct'
class OpenStruct
- # Deserializes JSON string by constructing new Struct object with values
- # <tt>t</tt> serialized by <tt>to_json</tt>.
def self.json_create(object)
new(object['t'] || object[:t])
end
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
klass = self.class.name
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
@@ -23,8 +36,15 @@ class OpenStruct
}
end
- # Stores class name (OpenStruct) with this struct's values <tt>t</tt> as a
- # JSON string.
def to_json(*args)
as_json.to_json(*args)
end
@@ -5,24 +5,28 @@ end
class Range
- # Returns a new \Range object constructed from <tt>object['a']</tt>,
- # which must be an array of values suitable for a call to Range.new:
- #
- # require 'json/add/range'
- # Range.json_create({"a"=>[1, 4]}) # => 1..4
- # Range.json_create({"a"=>[1, 4, true]}) # => 1...4
- # Range.json_create({"a" => ['a', 'd']}) # => "a".."d"
- #
def self.json_create(object)
new(*object['a'])
end
- # Returns a 2-element hash representing +self+:
#
# require 'json/add/range'
- # (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
- # (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
- # ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
#
def as_json(*)
{
@@ -34,9 +38,15 @@ class Range
# Returns a JSON string representing +self+:
#
# require 'json/add/range'
- # (1..4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,false]}"
- # (1...4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,true]}"
- # ('a'..'d').to_json # => "{\"json_class\":\"Range\",\"a\":[\"a\",\"d\",false]}"
#
def to_json(*args)
as_json.to_json(*args)
@@ -4,14 +4,28 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
end
class Rational
- # Deserializes JSON string by converting numerator value <tt>n</tt>,
- # denominator value <tt>d</tt>, to a Rational object.
def self.json_create(object)
Rational(object['n'], object['d'])
end
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -20,7 +34,15 @@ class Rational
}
end
- # Stores class name (Rational) along with numerator value <tt>n</tt> and denominator value <tt>d</tt> as JSON string
def to_json(*args)
as_json.to_json(*args)
end
@@ -5,15 +5,26 @@ end
class Regexp
- # Deserializes JSON string by constructing new Regexp object with source
- # <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by
- # <tt>to_json</tt>
def self.json_create(object)
new(object['s'], object['o'])
end
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -22,8 +33,15 @@ class Regexp
}
end
- # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
- # (Regexp or String) as JSON string
def to_json(*args)
as_json.to_json(*args)
end
@@ -4,16 +4,27 @@ end
defined?(::Set) or require 'set'
class Set
- # Import a JSON Marshalled object.
- #
- # method used for JSON marshalling support.
def self.json_create(object)
new object['a']
end
- # Marshal the object to JSON.
#
- # method used for JSON marshalling support.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -21,7 +32,15 @@ class Set
}
end
- # return the JSON value
def to_json(*args)
as_json.to_json(*args)
end
@@ -5,14 +5,28 @@ end
class Struct
- # Deserializes JSON string by constructing new Struct object with values
- # <tt>v</tt> serialized by <tt>to_json</tt>.
def self.json_create(object)
new(*object['v'])
end
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
klass = self.class.name
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
@@ -22,8 +36,16 @@ class Struct
}
end
- # Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string.
- # Only named structs are supported.
def to_json(*args)
as_json.to_json(*args)
end
@@ -1,11 +1,26 @@
#frozen_string_literal: false
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json'
end
class Symbol
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -13,12 +28,20 @@ class Symbol
}
end
- # Stores class name (Symbol) with String representation of Symbol as a JSON string.
def to_json(*a)
as_json.to_json(*a)
end
- # Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol
def self.json_create(o)
o['s'].to_sym
end
@@ -5,7 +5,7 @@ end
class Time
- # Deserializes JSON string by converting time since epoch to Time
def self.json_create(object)
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
object['n'] = usec * 1000
@@ -17,8 +17,22 @@ class Time
end
end
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
def as_json(*)
nanoseconds = [ tv_usec * 1000 ]
respond_to?(:tv_nsec) and nanoseconds << tv_nsec
@@ -30,8 +44,15 @@ class Time
}
end
- # Stores class name (Time) with number of seconds since epoch and number of
- # microseconds for Time as JSON string
def to_json(*args)
as_json.to_json(*args)
end