summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/add/core.rb
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-12-04 08:09:44 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-12-04 08:09:44 +0000
commit825ce503c0e52edb944def195be0d3d55e3e71b3 (patch)
treefce260b670515f356a94d399fe97853b65ae1268 /ext/json/lib/json/add/core.rb
parent86f1cff0eb8fa3c4d5925fb8fe5eac31a323bb84 (diff)
* lib/json.rb, lib/json/*: moved to ext/json/lib.
-- M trunk/ChangeLog D trunk/lib/json D trunk/lib/json.rb A trunk/ext/json/lib A trunk/ext/json/lib/json A trunk/ext/json/lib/json.rb A trunk/ext/json/extconf.rb git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14100 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/json/lib/json/add/core.rb')
-rw-r--r--ext/json/lib/json/add/core.rb120
1 files changed, 120 insertions, 0 deletions
diff --git a/ext/json/lib/json/add/core.rb b/ext/json/lib/json/add/core.rb
new file mode 100644
index 0000000000..630ed6e4b7
--- /dev/null
+++ b/ext/json/lib/json/add/core.rb
@@ -0,0 +1,120 @@
+# This file contains implementations of ruby core's custom objects for
+# serialisation/deserialisation.
+
+unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
+ ::JSON::JSON_LOADED
+ require 'json'
+end
+require 'date'
+
+class Time
+ def self.json_create(object)
+ at(*object.values_at('s', 'u'))
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name.to_s,
+ 's' => tv_sec,
+ 'u' => tv_usec,
+ }.to_json(*args)
+ end
+end
+
+class Date
+ def self.json_create(object)
+ civil(*object.values_at('y', 'm', 'd', 'sg'))
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name.to_s,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'sg' => @sg,
+ }.to_json(*args)
+ end
+end
+
+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)
+ args << object['sg']
+ civil(*args)
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name.to_s,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'H' => hour,
+ 'M' => min,
+ 'S' => sec,
+ 'of' => offset.to_s,
+ 'sg' => @sg,
+ }.to_json(*args)
+ end
+end
+
+class Range
+ def self.json_create(object)
+ new(*object['a'])
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name.to_s,
+ 'a' => [ first, last, exclude_end? ]
+ }.to_json(*args)
+ end
+end
+
+class Struct
+ def self.json_create(object)
+ new(*object['v'])
+ end
+
+ def to_json(*args)
+ klass = self.class.name.to_s
+ klass.empty? and raise JSON::JSONError, "Only named structs are supported!"
+ {
+ 'json_class' => klass,
+ 'v' => values,
+ }.to_json(*args)
+ end
+end
+
+class Exception
+ def self.json_create(object)
+ result = new(object['m'])
+ result.set_backtrace object['b']
+ result
+ end
+
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name.to_s,
+ 'm' => message,
+ 'b' => backtrace,
+ }.to_json(*args)
+ end
+end
+
+class Regexp
+ def self.json_create(object)
+ new(object['s'], object['o'])
+ end
+
+ def to_json(*)
+ {
+ 'json_class' => self.class.name.to_s,
+ 'o' => options,
+ 's' => source,
+ }.to_json
+ end
+end