stripe 1.5.13 → 1.5.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/stripe/version.rb +1 -1
- data/vendor/stripe-json/lib/json/common.rb +423 -0
- data/vendor/stripe-json/lib/json/pure.rb +28 -0
- data/vendor/stripe-json/lib/json/pure/generator.rb +442 -0
- data/vendor/stripe-json/lib/json/pure/parser.rb +320 -0
- data/vendor/stripe-json/lib/json/version.rb +8 -0
- metadata +22 -4
data/lib/stripe/version.rb
CHANGED
@@ -0,0 +1,423 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'version')
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class << self
|
5
|
+
# If _object_ is string-like parse the string and return the parsed result
|
6
|
+
# as a Ruby data structure. Otherwise generate a JSON text from the Ruby
|
7
|
+
# data structure object and return it.
|
8
|
+
#
|
9
|
+
# The _opts_ argument is passed through to generate/parse respectively, see
|
10
|
+
# generate and parse for their documentation.
|
11
|
+
def [](object, opts = {})
|
12
|
+
if object.respond_to? :to_str
|
13
|
+
JSON.parse(object.to_str, opts)
|
14
|
+
else
|
15
|
+
JSON.generate(object, opts)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns the JSON parser class, that is used by JSON. This might be either
|
20
|
+
# JSON::Ext::Parser or JSON::Pure::Parser.
|
21
|
+
attr_reader :parser
|
22
|
+
|
23
|
+
# Set the JSON parser class _parser_ to be used by JSON.
|
24
|
+
def parser=(parser) # :nodoc:
|
25
|
+
@parser = parser
|
26
|
+
remove_const :Parser if JSON.const_defined_in?(self, :Parser)
|
27
|
+
const_set :Parser, parser
|
28
|
+
end
|
29
|
+
|
30
|
+
# Return the constant located at _path_. The format of _path_ has to be
|
31
|
+
# either ::A::B::C or A::B::C. In any case A has to be located at the top
|
32
|
+
# level (absolute namespace path?). If there doesn't exist a constant at
|
33
|
+
# the given path, an ArgumentError is raised.
|
34
|
+
def deep_const_get(path) # :nodoc:
|
35
|
+
path.to_s.split(/::/).inject(Object) do |p, c|
|
36
|
+
case
|
37
|
+
when c.empty? then p
|
38
|
+
when JSON.const_defined_in?(p, c) then p.const_get(c)
|
39
|
+
else
|
40
|
+
begin
|
41
|
+
p.const_missing(c)
|
42
|
+
rescue NameError => e
|
43
|
+
raise ArgumentError, "can't get const #{path}: #{e}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Set the module _generator_ to be used by JSON.
|
50
|
+
def generator=(generator) # :nodoc:
|
51
|
+
old, $VERBOSE = $VERBOSE, nil
|
52
|
+
@generator = generator
|
53
|
+
generator_methods = generator::GeneratorMethods
|
54
|
+
for const in generator_methods.constants
|
55
|
+
klass = deep_const_get(const)
|
56
|
+
modul = generator_methods.const_get(const)
|
57
|
+
klass.class_eval do
|
58
|
+
instance_methods(false).each do |m|
|
59
|
+
m.to_s == 'to_json' and remove_method m
|
60
|
+
end
|
61
|
+
include modul
|
62
|
+
end
|
63
|
+
end
|
64
|
+
self.state = generator::State
|
65
|
+
const_set :State, self.state
|
66
|
+
const_set :SAFE_STATE_PROTOTYPE, State.new
|
67
|
+
const_set :FAST_STATE_PROTOTYPE, State.new(
|
68
|
+
:indent => '',
|
69
|
+
:space => '',
|
70
|
+
:object_nl => "",
|
71
|
+
:array_nl => "",
|
72
|
+
:max_nesting => false
|
73
|
+
)
|
74
|
+
const_set :PRETTY_STATE_PROTOTYPE, State.new(
|
75
|
+
:indent => ' ',
|
76
|
+
:space => ' ',
|
77
|
+
:object_nl => "\n",
|
78
|
+
:array_nl => "\n"
|
79
|
+
)
|
80
|
+
ensure
|
81
|
+
$VERBOSE = old
|
82
|
+
end
|
83
|
+
|
84
|
+
# Returns the JSON generator modul, that is used by JSON. This might be
|
85
|
+
# either JSON::Ext::Generator or JSON::Pure::Generator.
|
86
|
+
attr_reader :generator
|
87
|
+
|
88
|
+
# Returns the JSON generator state class, that is used by JSON. This might
|
89
|
+
# be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
90
|
+
attr_accessor :state
|
91
|
+
|
92
|
+
# This is create identifier, that is used to decide, if the _json_create_
|
93
|
+
# hook of a class should be called. It defaults to 'json_class'.
|
94
|
+
attr_accessor :create_id
|
95
|
+
end
|
96
|
+
self.create_id = 'json_class'
|
97
|
+
|
98
|
+
NaN = 0.0/0
|
99
|
+
|
100
|
+
Infinity = 1.0/0
|
101
|
+
|
102
|
+
MinusInfinity = -Infinity
|
103
|
+
|
104
|
+
# The base exception for JSON errors.
|
105
|
+
class JSONError < StandardError; end
|
106
|
+
|
107
|
+
# This exception is raised, if a parser error occurs.
|
108
|
+
class ParserError < JSONError; end
|
109
|
+
|
110
|
+
# This exception is raised, if the nesting of parsed datastructures is too
|
111
|
+
# deep.
|
112
|
+
class NestingError < ParserError; end
|
113
|
+
|
114
|
+
# :stopdoc:
|
115
|
+
class CircularDatastructure < NestingError; end
|
116
|
+
# :startdoc:
|
117
|
+
|
118
|
+
# This exception is raised, if a generator or unparser error occurs.
|
119
|
+
class GeneratorError < JSONError; end
|
120
|
+
# For backwards compatibility
|
121
|
+
UnparserError = GeneratorError
|
122
|
+
|
123
|
+
# This exception is raised, if the required unicode support is missing on the
|
124
|
+
# system. Usually this means, that the iconv library is not installed.
|
125
|
+
class MissingUnicodeSupport < JSONError; end
|
126
|
+
|
127
|
+
module_function
|
128
|
+
|
129
|
+
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
130
|
+
#
|
131
|
+
# _opts_ can have the following
|
132
|
+
# keys:
|
133
|
+
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
134
|
+
# structures. Disable depth checking with :max_nesting => false, it defaults
|
135
|
+
# to 19.
|
136
|
+
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
137
|
+
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
138
|
+
# to false.
|
139
|
+
# * *symbolize_names*: If set to true, returns symbols for the names
|
140
|
+
# (keys) in a JSON object. Otherwise strings are returned, which is also
|
141
|
+
# the default.
|
142
|
+
# * *create_additions*: If set to false, the Parser doesn't create
|
143
|
+
# additions even if a matchin class and create_id was found. This option
|
144
|
+
# defaults to true.
|
145
|
+
# * *object_class*: Defaults to Hash
|
146
|
+
# * *array_class*: Defaults to Array
|
147
|
+
def parse(source, opts = {})
|
148
|
+
Parser.new(source, opts).parse
|
149
|
+
end
|
150
|
+
|
151
|
+
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
152
|
+
# The bang version of the parse method, defaults to the more dangerous values
|
153
|
+
# for the _opts_ hash, so be sure only to parse trusted _source_ documents.
|
154
|
+
#
|
155
|
+
# _opts_ can have the following keys:
|
156
|
+
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
157
|
+
# structures. Enable depth checking with :max_nesting => anInteger. The parse!
|
158
|
+
# methods defaults to not doing max depth checking: This can be dangerous,
|
159
|
+
# if someone wants to fill up your stack.
|
160
|
+
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
|
161
|
+
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
162
|
+
# to true.
|
163
|
+
# * *create_additions*: If set to false, the Parser doesn't create
|
164
|
+
# additions even if a matchin class and create_id was found. This option
|
165
|
+
# defaults to true.
|
166
|
+
def parse!(source, opts = {})
|
167
|
+
opts = {
|
168
|
+
:max_nesting => false,
|
169
|
+
:allow_nan => true
|
170
|
+
}.update(opts)
|
171
|
+
Parser.new(source, opts).parse
|
172
|
+
end
|
173
|
+
|
174
|
+
# Generate a JSON document from the Ruby data structure _obj_ and return
|
175
|
+
# it. _state_ is * a JSON::State object,
|
176
|
+
# * or a Hash like object (responding to to_hash),
|
177
|
+
# * an object convertible into a hash by a to_h method,
|
178
|
+
# that is used as or to configure a State object.
|
179
|
+
#
|
180
|
+
# It defaults to a state object, that creates the shortest possible JSON text
|
181
|
+
# in one line, checks for circular data structures and doesn't allow NaN,
|
182
|
+
# Infinity, and -Infinity.
|
183
|
+
#
|
184
|
+
# A _state_ hash can have the following keys:
|
185
|
+
# * *indent*: a string used to indent levels (default: ''),
|
186
|
+
# * *space*: a string that is put after, a : or , delimiter (default: ''),
|
187
|
+
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
188
|
+
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
189
|
+
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
190
|
+
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
191
|
+
# generated, otherwise an exception is thrown, if these values are
|
192
|
+
# encountered. This options defaults to false.
|
193
|
+
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
194
|
+
# structures from which JSON is to be generated. Disable depth checking
|
195
|
+
# with :max_nesting => false, it defaults to 19.
|
196
|
+
#
|
197
|
+
# See also the fast_generate for the fastest creation method with the least
|
198
|
+
# amount of sanity checks, and the pretty_generate method for some
|
199
|
+
# defaults for a pretty output.
|
200
|
+
def generate(obj, opts = nil)
|
201
|
+
state = SAFE_STATE_PROTOTYPE.dup
|
202
|
+
if opts
|
203
|
+
if opts.respond_to? :to_hash
|
204
|
+
opts = opts.to_hash
|
205
|
+
elsif opts.respond_to? :to_h
|
206
|
+
opts = opts.to_h
|
207
|
+
else
|
208
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
209
|
+
end
|
210
|
+
state = state.configure(opts)
|
211
|
+
end
|
212
|
+
state.generate(obj)
|
213
|
+
end
|
214
|
+
|
215
|
+
# :stopdoc:
|
216
|
+
# I want to deprecate these later, so I'll first be silent about them, and
|
217
|
+
# later delete them.
|
218
|
+
alias unparse generate
|
219
|
+
module_function :unparse
|
220
|
+
# :startdoc:
|
221
|
+
|
222
|
+
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
223
|
+
# This method disables the checks for circles in Ruby objects.
|
224
|
+
#
|
225
|
+
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
226
|
+
# _obj_ argument, because this will cause JSON to go into an infinite loop.
|
227
|
+
def fast_generate(obj, opts = nil)
|
228
|
+
state = FAST_STATE_PROTOTYPE.dup
|
229
|
+
if opts
|
230
|
+
if opts.respond_to? :to_hash
|
231
|
+
opts = opts.to_hash
|
232
|
+
elsif opts.respond_to? :to_h
|
233
|
+
opts = opts.to_h
|
234
|
+
else
|
235
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
236
|
+
end
|
237
|
+
state.configure(opts)
|
238
|
+
end
|
239
|
+
state.generate(obj)
|
240
|
+
end
|
241
|
+
|
242
|
+
# :stopdoc:
|
243
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
244
|
+
alias fast_unparse fast_generate
|
245
|
+
module_function :fast_unparse
|
246
|
+
# :startdoc:
|
247
|
+
|
248
|
+
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
249
|
+
# The returned document is a prettier form of the document returned by
|
250
|
+
# #unparse.
|
251
|
+
#
|
252
|
+
# The _opts_ argument can be used to configure the generator, see the
|
253
|
+
# generate method for a more detailed explanation.
|
254
|
+
def pretty_generate(obj, opts = nil)
|
255
|
+
state = PRETTY_STATE_PROTOTYPE.dup
|
256
|
+
if opts
|
257
|
+
if opts.respond_to? :to_hash
|
258
|
+
opts = opts.to_hash
|
259
|
+
elsif opts.respond_to? :to_h
|
260
|
+
opts = opts.to_h
|
261
|
+
else
|
262
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
263
|
+
end
|
264
|
+
state.configure(opts)
|
265
|
+
end
|
266
|
+
state.generate(obj)
|
267
|
+
end
|
268
|
+
|
269
|
+
# :stopdoc:
|
270
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
271
|
+
alias pretty_unparse pretty_generate
|
272
|
+
module_function :pretty_unparse
|
273
|
+
# :startdoc:
|
274
|
+
|
275
|
+
# Load a ruby data structure from a JSON _source_ and return it. A source can
|
276
|
+
# either be a string-like object, an IO like object, or an object responding
|
277
|
+
# to the read method. If _proc_ was given, it will be called with any nested
|
278
|
+
# Ruby object as an argument recursively in depth first order.
|
279
|
+
#
|
280
|
+
# This method is part of the implementation of the load/dump interface of
|
281
|
+
# Marshal and YAML.
|
282
|
+
def load(source, proc = nil)
|
283
|
+
if source.respond_to? :to_str
|
284
|
+
source = source.to_str
|
285
|
+
elsif source.respond_to? :to_io
|
286
|
+
source = source.to_io.read
|
287
|
+
else
|
288
|
+
source = source.read
|
289
|
+
end
|
290
|
+
result = parse(source, :max_nesting => false, :allow_nan => true)
|
291
|
+
recurse_proc(result, &proc) if proc
|
292
|
+
result
|
293
|
+
end
|
294
|
+
|
295
|
+
# Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
|
296
|
+
def recurse_proc(result, &proc)
|
297
|
+
case result
|
298
|
+
when Array
|
299
|
+
result.each { |x| recurse_proc x, &proc }
|
300
|
+
proc.call result
|
301
|
+
when Hash
|
302
|
+
result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
|
303
|
+
proc.call result
|
304
|
+
else
|
305
|
+
proc.call result
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
alias restore load
|
310
|
+
module_function :restore
|
311
|
+
|
312
|
+
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
|
313
|
+
# the result.
|
314
|
+
#
|
315
|
+
# If anIO (an IO like object or an object that responds to the write method)
|
316
|
+
# was given, the resulting JSON is written to it.
|
317
|
+
#
|
318
|
+
# If the number of nested arrays or objects exceeds _limit_ an ArgumentError
|
319
|
+
# exception is raised. This argument is similar (but not exactly the
|
320
|
+
# same!) to the _limit_ argument in Marshal.dump.
|
321
|
+
#
|
322
|
+
# This method is part of the implementation of the load/dump interface of
|
323
|
+
# Marshal and YAML.
|
324
|
+
def dump(obj, anIO = nil, limit = nil)
|
325
|
+
if anIO and limit.nil?
|
326
|
+
anIO = anIO.to_io if anIO.respond_to?(:to_io)
|
327
|
+
unless anIO.respond_to?(:write)
|
328
|
+
limit = anIO
|
329
|
+
anIO = nil
|
330
|
+
end
|
331
|
+
end
|
332
|
+
limit ||= 0
|
333
|
+
result = generate(obj, :allow_nan => true, :max_nesting => limit)
|
334
|
+
if anIO
|
335
|
+
anIO.write result
|
336
|
+
anIO
|
337
|
+
else
|
338
|
+
result
|
339
|
+
end
|
340
|
+
rescue JSON::NestingError
|
341
|
+
raise ArgumentError, "exceed depth limit"
|
342
|
+
end
|
343
|
+
|
344
|
+
# Swap consecutive bytes of _string_ in place.
|
345
|
+
def self.swap!(string) # :nodoc:
|
346
|
+
0.upto(string.size / 2) do |i|
|
347
|
+
break unless string[2 * i + 1]
|
348
|
+
string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
|
349
|
+
end
|
350
|
+
string
|
351
|
+
end
|
352
|
+
|
353
|
+
# Shortuct for iconv.
|
354
|
+
if ::String.method_defined?(:encode)
|
355
|
+
# Encodes string using Ruby's _String.encode_
|
356
|
+
def self.iconv(to, from, string)
|
357
|
+
string.encode(to, from)
|
358
|
+
end
|
359
|
+
else
|
360
|
+
require 'iconv'
|
361
|
+
# Encodes string using _iconv_ library
|
362
|
+
def self.iconv(to, from, string)
|
363
|
+
Iconv.conv(to, from, string)
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
if ::Object.method(:const_defined?).arity == 1
|
368
|
+
def self.const_defined_in?(modul, constant)
|
369
|
+
modul.const_defined?(constant)
|
370
|
+
end
|
371
|
+
else
|
372
|
+
def self.const_defined_in?(modul, constant)
|
373
|
+
modul.const_defined?(constant, false)
|
374
|
+
end
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
module ::Kernel
|
379
|
+
private
|
380
|
+
|
381
|
+
# Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
|
382
|
+
# one line.
|
383
|
+
def j(*objs)
|
384
|
+
objs.each do |obj|
|
385
|
+
puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
|
386
|
+
end
|
387
|
+
nil
|
388
|
+
end
|
389
|
+
|
390
|
+
# Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
|
391
|
+
# indentation and over many lines.
|
392
|
+
def jj(*objs)
|
393
|
+
objs.each do |obj|
|
394
|
+
puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
|
395
|
+
end
|
396
|
+
nil
|
397
|
+
end
|
398
|
+
|
399
|
+
# If _object_ is string-like parse the string and return the parsed result as
|
400
|
+
# a Ruby data structure. Otherwise generate a JSON text from the Ruby data
|
401
|
+
# structure object and return it.
|
402
|
+
#
|
403
|
+
# The _opts_ argument is passed through to generate/parse respectively, see
|
404
|
+
# generate and parse for their documentation.
|
405
|
+
def JSON(object, *args)
|
406
|
+
if object.respond_to? :to_str
|
407
|
+
JSON.parse(object.to_str, args.first)
|
408
|
+
else
|
409
|
+
JSON.generate(object, args.first)
|
410
|
+
end
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
# Extends any Class to include _json_creatable?_ method.
|
415
|
+
class ::Class
|
416
|
+
# Returns true, if this class can be used to create an instance
|
417
|
+
# from a serialised JSON string. The class has to implement a class
|
418
|
+
# method _json_create_ that expects a hash as first parameter, which includes
|
419
|
+
# the required data.
|
420
|
+
def json_creatable?
|
421
|
+
respond_to?(:json_create)
|
422
|
+
end
|
423
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'common')
|
2
|
+
require File.join(File.dirname(__FILE__), 'pure/parser')
|
3
|
+
require File.join(File.dirname(__FILE__), 'pure/generator')
|
4
|
+
|
5
|
+
module JSON
|
6
|
+
# This module holds all the modules/classes that implement JSON's
|
7
|
+
# functionality in pure ruby.
|
8
|
+
module Pure
|
9
|
+
$DEBUG and warn "Using Pure library for JSON."
|
10
|
+
JSON.parser = Parser
|
11
|
+
JSON.generator = Generator
|
12
|
+
end
|
13
|
+
|
14
|
+
JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
|
15
|
+
end
|
16
|
+
|
17
|
+
## Hacks needed to make to_json work
|
18
|
+
class Fixnum
|
19
|
+
def to_json(options=nil)
|
20
|
+
to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Integer
|
25
|
+
def to_json(options=nil)
|
26
|
+
to_s
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,442 @@
|
|
1
|
+
module JSON
|
2
|
+
MAP = {
|
3
|
+
"\x0" => '\u0000',
|
4
|
+
"\x1" => '\u0001',
|
5
|
+
"\x2" => '\u0002',
|
6
|
+
"\x3" => '\u0003',
|
7
|
+
"\x4" => '\u0004',
|
8
|
+
"\x5" => '\u0005',
|
9
|
+
"\x6" => '\u0006',
|
10
|
+
"\x7" => '\u0007',
|
11
|
+
"\b" => '\b',
|
12
|
+
"\t" => '\t',
|
13
|
+
"\n" => '\n',
|
14
|
+
"\xb" => '\u000b',
|
15
|
+
"\f" => '\f',
|
16
|
+
"\r" => '\r',
|
17
|
+
"\xe" => '\u000e',
|
18
|
+
"\xf" => '\u000f',
|
19
|
+
"\x10" => '\u0010',
|
20
|
+
"\x11" => '\u0011',
|
21
|
+
"\x12" => '\u0012',
|
22
|
+
"\x13" => '\u0013',
|
23
|
+
"\x14" => '\u0014',
|
24
|
+
"\x15" => '\u0015',
|
25
|
+
"\x16" => '\u0016',
|
26
|
+
"\x17" => '\u0017',
|
27
|
+
"\x18" => '\u0018',
|
28
|
+
"\x19" => '\u0019',
|
29
|
+
"\x1a" => '\u001a',
|
30
|
+
"\x1b" => '\u001b',
|
31
|
+
"\x1c" => '\u001c',
|
32
|
+
"\x1d" => '\u001d',
|
33
|
+
"\x1e" => '\u001e',
|
34
|
+
"\x1f" => '\u001f',
|
35
|
+
'"' => '\"',
|
36
|
+
'\\' => '\\\\',
|
37
|
+
} # :nodoc:
|
38
|
+
|
39
|
+
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
|
40
|
+
# UTF16 big endian characters as \u????, and return it.
|
41
|
+
if defined?(::Encoding)
|
42
|
+
def utf8_to_json(string) # :nodoc:
|
43
|
+
string = string.dup
|
44
|
+
string << '' # XXX workaround: avoid buffer sharing
|
45
|
+
string.force_encoding(::Encoding::ASCII_8BIT)
|
46
|
+
string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
|
47
|
+
string.force_encoding(::Encoding::UTF_8)
|
48
|
+
string
|
49
|
+
end
|
50
|
+
|
51
|
+
def utf8_to_json_ascii(string) # :nodoc:
|
52
|
+
string = string.dup
|
53
|
+
string << '' # XXX workaround: avoid buffer sharing
|
54
|
+
string.force_encoding(::Encoding::ASCII_8BIT)
|
55
|
+
string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
|
56
|
+
string.gsub!(/(
|
57
|
+
(?:
|
58
|
+
[\xc2-\xdf][\x80-\xbf] |
|
59
|
+
[\xe0-\xef][\x80-\xbf]{2} |
|
60
|
+
[\xf0-\xf4][\x80-\xbf]{3}
|
61
|
+
)+ |
|
62
|
+
[\x80-\xc1\xf5-\xff] # invalid
|
63
|
+
)/nx) { |c|
|
64
|
+
c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
|
65
|
+
s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
|
66
|
+
s.gsub!(/.{4}/n, '\\\\u\&')
|
67
|
+
}
|
68
|
+
string.force_encoding(::Encoding::UTF_8)
|
69
|
+
string
|
70
|
+
rescue => e
|
71
|
+
raise GeneratorError, "Caught #{e.class}: #{e}"
|
72
|
+
end
|
73
|
+
else
|
74
|
+
def utf8_to_json(string) # :nodoc:
|
75
|
+
string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
|
76
|
+
end
|
77
|
+
|
78
|
+
def utf8_to_json_ascii(string) # :nodoc:
|
79
|
+
string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
|
80
|
+
string.gsub!(/(
|
81
|
+
(?:
|
82
|
+
[\xc2-\xdf][\x80-\xbf] |
|
83
|
+
[\xe0-\xef][\x80-\xbf]{2} |
|
84
|
+
[\xf0-\xf4][\x80-\xbf]{3}
|
85
|
+
)+ |
|
86
|
+
[\x80-\xc1\xf5-\xff] # invalid
|
87
|
+
)/nx) { |c|
|
88
|
+
c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
|
89
|
+
s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
|
90
|
+
s.gsub!(/.{4}/n, '\\\\u\&')
|
91
|
+
}
|
92
|
+
string
|
93
|
+
rescue => e
|
94
|
+
raise GeneratorError, "Caught #{e.class}: #{e}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
module_function :utf8_to_json, :utf8_to_json_ascii
|
98
|
+
|
99
|
+
module Pure
|
100
|
+
module Generator
|
101
|
+
# This class is used to create State instances, that are use to hold data
|
102
|
+
# while generating a JSON text from a Ruby data structure.
|
103
|
+
class State
|
104
|
+
# Creates a State object from _opts_, which ought to be Hash to create
|
105
|
+
# a new State instance configured by _opts_, something else to create
|
106
|
+
# an unconfigured instance. If _opts_ is a State object, it is just
|
107
|
+
# returned.
|
108
|
+
def self.from_state(opts)
|
109
|
+
case
|
110
|
+
when self === opts
|
111
|
+
opts
|
112
|
+
when opts.respond_to?(:to_hash)
|
113
|
+
new(opts.to_hash)
|
114
|
+
when opts.respond_to?(:to_h)
|
115
|
+
new(opts.to_h)
|
116
|
+
else
|
117
|
+
SAFE_STATE_PROTOTYPE.dup
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Instantiates a new State object, configured by _opts_.
|
122
|
+
#
|
123
|
+
# _opts_ can have the following keys:
|
124
|
+
#
|
125
|
+
# * *indent*: a string used to indent levels (default: ''),
|
126
|
+
# * *space*: a string that is put after, a : or , delimiter (default: ''),
|
127
|
+
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
128
|
+
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
129
|
+
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
130
|
+
# * *check_circular*: is deprecated now, use the :max_nesting option instead,
|
131
|
+
# * *max_nesting*: sets the maximum level of data structure nesting in
|
132
|
+
# the generated JSON, max_nesting = 0 if no maximum should be checked.
|
133
|
+
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
134
|
+
# generated, otherwise an exception is thrown, if these values are
|
135
|
+
# encountered. This options defaults to false.
|
136
|
+
def initialize(opts = {})
|
137
|
+
@indent = ''
|
138
|
+
@space = ''
|
139
|
+
@space_before = ''
|
140
|
+
@object_nl = ''
|
141
|
+
@array_nl = ''
|
142
|
+
@allow_nan = false
|
143
|
+
@ascii_only = false
|
144
|
+
configure opts
|
145
|
+
end
|
146
|
+
|
147
|
+
# This string is used to indent levels in the JSON text.
|
148
|
+
attr_accessor :indent
|
149
|
+
|
150
|
+
# This string is used to insert a space between the tokens in a JSON
|
151
|
+
# string.
|
152
|
+
attr_accessor :space
|
153
|
+
|
154
|
+
# This string is used to insert a space before the ':' in JSON objects.
|
155
|
+
attr_accessor :space_before
|
156
|
+
|
157
|
+
# This string is put at the end of a line that holds a JSON object (or
|
158
|
+
# Hash).
|
159
|
+
attr_accessor :object_nl
|
160
|
+
|
161
|
+
# This string is put at the end of a line that holds a JSON array.
|
162
|
+
attr_accessor :array_nl
|
163
|
+
|
164
|
+
# This integer returns the maximum level of data structure nesting in
|
165
|
+
# the generated JSON, max_nesting = 0 if no maximum is checked.
|
166
|
+
attr_accessor :max_nesting
|
167
|
+
|
168
|
+
# This integer returns the current depth data structure nesting in the
|
169
|
+
# generated JSON.
|
170
|
+
attr_accessor :depth
|
171
|
+
|
172
|
+
def check_max_nesting # :nodoc:
|
173
|
+
return if @max_nesting.zero?
|
174
|
+
current_nesting = depth + 1
|
175
|
+
current_nesting > @max_nesting and
|
176
|
+
raise NestingError, "nesting of #{current_nesting} is too deep"
|
177
|
+
end
|
178
|
+
|
179
|
+
# Returns true, if circular data structures are checked,
|
180
|
+
# otherwise returns false.
|
181
|
+
def check_circular?
|
182
|
+
!@max_nesting.zero?
|
183
|
+
end
|
184
|
+
|
185
|
+
# Returns true if NaN, Infinity, and -Infinity should be considered as
|
186
|
+
# valid JSON and output.
|
187
|
+
def allow_nan?
|
188
|
+
@allow_nan
|
189
|
+
end
|
190
|
+
|
191
|
+
def ascii_only?
|
192
|
+
@ascii_only
|
193
|
+
end
|
194
|
+
|
195
|
+
# Configure this State instance with the Hash _opts_, and return
|
196
|
+
# itself.
|
197
|
+
def configure(opts)
|
198
|
+
@indent = opts[:indent] if opts.key?(:indent)
|
199
|
+
@space = opts[:space] if opts.key?(:space)
|
200
|
+
@space_before = opts[:space_before] if opts.key?(:space_before)
|
201
|
+
@object_nl = opts[:object_nl] if opts.key?(:object_nl)
|
202
|
+
@array_nl = opts[:array_nl] if opts.key?(:array_nl)
|
203
|
+
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
|
204
|
+
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
|
205
|
+
@depth = opts[:depth] || 0
|
206
|
+
if !opts.key?(:max_nesting) # defaults to 19
|
207
|
+
@max_nesting = 19
|
208
|
+
elsif opts[:max_nesting]
|
209
|
+
@max_nesting = opts[:max_nesting]
|
210
|
+
else
|
211
|
+
@max_nesting = 0
|
212
|
+
end
|
213
|
+
self
|
214
|
+
end
|
215
|
+
alias merge configure
|
216
|
+
|
217
|
+
# Returns the configuration instance variables as a hash, that can be
|
218
|
+
# passed to the configure method.
|
219
|
+
def to_h
|
220
|
+
result = {}
|
221
|
+
for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only depth]
|
222
|
+
result[iv.intern] = instance_variable_get("@#{iv}")
|
223
|
+
end
|
224
|
+
result
|
225
|
+
end
|
226
|
+
|
227
|
+
# Generates a valid JSON document from object +obj+ and returns the
|
228
|
+
# result. If no valid JSON document can be created this method raises a
|
229
|
+
# GeneratorError exception.
|
230
|
+
def generate(obj)
|
231
|
+
result = obj.to_json(self)
|
232
|
+
if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
|
233
|
+
raise GeneratorError, "only generation of JSON objects or arrays allowed"
|
234
|
+
end
|
235
|
+
result
|
236
|
+
end
|
237
|
+
|
238
|
+
# Return the value returned by method +name+.
|
239
|
+
def [](name)
|
240
|
+
__send__ name
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
module GeneratorMethods
|
245
|
+
module Object
|
246
|
+
# Converts this object to a string (calling #to_s), converts
|
247
|
+
# it to a JSON string, and returns the result. This is a fallback, if no
|
248
|
+
# special method #to_json was defined for some object.
|
249
|
+
def to_json(*) to_s.to_json end
|
250
|
+
end
|
251
|
+
|
252
|
+
module Hash
|
253
|
+
# Returns a JSON string containing a JSON object, that is unparsed from
|
254
|
+
# this Hash instance.
|
255
|
+
# _state_ is a JSON::State object, that can also be used to configure the
|
256
|
+
# produced JSON string output further.
|
257
|
+
# _depth_ is used to find out nesting depth, to indent accordingly.
|
258
|
+
def to_json(state = nil, *)
|
259
|
+
state = State.from_state(state)
|
260
|
+
state.check_max_nesting
|
261
|
+
json_transform(state)
|
262
|
+
end
|
263
|
+
|
264
|
+
private
|
265
|
+
|
266
|
+
def json_shift(state)
|
267
|
+
state.object_nl.empty? or return ''
|
268
|
+
state.indent * state.depth
|
269
|
+
end
|
270
|
+
|
271
|
+
def json_transform(state)
|
272
|
+
delim = ','
|
273
|
+
delim << state.object_nl
|
274
|
+
result = '{'
|
275
|
+
result << state.object_nl
|
276
|
+
depth = state.depth += 1
|
277
|
+
first = true
|
278
|
+
indent = !state.object_nl.empty?
|
279
|
+
each { |key,value|
|
280
|
+
result << delim unless first
|
281
|
+
result << state.indent * depth if indent
|
282
|
+
result << key.to_s.to_json(state)
|
283
|
+
result << state.space_before
|
284
|
+
result << ':'
|
285
|
+
result << state.space
|
286
|
+
result << value.to_json(state)
|
287
|
+
first = false
|
288
|
+
}
|
289
|
+
depth = state.depth -= 1
|
290
|
+
result << state.object_nl
|
291
|
+
result << state.indent * depth if indent if indent
|
292
|
+
result << '}'
|
293
|
+
result
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
module Array
|
298
|
+
# Returns a JSON string containing a JSON array, that is unparsed from
|
299
|
+
# this Array instance.
|
300
|
+
# _state_ is a JSON::State object, that can also be used to configure the
|
301
|
+
# produced JSON string output further.
|
302
|
+
def to_json(state = nil, *)
|
303
|
+
state = State.from_state(state)
|
304
|
+
state.check_max_nesting
|
305
|
+
json_transform(state)
|
306
|
+
end
|
307
|
+
|
308
|
+
private
|
309
|
+
|
310
|
+
def json_transform(state)
|
311
|
+
delim = ','
|
312
|
+
delim << state.array_nl
|
313
|
+
result = '['
|
314
|
+
result << state.array_nl
|
315
|
+
depth = state.depth += 1
|
316
|
+
first = true
|
317
|
+
indent = !state.array_nl.empty?
|
318
|
+
each { |value|
|
319
|
+
result << delim unless first
|
320
|
+
result << state.indent * depth if indent
|
321
|
+
result << value.to_json(state)
|
322
|
+
first = false
|
323
|
+
}
|
324
|
+
depth = state.depth -= 1
|
325
|
+
result << state.array_nl
|
326
|
+
result << state.indent * depth if indent
|
327
|
+
result << ']'
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
module Integer
|
332
|
+
# Returns a JSON string representation for this Integer number.
|
333
|
+
def to_json(*) to_s end
|
334
|
+
end
|
335
|
+
|
336
|
+
module Float
|
337
|
+
# Returns a JSON string representation for this Float number.
|
338
|
+
def to_json(state = nil, *)
|
339
|
+
state = State.from_state(state)
|
340
|
+
case
|
341
|
+
when infinite?
|
342
|
+
if state.allow_nan?
|
343
|
+
to_s
|
344
|
+
else
|
345
|
+
raise GeneratorError, "#{self} not allowed in JSON"
|
346
|
+
end
|
347
|
+
when nan?
|
348
|
+
if state.allow_nan?
|
349
|
+
to_s
|
350
|
+
else
|
351
|
+
raise GeneratorError, "#{self} not allowed in JSON"
|
352
|
+
end
|
353
|
+
else
|
354
|
+
to_s
|
355
|
+
end
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
module String
|
360
|
+
if defined?(::Encoding)
|
361
|
+
# This string should be encoded with UTF-8 A call to this method
|
362
|
+
# returns a JSON string encoded with UTF16 big endian characters as
|
363
|
+
# \u????.
|
364
|
+
def to_json(state = nil, *args)
|
365
|
+
state = State.from_state(state)
|
366
|
+
if encoding == ::Encoding::UTF_8
|
367
|
+
string = self
|
368
|
+
else
|
369
|
+
string = encode(::Encoding::UTF_8)
|
370
|
+
end
|
371
|
+
if state.ascii_only?
|
372
|
+
'"' << JSON.utf8_to_json_ascii(string) << '"'
|
373
|
+
else
|
374
|
+
'"' << JSON.utf8_to_json(string) << '"'
|
375
|
+
end
|
376
|
+
end
|
377
|
+
else
|
378
|
+
# This string should be encoded with UTF-8 A call to this method
|
379
|
+
# returns a JSON string encoded with UTF16 big endian characters as
|
380
|
+
# \u????.
|
381
|
+
def to_json(state = nil, *args)
|
382
|
+
state = State.from_state(state)
|
383
|
+
if state.ascii_only?
|
384
|
+
'"' << JSON.utf8_to_json_ascii(self) << '"'
|
385
|
+
else
|
386
|
+
'"' << JSON.utf8_to_json(self) << '"'
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
# Module that holds the extinding methods if, the String module is
|
392
|
+
# included.
|
393
|
+
module Extend
|
394
|
+
# Raw Strings are JSON Objects (the raw bytes are stored in an
|
395
|
+
# array for the key "raw"). The Ruby String can be created by this
|
396
|
+
# module method.
|
397
|
+
def json_create(o)
|
398
|
+
o['raw'].pack('C*')
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
# Extends _modul_ with the String::Extend module.
|
403
|
+
def self.included(modul)
|
404
|
+
modul.extend Extend
|
405
|
+
end
|
406
|
+
|
407
|
+
# This method creates a raw object hash, that can be nested into
|
408
|
+
# other data structures and will be unparsed as a raw string. This
|
409
|
+
# method should be used, if you want to convert raw strings to JSON
|
410
|
+
# instead of UTF-8 strings, e. g. binary data.
|
411
|
+
def to_json_raw_object
|
412
|
+
{
|
413
|
+
JSON.create_id => self.class.name,
|
414
|
+
'raw' => self.unpack('C*'),
|
415
|
+
}
|
416
|
+
end
|
417
|
+
|
418
|
+
# This method creates a JSON text from the result of
|
419
|
+
# a call to to_json_raw_object of this String.
|
420
|
+
def to_json_raw(*args)
|
421
|
+
to_json_raw_object.to_json(*args)
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
module TrueClass
|
426
|
+
# Returns a JSON string for true: 'true'.
|
427
|
+
def to_json(*) 'true' end
|
428
|
+
end
|
429
|
+
|
430
|
+
module FalseClass
|
431
|
+
# Returns a JSON string for false: 'false'.
|
432
|
+
def to_json(*) 'false' end
|
433
|
+
end
|
434
|
+
|
435
|
+
module NilClass
|
436
|
+
# Returns a JSON string for nil: 'null'.
|
437
|
+
def to_json(*) 'null' end
|
438
|
+
end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
end
|
442
|
+
end
|
@@ -0,0 +1,320 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
module Pure
|
5
|
+
# This class implements the JSON parser that is used to parse a JSON string
|
6
|
+
# into a Ruby data structure.
|
7
|
+
class Parser < StringScanner
|
8
|
+
STRING = /" ((?:[^\x0-\x1f"\\] |
|
9
|
+
# escaped special characters:
|
10
|
+
\\["\\\/bfnrt] |
|
11
|
+
\\u[0-9a-fA-F]{4} |
|
12
|
+
# match all but escaped special characters:
|
13
|
+
\\[\x20-\x21\x23-\x2e\x30-\x5b\x5d-\x61\x63-\x65\x67-\x6d\x6f-\x71\x73\x75-\xff])*)
|
14
|
+
"/nx
|
15
|
+
INTEGER = /(-?0|-?[1-9]\d*)/
|
16
|
+
FLOAT = /(-?
|
17
|
+
(?:0|[1-9]\d*)
|
18
|
+
(?:
|
19
|
+
\.\d+(?i:e[+-]?\d+) |
|
20
|
+
\.\d+ |
|
21
|
+
(?i:e[+-]?\d+)
|
22
|
+
)
|
23
|
+
)/x
|
24
|
+
NAN = /NaN/
|
25
|
+
INFINITY = /Infinity/
|
26
|
+
MINUS_INFINITY = /-Infinity/
|
27
|
+
OBJECT_OPEN = /\{/
|
28
|
+
OBJECT_CLOSE = /\}/
|
29
|
+
ARRAY_OPEN = /\[/
|
30
|
+
ARRAY_CLOSE = /\]/
|
31
|
+
PAIR_DELIMITER = /:/
|
32
|
+
COLLECTION_DELIMITER = /,/
|
33
|
+
TRUE = /true/
|
34
|
+
FALSE = /false/
|
35
|
+
NULL = /null/
|
36
|
+
IGNORE = %r(
|
37
|
+
(?:
|
38
|
+
//[^\n\r]*[\n\r]| # line comments
|
39
|
+
/\* # c-style comments
|
40
|
+
(?:
|
41
|
+
[^*/]| # normal chars
|
42
|
+
/[^*]| # slashes that do not start a nested comment
|
43
|
+
\*[^/]| # asterisks that do not end this comment
|
44
|
+
/(?=\*/) # single slash before this comment's end
|
45
|
+
)*
|
46
|
+
\*/ # the End of this comment
|
47
|
+
|[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr
|
48
|
+
)+
|
49
|
+
)mx
|
50
|
+
|
51
|
+
UNPARSED = Object.new
|
52
|
+
|
53
|
+
# Creates a new JSON::Pure::Parser instance for the string _source_.
|
54
|
+
#
|
55
|
+
# It will be configured by the _opts_ hash. _opts_ can have the following
|
56
|
+
# keys:
|
57
|
+
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
58
|
+
# structures. Disable depth checking with :max_nesting => false|nil|0,
|
59
|
+
# it defaults to 19.
|
60
|
+
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
61
|
+
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
62
|
+
# to false.
|
63
|
+
# * *symbolize_names*: If set to true, returns symbols for the names
|
64
|
+
# (keys) in a JSON object. Otherwise strings are returned, which is also
|
65
|
+
# the default.
|
66
|
+
# * *create_additions*: If set to false, the Parser doesn't create
|
67
|
+
# additions even if a matchin class and create_id was found. This option
|
68
|
+
# defaults to true.
|
69
|
+
# * *object_class*: Defaults to Hash
|
70
|
+
# * *array_class*: Defaults to Array
|
71
|
+
def initialize(source, opts = {})
|
72
|
+
opts ||= {}
|
73
|
+
if defined?(::Encoding)
|
74
|
+
if source.encoding == ::Encoding::ASCII_8BIT
|
75
|
+
b = source[0, 4].bytes.to_a
|
76
|
+
source = case
|
77
|
+
when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
|
78
|
+
source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
|
79
|
+
when b.size >= 4 && b[0] == 0 && b[2] == 0
|
80
|
+
source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
|
81
|
+
when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
|
82
|
+
source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
|
83
|
+
|
84
|
+
when b.size >= 4 && b[1] == 0 && b[3] == 0
|
85
|
+
source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
|
86
|
+
else
|
87
|
+
source.dup
|
88
|
+
end
|
89
|
+
else
|
90
|
+
source = source.encode(::Encoding::UTF_8)
|
91
|
+
end
|
92
|
+
source.force_encoding(::Encoding::ASCII_8BIT)
|
93
|
+
else
|
94
|
+
b = source
|
95
|
+
source = case
|
96
|
+
when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
|
97
|
+
JSON.iconv('utf-8', 'utf-32be', b)
|
98
|
+
when b.size >= 4 && b[0] == 0 && b[2] == 0
|
99
|
+
JSON.iconv('utf-8', 'utf-16be', b)
|
100
|
+
when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
|
101
|
+
JSON.iconv('utf-8', 'utf-32le', b)
|
102
|
+
when b.size >= 4 && b[1] == 0 && b[3] == 0
|
103
|
+
JSON.iconv('utf-8', 'utf-16le', b)
|
104
|
+
else
|
105
|
+
b
|
106
|
+
end
|
107
|
+
end
|
108
|
+
super source
|
109
|
+
if !opts.key?(:max_nesting) # defaults to 19
|
110
|
+
@max_nesting = 19
|
111
|
+
elsif opts[:max_nesting]
|
112
|
+
@max_nesting = opts[:max_nesting]
|
113
|
+
else
|
114
|
+
@max_nesting = 0
|
115
|
+
end
|
116
|
+
@allow_nan = !!opts[:allow_nan]
|
117
|
+
@symbolize_names = !!opts[:symbolize_names]
|
118
|
+
@create_additions = opts.key?(:create_additions) ? !!opts[:create_additions] : true
|
119
|
+
@create_id = opts[:create_id] || JSON.create_id
|
120
|
+
@object_class = opts[:object_class] || Hash
|
121
|
+
@array_class = opts[:array_class] || Array
|
122
|
+
@match_string = opts[:match_string]
|
123
|
+
end
|
124
|
+
|
125
|
+
alias source string
|
126
|
+
|
127
|
+
# Parses the current JSON string _source_ and returns the complete data
|
128
|
+
# structure as a result.
|
129
|
+
def parse
|
130
|
+
reset
|
131
|
+
obj = nil
|
132
|
+
until eos?
|
133
|
+
case
|
134
|
+
when scan(OBJECT_OPEN)
|
135
|
+
obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
|
136
|
+
@current_nesting = 1
|
137
|
+
obj = parse_object
|
138
|
+
when scan(ARRAY_OPEN)
|
139
|
+
obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
|
140
|
+
@current_nesting = 1
|
141
|
+
obj = parse_array
|
142
|
+
when skip(IGNORE)
|
143
|
+
;
|
144
|
+
else
|
145
|
+
raise ParserError, "source '#{peek(20)}' not in JSON!"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
obj or raise ParserError, "source did not contain any JSON!"
|
149
|
+
obj
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
# Unescape characters in strings.
|
155
|
+
UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
|
156
|
+
UNESCAPE_MAP.update({
|
157
|
+
?" => '"',
|
158
|
+
?\\ => '\\',
|
159
|
+
?/ => '/',
|
160
|
+
?b => "\b",
|
161
|
+
?f => "\f",
|
162
|
+
?n => "\n",
|
163
|
+
?r => "\r",
|
164
|
+
?t => "\t",
|
165
|
+
?u => nil,
|
166
|
+
})
|
167
|
+
|
168
|
+
EMPTY_8BIT_STRING = ''
|
169
|
+
if ::String.method_defined?(:encode)
|
170
|
+
EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
|
171
|
+
end
|
172
|
+
|
173
|
+
def parse_string
|
174
|
+
if scan(STRING)
|
175
|
+
return '' if self[1].empty?
|
176
|
+
string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
|
177
|
+
if u = UNESCAPE_MAP[$&[1]]
|
178
|
+
u
|
179
|
+
else # \uXXXX
|
180
|
+
bytes = EMPTY_8BIT_STRING.dup
|
181
|
+
i = 0
|
182
|
+
while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
|
183
|
+
bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
|
184
|
+
i += 1
|
185
|
+
end
|
186
|
+
JSON.iconv('utf-8', 'utf-16be', bytes)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
if string.respond_to?(:force_encoding)
|
190
|
+
string.force_encoding(::Encoding::UTF_8)
|
191
|
+
end
|
192
|
+
if @create_additions and @match_string
|
193
|
+
for (regexp, klass) in @match_string
|
194
|
+
klass.json_creatable? or next
|
195
|
+
string =~ regexp and return klass.json_create(string)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
string
|
199
|
+
else
|
200
|
+
UNPARSED
|
201
|
+
end
|
202
|
+
rescue => e
|
203
|
+
raise ParserError, "Caught #{e.class} at '#{peek(20)}': #{e}"
|
204
|
+
end
|
205
|
+
|
206
|
+
def parse_value
|
207
|
+
case
|
208
|
+
when scan(FLOAT)
|
209
|
+
Float(self[1])
|
210
|
+
when scan(INTEGER)
|
211
|
+
Integer(self[1])
|
212
|
+
when scan(TRUE)
|
213
|
+
true
|
214
|
+
when scan(FALSE)
|
215
|
+
false
|
216
|
+
when scan(NULL)
|
217
|
+
nil
|
218
|
+
when (string = parse_string) != UNPARSED
|
219
|
+
string
|
220
|
+
when scan(ARRAY_OPEN)
|
221
|
+
@current_nesting += 1
|
222
|
+
ary = parse_array
|
223
|
+
@current_nesting -= 1
|
224
|
+
ary
|
225
|
+
when scan(OBJECT_OPEN)
|
226
|
+
@current_nesting += 1
|
227
|
+
obj = parse_object
|
228
|
+
@current_nesting -= 1
|
229
|
+
obj
|
230
|
+
when @allow_nan && scan(NAN)
|
231
|
+
NaN
|
232
|
+
when @allow_nan && scan(INFINITY)
|
233
|
+
Infinity
|
234
|
+
when @allow_nan && scan(MINUS_INFINITY)
|
235
|
+
MinusInfinity
|
236
|
+
else
|
237
|
+
UNPARSED
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def parse_array
|
242
|
+
raise NestingError, "nesting of #@current_nesting is too deep" if
|
243
|
+
@max_nesting.nonzero? && @current_nesting > @max_nesting
|
244
|
+
result = @array_class.new
|
245
|
+
delim = false
|
246
|
+
until eos?
|
247
|
+
case
|
248
|
+
when (value = parse_value) != UNPARSED
|
249
|
+
delim = false
|
250
|
+
result << value
|
251
|
+
skip(IGNORE)
|
252
|
+
if scan(COLLECTION_DELIMITER)
|
253
|
+
delim = true
|
254
|
+
elsif match?(ARRAY_CLOSE)
|
255
|
+
;
|
256
|
+
else
|
257
|
+
raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!"
|
258
|
+
end
|
259
|
+
when scan(ARRAY_CLOSE)
|
260
|
+
if delim
|
261
|
+
raise ParserError, "expected next element in array at '#{peek(20)}'!"
|
262
|
+
end
|
263
|
+
break
|
264
|
+
when skip(IGNORE)
|
265
|
+
;
|
266
|
+
else
|
267
|
+
raise ParserError, "unexpected token in array at '#{peek(20)}'!"
|
268
|
+
end
|
269
|
+
end
|
270
|
+
result
|
271
|
+
end
|
272
|
+
|
273
|
+
def parse_object
|
274
|
+
raise NestingError, "nesting of #@current_nesting is too deep" if
|
275
|
+
@max_nesting.nonzero? && @current_nesting > @max_nesting
|
276
|
+
result = @object_class.new
|
277
|
+
delim = false
|
278
|
+
until eos?
|
279
|
+
case
|
280
|
+
when (string = parse_string) != UNPARSED
|
281
|
+
skip(IGNORE)
|
282
|
+
unless scan(PAIR_DELIMITER)
|
283
|
+
raise ParserError, "expected ':' in object at '#{peek(20)}'!"
|
284
|
+
end
|
285
|
+
skip(IGNORE)
|
286
|
+
unless (value = parse_value).equal? UNPARSED
|
287
|
+
result[@symbolize_names ? string.to_sym : string] = value
|
288
|
+
delim = false
|
289
|
+
skip(IGNORE)
|
290
|
+
if scan(COLLECTION_DELIMITER)
|
291
|
+
delim = true
|
292
|
+
elsif match?(OBJECT_CLOSE)
|
293
|
+
;
|
294
|
+
else
|
295
|
+
raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!"
|
296
|
+
end
|
297
|
+
else
|
298
|
+
raise ParserError, "expected value in object at '#{peek(20)}'!"
|
299
|
+
end
|
300
|
+
when scan(OBJECT_CLOSE)
|
301
|
+
if delim
|
302
|
+
raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
|
303
|
+
end
|
304
|
+
if @create_additions and klassname = result[@create_id]
|
305
|
+
klass = JSON.deep_const_get klassname
|
306
|
+
break unless klass and klass.json_creatable?
|
307
|
+
result = klass.json_create(result)
|
308
|
+
end
|
309
|
+
break
|
310
|
+
when skip(IGNORE)
|
311
|
+
;
|
312
|
+
else
|
313
|
+
raise ParserError, "unexpected token in object at '#{peek(20)}'!"
|
314
|
+
end
|
315
|
+
end
|
316
|
+
result
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stripe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 1
|
7
8
|
- 5
|
8
|
-
-
|
9
|
-
version: 1.5.
|
9
|
+
- 14
|
10
|
+
version: 1.5.14
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Ross Boucher
|
@@ -15,16 +16,18 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2011-
|
19
|
+
date: 2011-08-08 00:00:00 -07:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
22
23
|
name: rest-client
|
23
24
|
prerelease: false
|
24
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
25
27
|
requirements:
|
26
28
|
- - ">="
|
27
29
|
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
28
31
|
segments:
|
29
32
|
- 0
|
30
33
|
version: "0"
|
@@ -34,9 +37,11 @@ dependencies:
|
|
34
37
|
name: mocha
|
35
38
|
prerelease: false
|
36
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
37
41
|
requirements:
|
38
42
|
- - ">="
|
39
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
40
45
|
segments:
|
41
46
|
- 0
|
42
47
|
version: "0"
|
@@ -46,9 +51,11 @@ dependencies:
|
|
46
51
|
name: shoulda
|
47
52
|
prerelease: false
|
48
53
|
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
49
55
|
requirements:
|
50
56
|
- - ">="
|
51
57
|
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
52
59
|
segments:
|
53
60
|
- 0
|
54
61
|
version: "0"
|
@@ -58,9 +65,11 @@ dependencies:
|
|
58
65
|
name: test-unit
|
59
66
|
prerelease: false
|
60
67
|
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
61
69
|
requirements:
|
62
70
|
- - ">="
|
63
71
|
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
64
73
|
segments:
|
65
74
|
- 0
|
66
75
|
version: "0"
|
@@ -81,6 +90,11 @@ files:
|
|
81
90
|
- lib/stripe.rb
|
82
91
|
- lib/stripe/version.rb
|
83
92
|
- lib/data/ca-certificates.crt
|
93
|
+
- vendor/stripe-json/lib/json/pure.rb
|
94
|
+
- vendor/stripe-json/lib/json/common.rb
|
95
|
+
- vendor/stripe-json/lib/json/version.rb
|
96
|
+
- vendor/stripe-json/lib/json/pure/generator.rb
|
97
|
+
- vendor/stripe-json/lib/json/pure/parser.rb
|
84
98
|
has_rdoc: true
|
85
99
|
homepage: https://stripe.com/api
|
86
100
|
licenses: []
|
@@ -91,23 +105,27 @@ rdoc_options: []
|
|
91
105
|
require_paths:
|
92
106
|
- lib
|
93
107
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
94
109
|
requirements:
|
95
110
|
- - ">="
|
96
111
|
- !ruby/object:Gem::Version
|
112
|
+
hash: 3
|
97
113
|
segments:
|
98
114
|
- 0
|
99
115
|
version: "0"
|
100
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
101
118
|
requirements:
|
102
119
|
- - ">="
|
103
120
|
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
104
122
|
segments:
|
105
123
|
- 0
|
106
124
|
version: "0"
|
107
125
|
requirements: []
|
108
126
|
|
109
127
|
rubyforge_project:
|
110
|
-
rubygems_version: 1.3.
|
128
|
+
rubygems_version: 1.3.7
|
111
129
|
signing_key:
|
112
130
|
specification_version: 3
|
113
131
|
summary: Ruby bindings for the Stripe API
|