summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2011-06-20 12:03:13 +0200
committerFlorian Frank <flori@ping.de>2011-06-20 12:03:13 +0200
commit9e41c1997c096330634e5596db8265eb7e7072f9 (patch)
treebf5b4f419aeb71f31fb971f69be416f0ff25d028 /lib
parentdd3a6b42044316ef3ae5b68e8c1836004e125e07 (diff)
downloadjson-9e41c1997c096330634e5596db8265eb7e7072f9.tar.gz
Define as_json methods, so rails can make objects
Diffstat (limited to 'lib')
-rw-r--r--lib/json/add/core.rb147
1 files changed, 106 insertions, 41 deletions
diff --git a/lib/json/add/core.rb b/lib/json/add/core.rb
index 955fdc6..9c06a9b 100644
--- a/lib/json/add/core.rb
+++ b/lib/json/add/core.rb
@@ -8,12 +8,18 @@ require 'date'
# Symbol serialization/deserialization
class Symbol
- # Stores class name (Symbol) with String representation of Symbol as a JSON string.
- def to_json(*a)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
- 's' => to_s,
- }.to_json(*a)
+ 's' => to_s,
+ }
+ 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
@@ -36,43 +42,60 @@ class Time
at(object['s'], object['n'] / 1000)
end
end
-
- # Stores class name (Time) with number of seconds since epoch and number of microseconds for Time as JSON string
- def to_json(*args)
+
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
- 's' => tv_sec,
- 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
- }.to_json(*args)
+ 's' => tv_sec,
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
+ }
+ 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
end
# Date serialization/deserialization
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.
+ # 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)
-
- # 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)
+
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
'y' => year,
'm' => month,
'd' => day,
'sg' => start,
- }.to_json(*args)
+ }
+ 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
end
# DateTime serialization/deserialization
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.
+ # 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('/')
@@ -86,9 +109,10 @@ class DateTime
end
alias start sg unless method_defined?(:start)
-
- # 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)
+
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
'y' => year,
@@ -99,80 +123,121 @@ class DateTime
'S' => sec,
'of' => offset.to_s,
'sg' => start,
- }.to_json(*args)
+ }
+ 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
end
# Range serialization/deserialization
class Range
- # Deserializes JSON string by constructing new Range object with arguments <tt>a</tt> serialized by <tt>to_json</tt>.
+ # Deserializes JSON string by constructing new Range object with arguments
+ # <tt>a</tt> serialized by <tt>to_json</tt>.
def self.json_create(object)
new(*object['a'])
end
- # Stores class name (Range) with JSON array of arguments <tt>a</tt> which include <tt>first</tt> (integer), <tt>last</tt> (integer), and <tt>exclude_end?</tt> (boolean) as JSON string.
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
- JSON.create_id => self.class.name,
- 'a' => [ first, last, exclude_end? ]
- }.to_json(*args)
+ JSON.create_id => self.class.name,
+ 'a' => [ first, last, exclude_end? ]
+ }
+ end
+
+ # Stores class name (Range) with JSON array of arguments <tt>a</tt> which
+ # include <tt>first</tt> (integer), <tt>last</tt> (integer), and
+ # <tt>exclude_end?</tt> (boolean) as JSON string.
+ def to_json(*args)
+ as_json.to_json(*args)
end
end
# Struct serialization/deserialization
class Struct
- # Deserializes JSON string by constructing new Struct object with values <tt>v</tt> serialized by <tt>to_json</tt>.
+ # 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
- # Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string. Only named structs are supported.
- def to_json(*args)
+ # 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!"
{
JSON.create_id => klass,
- 'v' => values,
- }.to_json(*args)
+ 'v' => values,
+ }
+ 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
end
# Exception serialization/deserialization
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>
+ # 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
- # Stores class name (Exception) with message <tt>m</tt> and backtrace array <tt>b</tt> as JSON string
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
- 'm' => message,
- 'b' => backtrace,
- }.to_json(*args)
+ 'm' => message,
+ 'b' => backtrace,
+ }
+ 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
end
# Regexp serialization/deserialization
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>
+ # 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
- # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt> (Regexp or String) as JSON string
- def to_json(*)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
'o' => options,
's' => source,
- }.to_json
+ }
+ end
+
+ # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
+ # (Regexp or String) as JSON string
+ def to_json(*)
+ as_json.to_json
end
end