aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gems/json-1.5.1/lib/json/add
diff options
context:
space:
mode:
authorSeb Bacon <seb@seb-virtual-machine.(none)>2011-03-25 12:37:59 +0000
committerSeb Bacon <seb@seb-virtual-machine.(none)>2011-03-25 12:37:59 +0000
commit22c25e42847d41924d43d31f3ef67be1ae4a155e (patch)
tree3534d4842184ab0a769f606b39ff0447c246de7e /vendor/gems/json-1.5.1/lib/json/add
parent1380232e20da4c0b9ce72efb43c1f4d1978ab5d7 (diff)
include json gem (for tests to pass)
Diffstat (limited to 'vendor/gems/json-1.5.1/lib/json/add')
-rw-r--r--vendor/gems/json-1.5.1/lib/json/add/core.rb147
-rw-r--r--vendor/gems/json-1.5.1/lib/json/add/rails.rb8
2 files changed, 155 insertions, 0 deletions
diff --git a/vendor/gems/json-1.5.1/lib/json/add/core.rb b/vendor/gems/json-1.5.1/lib/json/add/core.rb
new file mode 100644
index 000000000..7a901d0b2
--- /dev/null
+++ b/vendor/gems/json-1.5.1/lib/json/add/core.rb
@@ -0,0 +1,147 @@
+# This file contains implementations of ruby core's custom objects for
+# serialisation/deserialisation.
+
+unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
+ require 'json'
+end
+require 'date'
+
+class Symbol
+ def to_json(*a)
+ {
+ JSON.create_id => self.class.name,
+ 's' => to_s,
+ }.to_json(*a)
+ end
+
+ def self.json_create(o)
+ o['s'].to_sym
+ end
+end
+
+class Time
+ def self.json_create(object)
+ 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.create_id => self.class.name,
+ 's' => tv_sec,
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
+ }.to_json(*args)
+ end
+end
+
+class Date
+ def self.json_create(object)
+ civil(*object.values_at('y', 'm', 'd', 'sg'))
+ end
+
+ alias start sg unless method_defined?(:start)
+
+ def to_json(*args)
+ {
+ JSON.create_id => self.class.name,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'sg' => start,
+ }.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('/')
+ 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.create_id => self.class.name,
+ 'y' => year,
+ 'm' => month,
+ 'd' => day,
+ 'H' => hour,
+ 'M' => min,
+ 'S' => sec,
+ 'of' => offset.to_s,
+ 'sg' => start,
+ }.to_json(*args)
+ end
+end
+
+class Range
+ def self.json_create(object)
+ new(*object['a'])
+ end
+
+ def to_json(*args)
+ {
+ JSON.create_id => self.class.name,
+ '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
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
+ {
+ JSON.create_id => 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.create_id => self.class.name,
+ '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.create_id => self.class.name,
+ 'o' => options,
+ 's' => source,
+ }.to_json
+ end
+end
diff --git a/vendor/gems/json-1.5.1/lib/json/add/rails.rb b/vendor/gems/json-1.5.1/lib/json/add/rails.rb
new file mode 100644
index 000000000..af95e52d4
--- /dev/null
+++ b/vendor/gems/json-1.5.1/lib/json/add/rails.rb
@@ -0,0 +1,8 @@
+# This file used to implementations of rails custom objects for
+# serialisation/deserialisation and is obsoleted now.
+
+unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
+ require 'json'
+end
+
+$DEBUG and warn "required json/add/rails which is obsolete now!"