bugly 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,426 @@
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 is what activesupport expects :(
111
+ class ParseError < ParserError; end
112
+
113
+ # This exception is raised, if the nesting of parsed datastructures is too
114
+ # deep.
115
+ class NestingError < ParserError; end
116
+
117
+ # :stopdoc:
118
+ class CircularDatastructure < NestingError; end
119
+ # :startdoc:
120
+
121
+ # This exception is raised, if a generator or unparser error occurs.
122
+ class GeneratorError < JSONError; end
123
+ # For backwards compatibility
124
+ UnparserError = GeneratorError
125
+
126
+ # This exception is raised, if the required unicode support is missing on the
127
+ # system. Usually this means, that the iconv library is not installed.
128
+ class MissingUnicodeSupport < JSONError; end
129
+
130
+ module_function
131
+
132
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
133
+ #
134
+ # _opts_ can have the following
135
+ # keys:
136
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
137
+ # structures. Disable depth checking with :max_nesting => false, it defaults
138
+ # to 19.
139
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
140
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
141
+ # to false.
142
+ # * *symbolize_names*: If set to true, returns symbols for the names
143
+ # (keys) in a JSON object. Otherwise strings are returned, which is also
144
+ # the default.
145
+ # * *create_additions*: If set to false, the Parser doesn't create
146
+ # additions even if a matchin class and create_id was found. This option
147
+ # defaults to true.
148
+ # * *object_class*: Defaults to Hash
149
+ # * *array_class*: Defaults to Array
150
+ def parse(source, opts = {})
151
+ Parser.new(source, opts).parse
152
+ end
153
+
154
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
155
+ # The bang version of the parse method, defaults to the more dangerous values
156
+ # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
157
+ #
158
+ # _opts_ can have the following keys:
159
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
160
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
161
+ # methods defaults to not doing max depth checking: This can be dangerous,
162
+ # if someone wants to fill up your stack.
163
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
164
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
165
+ # to true.
166
+ # * *create_additions*: If set to false, the Parser doesn't create
167
+ # additions even if a matchin class and create_id was found. This option
168
+ # defaults to true.
169
+ def parse!(source, opts = {})
170
+ opts = {
171
+ :max_nesting => false,
172
+ :allow_nan => true
173
+ }.update(opts)
174
+ Parser.new(source, opts).parse
175
+ end
176
+
177
+ # Generate a JSON document from the Ruby data structure _obj_ and return
178
+ # it. _state_ is * a JSON::State object,
179
+ # * or a Hash like object (responding to to_hash),
180
+ # * an object convertible into a hash by a to_h method,
181
+ # that is used as or to configure a State object.
182
+ #
183
+ # It defaults to a state object, that creates the shortest possible JSON text
184
+ # in one line, checks for circular data structures and doesn't allow NaN,
185
+ # Infinity, and -Infinity.
186
+ #
187
+ # A _state_ hash can have the following keys:
188
+ # * *indent*: a string used to indent levels (default: ''),
189
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
190
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
191
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
192
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
193
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
194
+ # generated, otherwise an exception is thrown, if these values are
195
+ # encountered. This options defaults to false.
196
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
197
+ # structures from which JSON is to be generated. Disable depth checking
198
+ # with :max_nesting => false, it defaults to 19.
199
+ #
200
+ # See also the fast_generate for the fastest creation method with the least
201
+ # amount of sanity checks, and the pretty_generate method for some
202
+ # defaults for a pretty output.
203
+ def generate(obj, opts = nil)
204
+ state = SAFE_STATE_PROTOTYPE.dup
205
+ if opts
206
+ if opts.respond_to? :to_hash
207
+ opts = opts.to_hash
208
+ elsif opts.respond_to? :to_h
209
+ opts = opts.to_h
210
+ else
211
+ raise TypeError, "can't convert #{opts.class} into Hash"
212
+ end
213
+ state = state.configure(opts)
214
+ end
215
+ state.generate(obj)
216
+ end
217
+
218
+ # :stopdoc:
219
+ # I want to deprecate these later, so I'll first be silent about them, and
220
+ # later delete them.
221
+ alias unparse generate
222
+ module_function :unparse
223
+ # :startdoc:
224
+
225
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
226
+ # This method disables the checks for circles in Ruby objects.
227
+ #
228
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
229
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
230
+ def fast_generate(obj, opts = nil)
231
+ state = FAST_STATE_PROTOTYPE.dup
232
+ if opts
233
+ if opts.respond_to? :to_hash
234
+ opts = opts.to_hash
235
+ elsif opts.respond_to? :to_h
236
+ opts = opts.to_h
237
+ else
238
+ raise TypeError, "can't convert #{opts.class} into Hash"
239
+ end
240
+ state.configure(opts)
241
+ end
242
+ state.generate(obj)
243
+ end
244
+
245
+ # :stopdoc:
246
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
247
+ alias fast_unparse fast_generate
248
+ module_function :fast_unparse
249
+ # :startdoc:
250
+
251
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
252
+ # The returned document is a prettier form of the document returned by
253
+ # #unparse.
254
+ #
255
+ # The _opts_ argument can be used to configure the generator, see the
256
+ # generate method for a more detailed explanation.
257
+ def pretty_generate(obj, opts = nil)
258
+ state = PRETTY_STATE_PROTOTYPE.dup
259
+ if opts
260
+ if opts.respond_to? :to_hash
261
+ opts = opts.to_hash
262
+ elsif opts.respond_to? :to_h
263
+ opts = opts.to_h
264
+ else
265
+ raise TypeError, "can't convert #{opts.class} into Hash"
266
+ end
267
+ state.configure(opts)
268
+ end
269
+ state.generate(obj)
270
+ end
271
+
272
+ # :stopdoc:
273
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
274
+ alias pretty_unparse pretty_generate
275
+ module_function :pretty_unparse
276
+ # :startdoc:
277
+
278
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
279
+ # either be a string-like object, an IO like object, or an object responding
280
+ # to the read method. If _proc_ was given, it will be called with any nested
281
+ # Ruby object as an argument recursively in depth first order.
282
+ #
283
+ # This method is part of the implementation of the load/dump interface of
284
+ # Marshal and YAML.
285
+ def load(source, proc = nil)
286
+ if source.respond_to? :to_str
287
+ source = source.to_str
288
+ elsif source.respond_to? :to_io
289
+ source = source.to_io.read
290
+ else
291
+ source = source.read
292
+ end
293
+ result = parse(source, :max_nesting => false, :allow_nan => true)
294
+ recurse_proc(result, &proc) if proc
295
+ result
296
+ end
297
+
298
+ # Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
299
+ def recurse_proc(result, &proc)
300
+ case result
301
+ when Array
302
+ result.each { |x| recurse_proc x, &proc }
303
+ proc.call result
304
+ when Hash
305
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
306
+ proc.call result
307
+ else
308
+ proc.call result
309
+ end
310
+ end
311
+
312
+ alias restore load
313
+ module_function :restore
314
+
315
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
316
+ # the result.
317
+ #
318
+ # If anIO (an IO like object or an object that responds to the write method)
319
+ # was given, the resulting JSON is written to it.
320
+ #
321
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
322
+ # exception is raised. This argument is similar (but not exactly the
323
+ # same!) to the _limit_ argument in Marshal.dump.
324
+ #
325
+ # This method is part of the implementation of the load/dump interface of
326
+ # Marshal and YAML.
327
+ def dump(obj, anIO = nil, limit = nil)
328
+ if anIO and limit.nil?
329
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
330
+ unless anIO.respond_to?(:write)
331
+ limit = anIO
332
+ anIO = nil
333
+ end
334
+ end
335
+ limit ||= 0
336
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
337
+ if anIO
338
+ anIO.write result
339
+ anIO
340
+ else
341
+ result
342
+ end
343
+ rescue JSON::NestingError
344
+ raise ArgumentError, "exceed depth limit"
345
+ end
346
+
347
+ # Swap consecutive bytes of _string_ in place.
348
+ def self.swap!(string) # :nodoc:
349
+ 0.upto(string.size / 2) do |i|
350
+ break unless string[2 * i + 1]
351
+ string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
352
+ end
353
+ string
354
+ end
355
+
356
+ # Shortuct for iconv.
357
+ if ::String.method_defined?(:encode)
358
+ # Encodes string using Ruby's _String.encode_
359
+ def self.iconv(to, from, string)
360
+ string.encode(to, from)
361
+ end
362
+ else
363
+ require 'iconv'
364
+ # Encodes string using _iconv_ library
365
+ def self.iconv(to, from, string)
366
+ Iconv.conv(to, from, string)
367
+ end
368
+ end
369
+
370
+ if ::Object.method(:const_defined?).arity == 1
371
+ def self.const_defined_in?(modul, constant)
372
+ modul.const_defined?(constant)
373
+ end
374
+ else
375
+ def self.const_defined_in?(modul, constant)
376
+ modul.const_defined?(constant, false)
377
+ end
378
+ end
379
+ end
380
+
381
+ module ::Kernel
382
+ private
383
+
384
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
385
+ # one line.
386
+ def j(*objs)
387
+ objs.each do |obj|
388
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
389
+ end
390
+ nil
391
+ end
392
+
393
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
394
+ # indentation and over many lines.
395
+ def jj(*objs)
396
+ objs.each do |obj|
397
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
398
+ end
399
+ nil
400
+ end
401
+
402
+ # If _object_ is string-like parse the string and return the parsed result as
403
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
404
+ # structure object and return it.
405
+ #
406
+ # The _opts_ argument is passed through to generate/parse respectively, see
407
+ # generate and parse for their documentation.
408
+ def JSON(object, *args)
409
+ if object.respond_to? :to_str
410
+ JSON.parse(object.to_str, args.first)
411
+ else
412
+ JSON.generate(object, args.first)
413
+ end
414
+ end
415
+ end
416
+
417
+ # Extends any Class to include _json_creatable?_ method.
418
+ class ::Class
419
+ # Returns true, if this class can be used to create an instance
420
+ # from a serialised JSON string. The class has to implement a class
421
+ # method _json_create_ that expects a hash as first parameter, which includes
422
+ # the required data.
423
+ def json_creatable?
424
+ respond_to?(:json_create)
425
+ end
426
+ 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