forked from opal/opal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.rb
More file actions
206 lines (163 loc) · 3.62 KB
/
json.rb
File metadata and controls
206 lines (163 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
module JSON
class JSONError < StandardError
end
class ParserError < JSONError
end
%x{
var $hasOwn = Opal.hasOwnProperty;
function $parse(source) {
try {
return JSON.parse(source);
} catch (e) {
#{raise JSON::ParserError, `e.message`};
}
};
function to_opal(value, options) {
var klass, arr, hash, i, ii, k;
switch (typeof value) {
case 'string':
return value;
case 'number':
return value;
case 'boolean':
return !!value;
case 'null':
return nil;
case 'object':
if (!value) return nil;
if (value.$$is_array) {
arr = #{`options.array_class`.new};
for (i = 0, ii = value.length; i < ii; i++) {
#{`arr`.push(`to_opal(value[i], options)`)};
}
return arr;
}
else {
hash = #{`options.object_class`.new};
for (k in value) {
if ($hasOwn.call(value, k)) {
#{`hash`[`k`] = `to_opal(value[k], options)`};
}
}
if (!options.parse && (klass = #{`hash`[JSON.create_id]}) != nil) {
return #{::Object.const_get(`klass`).json_create(`hash`)};
}
else {
return hash;
}
}
}
};
}
class << self
attr_accessor :create_id
end
self.create_id = :json_class
def self.[](value, options = {})
if String === value
parse(value, options)
else
generate(value, options)
end
end
def self.parse(source, options = {})
from_object(`$parse(source)`, options.merge(parse: true))
end
def self.parse!(source, options = {})
parse(source, options)
end
def self.load(source, options = {})
from_object(`$parse(source)`, options)
end
# Raw js object => opal object
def self.from_object(js_object, options = {})
options[:object_class] ||= Hash
options[:array_class] ||= Array
`to_opal(js_object, options.$$smap)`
end
def self.generate(obj, options = {})
obj.to_json(options)
end
def self.dump(obj, io = nil, limit = nil)
string = generate(obj)
if io
io = io.to_io if io.responds_to? :to_io
io.write string
io
else
string
end
end
end
class Object
def to_json
to_s.to_json
end
end
# BUG: Enumerable must come before Array, otherwise it overrides #to_json
# this is due to how modules are implemented.
module Enumerable
def to_json
to_a.to_json
end
end
class Array
def to_json
%x{
var result = [];
for (var i = 0, length = #{self}.length; i < length; i++) {
result.push(#{`self[i]`.to_json});
}
return '[' + result.join(', ') + ']';
}
end
end
class Boolean
def to_json
`(self == true) ? 'true' : 'false'`
end
end
class Hash
def to_json
%x{
var result = [];
for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
value = self.$$smap[key];
} else {
value = key.value;
key = key.key;
}
result.push(#{`key`.to_s.to_json} + ':' + #{`value`.to_json});
}
return '{' + result.join(', ') + '}';
}
end
end
class NilClass
def to_json
'null'
end
end
class Numeric
def to_json
`self.toString()`
end
end
class String
alias to_json inspect
end
class Time
def to_json
strftime('%FT%T%z').to_json
end
end
class Date
def to_json
to_s.to_json
end
def as_json
to_s
end
end