summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/add/core.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/lib/json/add/core.rb')
-rw-r--r--ext/json/lib/json/add/core.rb41
1 files changed, 28 insertions, 13 deletions
diff --git a/ext/json/lib/json/add/core.rb b/ext/json/lib/json/add/core.rb
index 630ed6e..4423e7a 100644
--- a/ext/json/lib/json/add/core.rb
+++ b/ext/json/lib/json/add/core.rb
@@ -9,14 +9,21 @@ require 'date'
class Time
def self.json_create(object)
- at(*object.values_at('s', 'u'))
+ if usec = object.delete('u') # used to be tv_usec -> tv_nsec
+ object['n'] = usec * 1000
+ end
+ if respond_to?(:tv_nsec)
+ at(*object.values_at('s', 'n'))
+ else
+ at(object['s'], object['n'] / 1000)
+ end
end
def to_json(*args)
{
- 'json_class' => self.class.name.to_s,
+ 'json_class' => self.class.name,
's' => tv_sec,
- 'u' => tv_usec,
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
}.to_json(*args)
end
end
@@ -26,13 +33,15 @@ class Date
civil(*object.values_at('y', 'm', 'd', 'sg'))
end
+ alias start sg unless method_defined?(:start)
+
def to_json(*args)
{
- 'json_class' => self.class.name.to_s,
+ 'json_class' => self.class.name,
'y' => year,
'm' => month,
'd' => day,
- 'sg' => @sg,
+ 'sg' => start,
}.to_json(*args)
end
end
@@ -41,14 +50,20 @@ class DateTime
def self.json_create(object)
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
of_a, of_b = object['of'].split('/')
- args << Rational(of_a.to_i, of_b.to_i)
+ 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)
+
def to_json(*args)
{
- 'json_class' => self.class.name.to_s,
+ 'json_class' => self.class.name,
'y' => year,
'm' => month,
'd' => day,
@@ -56,7 +71,7 @@ class DateTime
'M' => min,
'S' => sec,
'of' => offset.to_s,
- 'sg' => @sg,
+ 'sg' => start,
}.to_json(*args)
end
end
@@ -68,7 +83,7 @@ class Range
def to_json(*args)
{
- 'json_class' => self.class.name.to_s,
+ 'json_class' => self.class.name,
'a' => [ first, last, exclude_end? ]
}.to_json(*args)
end
@@ -80,8 +95,8 @@ class Struct
end
def to_json(*args)
- klass = self.class.name.to_s
- klass.empty? and raise JSON::JSONError, "Only named structs are supported!"
+ klass = self.class.name
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
{
'json_class' => klass,
'v' => values,
@@ -98,7 +113,7 @@ class Exception
def to_json(*args)
{
- 'json_class' => self.class.name.to_s,
+ 'json_class' => self.class.name,
'm' => message,
'b' => backtrace,
}.to_json(*args)
@@ -112,7 +127,7 @@ class Regexp
def to_json(*)
{
- 'json_class' => self.class.name.to_s,
+ 'json_class' => self.class.name,
'o' => options,
's' => source,
}.to_json