taps 0.3.18 → 0.3.19.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+ 1.5.1
@@ -0,0 +1,10 @@
1
+ require 'json/common'
2
+ module JSON
3
+ require 'json/version'
4
+
5
+ begin
6
+ require 'json/ext'
7
+ rescue LoadError
8
+ require 'json/pure'
9
+ end
10
+ end
@@ -0,0 +1,147 @@
1
+ # This file contains implementations of ruby core's custom objects for
2
+ # serialisation/deserialisation.
3
+
4
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
5
+ require 'json'
6
+ end
7
+ require 'date'
8
+
9
+ class Symbol
10
+ def to_json(*a)
11
+ {
12
+ JSON.create_id => self.class.name,
13
+ 's' => to_s,
14
+ }.to_json(*a)
15
+ end
16
+
17
+ def self.json_create(o)
18
+ o['s'].to_sym
19
+ end
20
+ end
21
+
22
+ class Time
23
+ def self.json_create(object)
24
+ if usec = object.delete('u') # used to be tv_usec -> tv_nsec
25
+ object['n'] = usec * 1000
26
+ end
27
+ if respond_to?(:tv_nsec)
28
+ at(*object.values_at('s', 'n'))
29
+ else
30
+ at(object['s'], object['n'] / 1000)
31
+ end
32
+ end
33
+
34
+ def to_json(*args)
35
+ {
36
+ JSON.create_id => self.class.name,
37
+ 's' => tv_sec,
38
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
39
+ }.to_json(*args)
40
+ end
41
+ end
42
+
43
+ class Date
44
+ def self.json_create(object)
45
+ civil(*object.values_at('y', 'm', 'd', 'sg'))
46
+ end
47
+
48
+ alias start sg unless method_defined?(:start)
49
+
50
+ def to_json(*args)
51
+ {
52
+ JSON.create_id => self.class.name,
53
+ 'y' => year,
54
+ 'm' => month,
55
+ 'd' => day,
56
+ 'sg' => start,
57
+ }.to_json(*args)
58
+ end
59
+ end
60
+
61
+ class DateTime
62
+ def self.json_create(object)
63
+ args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
64
+ of_a, of_b = object['of'].split('/')
65
+ if of_b and of_b != '0'
66
+ args << Rational(of_a.to_i, of_b.to_i)
67
+ else
68
+ args << of_a
69
+ end
70
+ args << object['sg']
71
+ civil(*args)
72
+ end
73
+
74
+ alias start sg unless method_defined?(:start)
75
+
76
+ def to_json(*args)
77
+ {
78
+ JSON.create_id => self.class.name,
79
+ 'y' => year,
80
+ 'm' => month,
81
+ 'd' => day,
82
+ 'H' => hour,
83
+ 'M' => min,
84
+ 'S' => sec,
85
+ 'of' => offset.to_s,
86
+ 'sg' => start,
87
+ }.to_json(*args)
88
+ end
89
+ end
90
+
91
+ class Range
92
+ def self.json_create(object)
93
+ new(*object['a'])
94
+ end
95
+
96
+ def to_json(*args)
97
+ {
98
+ JSON.create_id => self.class.name,
99
+ 'a' => [ first, last, exclude_end? ]
100
+ }.to_json(*args)
101
+ end
102
+ end
103
+
104
+ class Struct
105
+ def self.json_create(object)
106
+ new(*object['v'])
107
+ end
108
+
109
+ def to_json(*args)
110
+ klass = self.class.name
111
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
112
+ {
113
+ JSON.create_id => klass,
114
+ 'v' => values,
115
+ }.to_json(*args)
116
+ end
117
+ end
118
+
119
+ class Exception
120
+ def self.json_create(object)
121
+ result = new(object['m'])
122
+ result.set_backtrace object['b']
123
+ result
124
+ end
125
+
126
+ def to_json(*args)
127
+ {
128
+ JSON.create_id => self.class.name,
129
+ 'm' => message,
130
+ 'b' => backtrace,
131
+ }.to_json(*args)
132
+ end
133
+ end
134
+
135
+ class Regexp
136
+ def self.json_create(object)
137
+ new(object['s'], object['o'])
138
+ end
139
+
140
+ def to_json(*)
141
+ {
142
+ JSON.create_id => self.class.name,
143
+ 'o' => options,
144
+ 's' => source,
145
+ }.to_json
146
+ end
147
+ end
@@ -0,0 +1,8 @@
1
+ # This file used to implementations of rails custom objects for
2
+ # serialisation/deserialisation and is obsoleted now.
3
+
4
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
5
+ require 'json'
6
+ end
7
+
8
+ $DEBUG and warn "required json/add/rails which is obsolete now!"
@@ -0,0 +1,419 @@
1
+ require 'json/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
+ def recurse_proc(result, &proc)
296
+ case result
297
+ when Array
298
+ result.each { |x| recurse_proc x, &proc }
299
+ proc.call result
300
+ when Hash
301
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
302
+ proc.call result
303
+ else
304
+ proc.call result
305
+ end
306
+ end
307
+
308
+ alias restore load
309
+ module_function :restore
310
+
311
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
312
+ # the result.
313
+ #
314
+ # If anIO (an IO like object or an object that responds to the write method)
315
+ # was given, the resulting JSON is written to it.
316
+ #
317
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
318
+ # exception is raised. This argument is similar (but not exactly the
319
+ # same!) to the _limit_ argument in Marshal.dump.
320
+ #
321
+ # This method is part of the implementation of the load/dump interface of
322
+ # Marshal and YAML.
323
+ def dump(obj, anIO = nil, limit = nil)
324
+ if anIO and limit.nil?
325
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
326
+ unless anIO.respond_to?(:write)
327
+ limit = anIO
328
+ anIO = nil
329
+ end
330
+ end
331
+ limit ||= 0
332
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
333
+ if anIO
334
+ anIO.write result
335
+ anIO
336
+ else
337
+ result
338
+ end
339
+ rescue JSON::NestingError
340
+ raise ArgumentError, "exceed depth limit"
341
+ end
342
+
343
+ # Swap consecutive bytes of _string_ in place.
344
+ def self.swap!(string) # :nodoc:
345
+ 0.upto(string.size / 2) do |i|
346
+ break unless string[2 * i + 1]
347
+ string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
348
+ end
349
+ string
350
+ end
351
+
352
+ # Shortuct for iconv.
353
+ if ::String.method_defined?(:encode)
354
+ def self.iconv(to, from, string)
355
+ string.encode(to, from)
356
+ end
357
+ else
358
+ require 'iconv'
359
+ def self.iconv(to, from, string)
360
+ Iconv.iconv(to, from, string).first
361
+ end
362
+ end
363
+
364
+ if ::Object.method(:const_defined?).arity == 1
365
+ def self.const_defined_in?(modul, constant)
366
+ modul.const_defined?(constant)
367
+ end
368
+ else
369
+ def self.const_defined_in?(modul, constant)
370
+ modul.const_defined?(constant, false)
371
+ end
372
+ end
373
+ end
374
+
375
+ module ::Kernel
376
+ private
377
+
378
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
379
+ # one line.
380
+ def j(*objs)
381
+ objs.each do |obj|
382
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
383
+ end
384
+ nil
385
+ end
386
+
387
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
388
+ # indentation and over many lines.
389
+ def jj(*objs)
390
+ objs.each do |obj|
391
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
392
+ end
393
+ nil
394
+ end
395
+
396
+ # If _object_ is string-like parse the string and return the parsed result as
397
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
398
+ # structure object and return it.
399
+ #
400
+ # The _opts_ argument is passed through to generate/parse respectively, see
401
+ # generate and parse for their documentation.
402
+ def JSON(object, *args)
403
+ if object.respond_to? :to_str
404
+ JSON.parse(object.to_str, args.first)
405
+ else
406
+ JSON.generate(object, args.first)
407
+ end
408
+ end
409
+ end
410
+
411
+ class ::Class
412
+ # Returns true, if this class can be used to create an instance
413
+ # from a serialised JSON string. The class has to implement a class
414
+ # method _json_create_ that expects a hash as first parameter, which includes
415
+ # the required data.
416
+ def json_creatable?
417
+ respond_to?(:json_create)
418
+ end
419
+ end