1 # This file contains implementations of ruby core's custom objects for
2 # serialisation/deserialisation.
4 unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
9 # Symbol serialization/deserialization
11 # Returns a hash, that will be turned into a JSON object and represent this
15 JSON.create_id => self.class.name,
20 # Stores class name (Symbol) with String representation of Symbol as a JSON string.
25 # Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol
26 def self.json_create(o)
31 # Time serialization/deserialization
34 # Deserializes JSON string by converting time since epoch to Time
35 def self.json_create(object)
36 if usec = object.delete('u') # used to be tv_usec -> tv_nsec
37 object['n'] = usec * 1000
39 if instance_methods.include?(:tv_nsec)
40 at(object['s'], Rational(object['n'], 1000))
42 at(object['s'], object['n'] / 1000)
46 # Returns a hash, that will be turned into a JSON object and represent this
49 nanoseconds = [ tv_usec * 1000 ]
50 respond_to?(:tv_nsec) and nanoseconds << tv_nsec
51 nanoseconds = nanoseconds.max
53 JSON.create_id => self.class.name,
59 # Stores class name (Time) with number of seconds since epoch and number of
60 # microseconds for Time as JSON string
62 as_json.to_json(*args)
66 # Date serialization/deserialization
69 # Deserializes JSON string by converting Julian year <tt>y</tt>, month
70 # <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
71 def self.json_create(object)
72 civil(*object.values_at('y', 'm', 'd', 'sg'))
75 alias start sg unless method_defined?(:start)
77 # Returns a hash, that will be turned into a JSON object and represent this
81 JSON.create_id => self.class.name,
89 # Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
90 # <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
92 as_json.to_json(*args)
96 # DateTime serialization/deserialization
99 # Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
100 # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
101 # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
102 def self.json_create(object)
103 args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
104 of_a, of_b = object['of'].split('/')
105 if of_b and of_b != '0'
106 args << Rational(of_a.to_i, of_b.to_i)
114 alias start sg unless method_defined?(:start)
116 # Returns a hash, that will be turned into a JSON object and represent this
120 JSON.create_id => self.class.name,
132 # Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
133 # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
134 # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
136 as_json.to_json(*args)
140 # Range serialization/deserialization
143 # Deserializes JSON string by constructing new Range object with arguments
144 # <tt>a</tt> serialized by <tt>to_json</tt>.
145 def self.json_create(object)
149 # Returns a hash, that will be turned into a JSON object and represent this
153 JSON.create_id => self.class.name,
154 'a' => [ first, last, exclude_end? ]
158 # Stores class name (Range) with JSON array of arguments <tt>a</tt> which
159 # include <tt>first</tt> (integer), <tt>last</tt> (integer), and
160 # <tt>exclude_end?</tt> (boolean) as JSON string.
162 as_json.to_json(*args)
166 # Struct serialization/deserialization
169 # Deserializes JSON string by constructing new Struct object with values
170 # <tt>v</tt> serialized by <tt>to_json</tt>.
171 def self.json_create(object)
175 # Returns a hash, that will be turned into a JSON object and represent this
178 klass = self.class.name
179 klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
181 JSON.create_id => klass,
186 # Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string.
187 # Only named structs are supported.
189 as_json.to_json(*args)
193 # Exception serialization/deserialization
196 # Deserializes JSON string by constructing new Exception object with message
197 # <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
198 def self.json_create(object)
199 result = new(object['m'])
200 result.set_backtrace object['b']
204 # Returns a hash, that will be turned into a JSON object and represent this
208 JSON.create_id => self.class.name,
214 # Stores class name (Exception) with message <tt>m</tt> and backtrace array
215 # <tt>b</tt> as JSON string
217 as_json.to_json(*args)
221 # Regexp serialization/deserialization
224 # Deserializes JSON string by constructing new Regexp object with source
225 # <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by
227 def self.json_create(object)
228 new(object['s'], object['o'])
231 # Returns a hash, that will be turned into a JSON object and represent this
235 JSON.create_id => self.class.name,
241 # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
242 # (Regexp or String) as JSON string