OSDN Git Service

configured for android
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / .ext / common / json / add / core.rb
1 # This file contains implementations of ruby core's custom objects for
2 # serialisation/deserialisation.
3
4 unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5   ::JSON::JSON_LOADED
6   require 'json'
7 end
8 require 'date'
9
10 class Time
11   def self.json_create(object)
12     if usec = object.delete('u') # used to be tv_usec -> tv_nsec
13       object['n'] = usec * 1000
14     end
15     if respond_to?(:tv_nsec)
16       at(*object.values_at('s', 'n'))
17     else
18       at(object['s'], object['n'] / 1000)
19     end
20   end
21
22   def to_json(*args)
23     {
24       'json_class' => self.class.name,
25       's' => tv_sec,
26       'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
27     }.to_json(*args)
28   end
29 end
30
31 class Date
32   def self.json_create(object)
33     civil(*object.values_at('y', 'm', 'd', 'sg'))
34   end
35
36   alias start sg unless method_defined?(:start)
37
38   def to_json(*args)
39     {
40       'json_class' => self.class.name,
41       'y' => year,
42       'm' => month,
43       'd' => day,
44       'sg' => start,
45     }.to_json(*args)
46   end
47 end
48
49 class DateTime
50   def self.json_create(object)
51     args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
52     of_a, of_b = object['of'].split('/')
53     if of_b and of_b != '0'
54       args << Rational(of_a.to_i, of_b.to_i)
55     else
56       args << of_a
57     end
58     args << object['sg']
59     civil(*args)
60   end
61
62   alias start sg unless method_defined?(:start)
63
64   def to_json(*args)
65     {
66       'json_class' => self.class.name,
67       'y' => year,
68       'm' => month,
69       'd' => day,
70       'H' => hour,
71       'M' => min,
72       'S' => sec,
73       'of' => offset.to_s,
74       'sg' => start,
75     }.to_json(*args)
76   end
77 end
78
79 class Range
80   def self.json_create(object)
81     new(*object['a'])
82   end
83
84   def to_json(*args)
85     {
86       'json_class'   => self.class.name,
87       'a'         => [ first, last, exclude_end? ]
88     }.to_json(*args)
89   end
90 end
91
92 class Struct
93   def self.json_create(object)
94     new(*object['v'])
95   end
96
97   def to_json(*args)
98     klass = self.class.name
99     klass.nil? and raise JSON::JSONError, "Only named structs are supported!"
100     {
101       'json_class' => klass,
102       'v'     => values,
103     }.to_json(*args)
104   end
105 end
106
107 class Exception
108   def self.json_create(object)
109     result = new(object['m'])
110     result.set_backtrace object['b']
111     result
112   end
113
114   def to_json(*args)
115     {
116       'json_class' => self.class.name,
117       'm'   => message,
118       'b' => backtrace,
119     }.to_json(*args)
120   end
121 end
122
123 class Regexp
124   def self.json_create(object)
125     new(object['s'], object['o'])
126   end
127
128   def to_json(*)
129     {
130       'json_class' => self.class.name,
131       'o' => options,
132       's' => source,
133     }.to_json
134   end
135 end