diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-20 17:41:14 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-20 17:41:14 +0000 |
commit | e8eb95ec33334f40f3f3bbcbf16404aee07dfa8e (patch) | |
tree | c7fade34f792e138365b59af246ea316616c04e7 /ext/json/lib/json/add | |
parent | a91cd48aa19dd31ec5abb6e4b982f2077bedcbcd (diff) |
* ext/json: import JSON v 1.1.3.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19436 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/json/lib/json/add')
-rw-r--r-- | ext/json/lib/json/add/core.rb | 41 |
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 630ed6e4b7..4423e7ad75 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 |