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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
#encoding: us-ascii
require 'active_support/core_ext/object/to_json'
require 'active_support/core_ext/module/delegation'
require 'active_support/json/variable'
require 'bigdecimal'
require 'active_support/core_ext/big_decimal/conversions' # for #to_s
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/object/instance_variables'
require 'time'
require 'active_support/core_ext/time/conversions'
require 'active_support/core_ext/date_time/conversions'
require 'active_support/core_ext/date/conversions'
require 'set'
module ActiveSupport
class << self
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
:escape_html_entities_in_json, :escape_html_entities_in_json=,
:encode_big_decimal_as_string, :encode_big_decimal_as_string=,
:to => :'ActiveSupport::JSON::Encoding'
end
module JSON
# matches YAML-formatted dates
DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
# Dumps objects in JSON (JavaScript Object Notation).
# See www.json.org for more info.
#
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
def self.encode(value, options = nil)
Encoding::Encoder.new(options).encode(value)
end
module Encoding #:nodoc:
class CircularReferenceError < StandardError; end
class Encoder
attr_reader :options
def initialize(options = nil)
@options = options || {}
@seen = Set.new
end
def encode(value, use_options = true)
check_for_circular_references(value) do
jsonified = use_options ? value.as_json(options_for(value)) : value.as_json
jsonified.encode_json(self)
end
end
# like encode, but only calls as_json, without encoding to string.
def as_json(value, use_options = true)
check_for_circular_references(value) do
use_options ? value.as_json(options_for(value)) : value.as_json
end
end
def options_for(value)
if value.is_a?(Array) || value.is_a?(Hash)
# hashes and arrays need to get encoder in the options, so that
# they can detect circular references.
options.merge(:encoder => self)
else
options.dup
end
end
def escape(string)
Encoding.escape(string)
end
private
def check_for_circular_references(value)
unless @seen.add?(value.__id__)
raise CircularReferenceError, 'object references itself'
end
yield
ensure
@seen.delete(value.__id__)
end
end
ESCAPED_CHARS = {
"\x00" => '\u0000', "\x01" => '\u0001', "\x02" => '\u0002',
"\x03" => '\u0003', "\x04" => '\u0004', "\x05" => '\u0005',
"\x06" => '\u0006', "\x07" => '\u0007', "\x0B" => '\u000B',
"\x0E" => '\u000E', "\x0F" => '\u000F', "\x10" => '\u0010',
"\x11" => '\u0011', "\x12" => '\u0012', "\x13" => '\u0013',
"\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016',
"\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019',
"\x1A" => '\u001A', "\x1B" => '\u001B', "\x1C" => '\u001C',
"\x1D" => '\u001D', "\x1E" => '\u001E', "\x1F" => '\u001F',
"\010" => '\b',
"\f" => '\f',
"\n" => '\n',
"\xe2\x80\xa8" => '\u2028',
"\xe2\x80\xa9" => '\u2029',
"\r" => '\r',
"\t" => '\t',
'"' => '\"',
'\\' => '\\\\',
'>' => '\u003E',
'<' => '\u003C',
'&' => '\u0026',
"#{0xe2.chr}#{0x80.chr}#{0xa8.chr}" => '\u2028',
"#{0xe2.chr}#{0x80.chr}#{0xa9.chr}" => '\u2029',
}
class << self
# If true, use ISO 8601 format for dates and times. Otherwise, fall back
# to the Active Support legacy format.
attr_accessor :use_standard_json_time_format
# If false, serializes BigDecimal objects as numeric instead of wrapping
# them in a string.
attr_accessor :encode_big_decimal_as_string
attr_accessor :escape_regex
attr_reader :escape_html_entities_in_json
def escape_html_entities_in_json=(value)
self.escape_regex = \
if @escape_html_entities_in_json = value
/\xe2\x80\xa8|\xe2\x80\xa9|[\x00-\x1F"\\><&]/
else
/\xe2\x80\xa8|\xe2\x80\xa9|[\x00-\x1F"\\]/
end
end
def escape(string)
string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
json = %("#{json}")
json.force_encoding(::Encoding::UTF_8)
json
end
end
self.use_standard_json_time_format = true
self.escape_html_entities_in_json = true
self.encode_big_decimal_as_string = true
end
end
end
class Object
def as_json(options = nil) #:nodoc:
if respond_to?(:to_hash)
to_hash
else
instance_values
end
end
end
class Struct #:nodoc:
def as_json(options = nil)
Hash[members.zip(values)]
end
end
class TrueClass
def as_json(options = nil) #:nodoc:
self
end
def encode_json(encoder) #:nodoc:
to_s
end
end
class FalseClass
def as_json(options = nil) #:nodoc:
self
end
def encode_json(encoder) #:nodoc:
to_s
end
end
class NilClass
def as_json(options = nil) #:nodoc:
self
end
def encode_json(encoder) #:nodoc:
'null'
end
end
class String
def as_json(options = nil) #:nodoc:
self
end
def encode_json(encoder) #:nodoc:
encoder.escape(self)
end
end
class Symbol
def as_json(options = nil) #:nodoc:
to_s
end
end
class Numeric
def as_json(options = nil) #:nodoc:
self
end
def encode_json(encoder) #:nodoc:
to_s
end
end
class Float
# Encoding Infinity or NaN to JSON should return "null". The default returns
# "Infinity" or "NaN" which breaks parsing the JSON. E.g. JSON.parse('[NaN]').
def as_json(options = nil) #:nodoc:
finite? ? self : nil
end
end
class BigDecimal
# A BigDecimal would be naturally represented as a JSON number. Most libraries,
# however, parse non-integer JSON numbers directly as floats. Clients using
# those libraries would get in general a wrong number and no way to recover
# other than manually inspecting the string with the JSON code itself.
#
# That's why a JSON string is returned. The JSON literal is not numeric, but
# if the other end knows by contract that the data is supposed to be a
# BigDecimal, it still has the chance to post-process the string and get the
# real value.
#
# Use <tt>ActiveSupport.use_standard_json_big_decimal_format = true</tt> to
# override this behavior.
def as_json(options = nil) #:nodoc:
if finite?
ActiveSupport.encode_big_decimal_as_string ? to_s : self
else
nil
end
end
end
class Regexp
def as_json(options = nil) #:nodoc:
to_s
end
end
module Enumerable
def as_json(options = nil) #:nodoc:
to_a.as_json(options)
end
end
class Range
def as_json(options = nil) #:nodoc:
to_s
end
end
class Array
def as_json(options = nil) #:nodoc:
# use encoder as a proxy to call as_json on all elements, to protect from circular references
encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options)
map { |v| encoder.as_json(v, options) }
end
def encode_json(encoder) #:nodoc:
# we assume here that the encoder has already run as_json on self and the elements, so we run encode_json directly
"[#{map { |v| v.encode_json(encoder) } * ','}]"
end
end
class Hash
def as_json(options = nil) #:nodoc:
# create a subset of the hash by applying :only or :except
subset = if options
if attrs = options[:only]
slice(*Array(attrs))
elsif attrs = options[:except]
except(*Array(attrs))
else
self
end
else
self
end
# use encoder as a proxy to call as_json on all values in the subset, to protect from circular references
encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options)
Hash[subset.map { |k, v| [k.to_s, encoder.as_json(v, options)] }]
end
def encode_json(encoder) #:nodoc:
# values are encoded with use_options = false, because we don't want hash representations from ActiveModel to be
# processed once again with as_json with options, as this could cause unexpected results (i.e. missing fields);
# on the other hand, we need to run as_json on the elements, because the model representation may contain fields
# like Time/Date in their original (not jsonified) form, etc.
"{#{map { |k,v| "#{encoder.encode(k.to_s)}:#{encoder.encode(v, false)}" } * ','}}"
end
end
class Time
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
xmlschema
else
%(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
end
end
end
class Date
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
strftime("%Y-%m-%d")
else
strftime("%Y/%m/%d")
end
end
end
class DateTime
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
xmlschema
else
strftime('%Y/%m/%d %H:%M:%S %z')
end
end
end
|