engineyard-serverside 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/lib/engineyard-serverside.rb +3 -3
  2. data/lib/engineyard-serverside/configuration.rb +6 -6
  3. data/lib/engineyard-serverside/deploy.rb +11 -7
  4. data/lib/engineyard-serverside/version.rb +1 -1
  5. data/lib/vendor/multi_json/CHANGELOG.md +121 -0
  6. data/lib/vendor/multi_json/CONTRIBUTING.md +46 -0
  7. data/lib/vendor/multi_json/Gemfile +31 -0
  8. data/lib/vendor/multi_json/LICENSE.md +20 -0
  9. data/lib/vendor/multi_json/README.md +105 -0
  10. data/lib/vendor/multi_json/Rakefile +12 -0
  11. data/lib/vendor/multi_json/lib/multi_json.rb +137 -0
  12. data/lib/vendor/multi_json/lib/multi_json/adapters/gson.rb +20 -0
  13. data/lib/vendor/multi_json/lib/multi_json/adapters/json_common.rb +35 -0
  14. data/lib/vendor/multi_json/lib/multi_json/adapters/json_gem.rb +12 -0
  15. data/lib/vendor/multi_json/lib/multi_json/adapters/json_pure.rb +12 -0
  16. data/lib/vendor/multi_json/lib/multi_json/adapters/nsjsonserialization.rb +35 -0
  17. data/lib/vendor/multi_json/lib/multi_json/adapters/oj.rb +29 -0
  18. data/lib/vendor/multi_json/lib/multi_json/adapters/ok_json.rb +58 -0
  19. data/lib/vendor/multi_json/lib/multi_json/adapters/yajl.rb +20 -0
  20. data/lib/vendor/multi_json/lib/multi_json/vendor/okjson.rb +602 -0
  21. data/lib/vendor/multi_json/lib/multi_json/version.rb +3 -0
  22. data/lib/vendor/multi_json/multi_json.gemspec +22 -0
  23. data/lib/vendor/multi_json/spec/adapter_shared_example.rb +162 -0
  24. data/lib/vendor/multi_json/spec/helper.rb +45 -0
  25. data/lib/vendor/multi_json/spec/json_common_shared_example.rb +36 -0
  26. data/lib/vendor/multi_json/spec/multi_json_spec.rb +151 -0
  27. data/spec/configuration_spec.rb +79 -1
  28. data/spec/deploy_hook_spec.rb +2 -2
  29. data/spec/spec_helper.rb +1 -1
  30. metadata +26 -120
  31. data/lib/vendor/json_pure/CHANGES +0 -166
  32. data/lib/vendor/json_pure/COPYING +0 -58
  33. data/lib/vendor/json_pure/GPL +0 -340
  34. data/lib/vendor/json_pure/README +0 -358
  35. data/lib/vendor/json_pure/Rakefile +0 -292
  36. data/lib/vendor/json_pure/TODO +0 -1
  37. data/lib/vendor/json_pure/VERSION +0 -1
  38. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +0 -52
  39. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +0 -1000
  40. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +0 -1001
  41. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +0 -900
  42. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +0 -901
  43. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +0 -1000
  44. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +0 -1001
  45. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +0 -261
  46. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +0 -1000
  47. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +0 -1001
  48. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +0 -1000
  49. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +0 -1001
  50. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +0 -1000
  51. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +0 -1001
  52. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +0 -262
  53. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +0 -1000
  54. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +0 -1001
  55. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +0 -82
  56. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +0 -34
  57. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +0 -900
  58. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +0 -901
  59. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +0 -81
  60. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +0 -1000
  61. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +0 -1001
  62. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +0 -82
  63. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +0 -1000
  64. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +0 -1001
  65. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +0 -82
  66. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +0 -1000
  67. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +0 -1001
  68. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +0 -82
  69. data/lib/vendor/json_pure/benchmarks/generator2_benchmark.rb +0 -222
  70. data/lib/vendor/json_pure/benchmarks/generator_benchmark.rb +0 -224
  71. data/lib/vendor/json_pure/benchmarks/ohai.json +0 -1216
  72. data/lib/vendor/json_pure/benchmarks/ohai.ruby +0 -1
  73. data/lib/vendor/json_pure/benchmarks/parser2_benchmark.rb +0 -251
  74. data/lib/vendor/json_pure/benchmarks/parser_benchmark.rb +0 -259
  75. data/lib/vendor/json_pure/bin/edit_json.rb +0 -9
  76. data/lib/vendor/json_pure/bin/prettify_json.rb +0 -75
  77. data/lib/vendor/json_pure/data/example.json +0 -1
  78. data/lib/vendor/json_pure/data/index.html +0 -38
  79. data/lib/vendor/json_pure/data/prototype.js +0 -4184
  80. data/lib/vendor/json_pure/ext/json/ext/generator/extconf.rb +0 -16
  81. data/lib/vendor/json_pure/ext/json/ext/generator/generator.c +0 -1323
  82. data/lib/vendor/json_pure/ext/json/ext/generator/generator.h +0 -170
  83. data/lib/vendor/json_pure/ext/json/ext/parser/extconf.rb +0 -15
  84. data/lib/vendor/json_pure/ext/json/ext/parser/parser.c +0 -1935
  85. data/lib/vendor/json_pure/ext/json/ext/parser/parser.h +0 -71
  86. data/lib/vendor/json_pure/ext/json/ext/parser/parser.rl +0 -792
  87. data/lib/vendor/json_pure/install.rb +0 -26
  88. data/lib/vendor/json_pure/lib/json.rb +0 -10
  89. data/lib/vendor/json_pure/lib/json/Array.xpm +0 -21
  90. data/lib/vendor/json_pure/lib/json/FalseClass.xpm +0 -21
  91. data/lib/vendor/json_pure/lib/json/Hash.xpm +0 -21
  92. data/lib/vendor/json_pure/lib/json/Key.xpm +0 -73
  93. data/lib/vendor/json_pure/lib/json/NilClass.xpm +0 -21
  94. data/lib/vendor/json_pure/lib/json/Numeric.xpm +0 -28
  95. data/lib/vendor/json_pure/lib/json/String.xpm +0 -96
  96. data/lib/vendor/json_pure/lib/json/TrueClass.xpm +0 -21
  97. data/lib/vendor/json_pure/lib/json/add/core.rb +0 -148
  98. data/lib/vendor/json_pure/lib/json/add/rails.rb +0 -58
  99. data/lib/vendor/json_pure/lib/json/common.rb +0 -397
  100. data/lib/vendor/json_pure/lib/json/editor.rb +0 -1371
  101. data/lib/vendor/json_pure/lib/json/ext.rb +0 -15
  102. data/lib/vendor/json_pure/lib/json/json.xpm +0 -1499
  103. data/lib/vendor/json_pure/lib/json/pure.rb +0 -77
  104. data/lib/vendor/json_pure/lib/json/pure/generator.rb +0 -452
  105. data/lib/vendor/json_pure/lib/json/pure/parser.rb +0 -307
  106. data/lib/vendor/json_pure/lib/json/version.rb +0 -8
  107. data/lib/vendor/json_pure/tests/fixtures/fail1.json +0 -1
  108. data/lib/vendor/json_pure/tests/fixtures/fail10.json +0 -1
  109. data/lib/vendor/json_pure/tests/fixtures/fail11.json +0 -1
  110. data/lib/vendor/json_pure/tests/fixtures/fail12.json +0 -1
  111. data/lib/vendor/json_pure/tests/fixtures/fail13.json +0 -1
  112. data/lib/vendor/json_pure/tests/fixtures/fail14.json +0 -1
  113. data/lib/vendor/json_pure/tests/fixtures/fail18.json +0 -1
  114. data/lib/vendor/json_pure/tests/fixtures/fail19.json +0 -1
  115. data/lib/vendor/json_pure/tests/fixtures/fail2.json +0 -1
  116. data/lib/vendor/json_pure/tests/fixtures/fail20.json +0 -1
  117. data/lib/vendor/json_pure/tests/fixtures/fail21.json +0 -1
  118. data/lib/vendor/json_pure/tests/fixtures/fail22.json +0 -1
  119. data/lib/vendor/json_pure/tests/fixtures/fail23.json +0 -1
  120. data/lib/vendor/json_pure/tests/fixtures/fail24.json +0 -1
  121. data/lib/vendor/json_pure/tests/fixtures/fail25.json +0 -1
  122. data/lib/vendor/json_pure/tests/fixtures/fail27.json +0 -2
  123. data/lib/vendor/json_pure/tests/fixtures/fail28.json +0 -2
  124. data/lib/vendor/json_pure/tests/fixtures/fail3.json +0 -1
  125. data/lib/vendor/json_pure/tests/fixtures/fail4.json +0 -1
  126. data/lib/vendor/json_pure/tests/fixtures/fail5.json +0 -1
  127. data/lib/vendor/json_pure/tests/fixtures/fail6.json +0 -1
  128. data/lib/vendor/json_pure/tests/fixtures/fail7.json +0 -1
  129. data/lib/vendor/json_pure/tests/fixtures/fail8.json +0 -1
  130. data/lib/vendor/json_pure/tests/fixtures/fail9.json +0 -1
  131. data/lib/vendor/json_pure/tests/fixtures/pass1.json +0 -56
  132. data/lib/vendor/json_pure/tests/fixtures/pass15.json +0 -1
  133. data/lib/vendor/json_pure/tests/fixtures/pass16.json +0 -1
  134. data/lib/vendor/json_pure/tests/fixtures/pass17.json +0 -1
  135. data/lib/vendor/json_pure/tests/fixtures/pass2.json +0 -1
  136. data/lib/vendor/json_pure/tests/fixtures/pass26.json +0 -1
  137. data/lib/vendor/json_pure/tests/fixtures/pass3.json +0 -6
  138. data/lib/vendor/json_pure/tests/test_json.rb +0 -361
  139. data/lib/vendor/json_pure/tests/test_json_addition.rb +0 -162
  140. data/lib/vendor/json_pure/tests/test_json_encoding.rb +0 -68
  141. data/lib/vendor/json_pure/tests/test_json_fixtures.rb +0 -34
  142. data/lib/vendor/json_pure/tests/test_json_generate.rb +0 -122
  143. data/lib/vendor/json_pure/tests/test_json_rails.rb +0 -144
  144. data/lib/vendor/json_pure/tests/test_json_unicode.rb +0 -76
  145. data/lib/vendor/json_pure/tools/fuzz.rb +0 -139
  146. data/lib/vendor/json_pure/tools/server.rb +0 -61
@@ -1,58 +0,0 @@
1
- # This file contains implementations of rails custom objects for
2
- # serialisation/deserialisation.
3
-
4
- unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
- ::JSON::JSON_LOADED
6
- require 'json'
7
- end
8
-
9
- class Object
10
- def self.json_create(object)
11
- obj = new
12
- for key, value in object
13
- next if key == JSON.create_id
14
- instance_variable_set "@#{key}", value
15
- end
16
- obj
17
- end
18
-
19
- def to_json(*a)
20
- result = {
21
- JSON.create_id => self.class.name
22
- }
23
- instance_variables.inject(result) do |r, name|
24
- r[name[1..-1]] = instance_variable_get name
25
- r
26
- end
27
- result.to_json(*a)
28
- end
29
- end
30
-
31
- class Symbol
32
- def to_json(*a)
33
- to_s.to_json(*a)
34
- end
35
- end
36
-
37
- module Enumerable
38
- def to_json(*a)
39
- to_a.to_json(*a)
40
- end
41
- end
42
-
43
- # class Regexp
44
- # def to_json(*)
45
- # inspect
46
- # end
47
- # end
48
- #
49
- # The above rails definition has some problems:
50
- #
51
- # 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}"
52
- # This isn't valid JSON, because the regular expression syntax is not
53
- # defined in RFC 4627. (And unquoted strings are disallowed there, too.)
54
- # Though it is valid Javascript.
55
- #
56
- # 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}"
57
- # This isn't even valid Javascript.
58
-
@@ -1,397 +0,0 @@
1
- require 'json/version'
2
- require 'iconv'
3
-
4
- module JSON
5
- class << self
6
- # If _object_ is string-like parse the string and return the parsed result
7
- # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
8
- # data structure object and return it.
9
- #
10
- # The _opts_ argument is passed through to generate/parse respectively, see
11
- # generate and parse for their documentation.
12
- def [](object, opts = {})
13
- if object.respond_to? :to_str
14
- JSON.parse(object.to_str, opts => {})
15
- else
16
- JSON.generate(object, opts => {})
17
- end
18
- end
19
-
20
- # Returns the JSON parser class, that is used by JSON. This might be either
21
- # JSON::Ext::Parser or JSON::Pure::Parser.
22
- attr_reader :parser
23
-
24
- # Set the JSON parser class _parser_ to be used by JSON.
25
- def parser=(parser) # :nodoc:
26
- @parser = parser
27
- remove_const :Parser if const_defined? :Parser
28
- const_set :Parser, parser
29
- end
30
-
31
- # Return the constant located at _path_. The format of _path_ has to be
32
- # either ::A::B::C or A::B::C. In any case A has to be located at the top
33
- # level (absolute namespace path?). If there doesn't exist a constant at
34
- # the given path, an ArgumentError is raised.
35
- def deep_const_get(path) # :nodoc:
36
- path.to_s.split(/::/).inject(Object) do |p, c|
37
- case
38
- when c.empty? then p
39
- when p.const_defined?(c) then p.const_get(c)
40
- else
41
- begin
42
- p.const_missing(c)
43
- rescue NameError
44
- raise ArgumentError, "can't find const #{path}"
45
- end
46
- end
47
- end
48
- end
49
-
50
- # Set the module _generator_ to be used by JSON.
51
- def generator=(generator) # :nodoc:
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.freeze
67
- const_set :FAST_STATE_PROTOTYPE, State.new(
68
- :indent => '',
69
- :space => '',
70
- :object_nl => "",
71
- :array_nl => "",
72
- :max_nesting => false
73
- ).freeze
74
- const_set :PRETTY_STATE_PROTOTYPE, State.new(
75
- :indent => ' ',
76
- :space => ' ',
77
- :object_nl => "\n",
78
- :array_nl => "\n"
79
- ).freeze
80
- end
81
-
82
- # Returns the JSON generator modul, that is used by JSON. This might be
83
- # either JSON::Ext::Generator or JSON::Pure::Generator.
84
- attr_reader :generator
85
-
86
- # Returns the JSON generator state class, that is used by JSON. This might
87
- # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
88
- attr_accessor :state
89
-
90
- # This is create identifier, that is used to decide, if the _json_create_
91
- # hook of a class should be called. It defaults to 'json_class'.
92
- attr_accessor :create_id
93
- end
94
- self.create_id = 'json_class'
95
-
96
- NaN = 0.0/0
97
-
98
- Infinity = 1.0/0
99
-
100
- MinusInfinity = -Infinity
101
-
102
- # The base exception for JSON errors.
103
- class JSONError < StandardError; end
104
-
105
- # This exception is raised, if a parser error occurs.
106
- class ParserError < JSONError; end
107
-
108
- # This exception is raised, if the nesting of parsed datastructures is too
109
- # deep.
110
- class NestingError < ParserError; end
111
-
112
- # :stopdoc:
113
- class CircularDatastructure < NestingError; end
114
- # :startdoc:
115
-
116
- # This exception is raised, if a generator or unparser error occurs.
117
- class GeneratorError < JSONError; end
118
- # For backwards compatibility
119
- UnparserError = GeneratorError
120
-
121
- # This exception is raised, if the required unicode support is missing on the
122
- # system. Usually this means, that the iconv library is not installed.
123
- class MissingUnicodeSupport < JSONError; end
124
-
125
- module_function
126
-
127
- # Parse the JSON document _source_ into a Ruby data structure and return it.
128
- #
129
- # _opts_ can have the following
130
- # keys:
131
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
132
- # structures. Disable depth checking with :max_nesting => false, it defaults
133
- # to 19.
134
- # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
135
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
136
- # to false.
137
- # * *symbolize_names*: If set to true, returns symbols for the names
138
- # (keys) in a JSON object. Otherwise strings are returned, which is also
139
- # the default.
140
- # * *create_additions*: If set to false, the Parser doesn't create
141
- # additions even if a matchin class and create_id was found. This option
142
- # defaults to true.
143
- # * *object_class*: Defaults to Hash
144
- # * *array_class*: Defaults to Array
145
- def parse(source, opts = {})
146
- Parser.new(source, opts).parse
147
- end
148
-
149
- # Parse the JSON document _source_ into a Ruby data structure and return it.
150
- # The bang version of the parse method, defaults to the more dangerous values
151
- # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
152
- #
153
- # _opts_ can have the following keys:
154
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
155
- # structures. Enable depth checking with :max_nesting => anInteger. The parse!
156
- # methods defaults to not doing max depth checking: This can be dangerous,
157
- # if someone wants to fill up your stack.
158
- # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
159
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
160
- # to true.
161
- # * *create_additions*: If set to false, the Parser doesn't create
162
- # additions even if a matchin class and create_id was found. This option
163
- # defaults to true.
164
- def parse!(source, opts = {})
165
- opts = {
166
- :max_nesting => false,
167
- :allow_nan => true
168
- }.update(opts)
169
- Parser.new(source, opts).parse
170
- end
171
-
172
- # Generate a JSON document from the Ruby data structure _obj_ and return
173
- # it. _state_ is * a JSON::State object,
174
- # * or a Hash like object (responding to to_hash),
175
- # * an object convertible into a hash by a to_h method,
176
- # that is used as or to configure a State object.
177
- #
178
- # It defaults to a state object, that creates the shortest possible JSON text
179
- # in one line, checks for circular data structures and doesn't allow NaN,
180
- # Infinity, and -Infinity.
181
- #
182
- # A _state_ hash can have the following keys:
183
- # * *indent*: a string used to indent levels (default: ''),
184
- # * *space*: a string that is put after, a : or , delimiter (default: ''),
185
- # * *space_before*: a string that is put before a : pair delimiter (default: ''),
186
- # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
187
- # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
188
- # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
189
- # generated, otherwise an exception is thrown, if these values are
190
- # encountered. This options defaults to false.
191
- # * *max_nesting*: The maximum depth of nesting allowed in the data
192
- # structures from which JSON is to be generated. Disable depth checking
193
- # with :max_nesting => false, it defaults to 19.
194
- #
195
- # See also the fast_generate for the fastest creation method with the least
196
- # amount of sanity checks, and the pretty_generate method for some
197
- # defaults for a pretty output.
198
- def generate(obj, opts = nil)
199
- if opts
200
- if opts.respond_to? :to_hash
201
- opts = opts.to_hash
202
- elsif opts.respond_to? :to_h
203
- opts = opts.to_h
204
- else
205
- raise TypeError, "can't convert #{opts.class} into Hash"
206
- end
207
- state = SAFE_STATE_PROTOTYPE.dup
208
- state = state.configure(opts)
209
- else
210
- state = SAFE_STATE_PROTOTYPE
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
- if opts
229
- if opts.respond_to? :to_hash
230
- opts = opts.to_hash
231
- elsif opts.respond_to? :to_h
232
- opts = opts.to_h
233
- else
234
- raise TypeError, "can't convert #{opts.class} into Hash"
235
- end
236
- state = FAST_STATE_PROTOTYPE.dup
237
- state.configure(opts)
238
- else
239
- state = FAST_STATE_PROTOTYPE
240
- end
241
- state.generate(obj)
242
- end
243
-
244
- # :stopdoc:
245
- # I want to deprecate these later, so I'll first be silent about them, and later delete them.
246
- alias fast_unparse fast_generate
247
- module_function :fast_unparse
248
- # :startdoc:
249
-
250
- # Generate a JSON document from the Ruby data structure _obj_ and return it.
251
- # The returned document is a prettier form of the document returned by
252
- # #unparse.
253
- #
254
- # The _opts_ argument can be used to configure the generator, see the
255
- # generate method for a more detailed explanation.
256
- def pretty_generate(obj, opts = nil)
257
- if opts
258
- if opts.respond_to? :to_hash
259
- opts = opts.to_hash
260
- elsif opts.respond_to? :to_h
261
- opts = opts.to_h
262
- else
263
- raise TypeError, "can't convert #{opts.class} into Hash"
264
- end
265
- state = PRETTY_STATE_PROTOTYPE.dup
266
- state.configure(opts)
267
- else
268
- state = PRETTY_STATE_PROTOTYPE
269
- end
270
- state.generate(obj)
271
- end
272
-
273
- # :stopdoc:
274
- # I want to deprecate these later, so I'll first be silent about them, and later delete them.
275
- alias pretty_unparse pretty_generate
276
- module_function :pretty_unparse
277
- # :startdoc:
278
-
279
- # Load a ruby data structure from a JSON _source_ and return it. A source can
280
- # either be a string-like object, an IO like object, or an object responding
281
- # to the read method. If _proc_ was given, it will be called with any nested
282
- # Ruby object as an argument recursively in depth first order.
283
- #
284
- # This method is part of the implementation of the load/dump interface of
285
- # Marshal and YAML.
286
- def load(source, proc = nil)
287
- if source.respond_to? :to_str
288
- source = source.to_str
289
- elsif source.respond_to? :to_io
290
- source = source.to_io.read
291
- else
292
- source = source.read
293
- end
294
- result = parse(source, :max_nesting => false, :allow_nan => true)
295
- recurse_proc(result, &proc) if proc
296
- result
297
- end
298
-
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
- # Shortuct for iconv.
348
- def self.iconv(to, from, string)
349
- Iconv.iconv(to, from, string).first
350
- end
351
- end
352
-
353
- module ::Kernel
354
- private
355
-
356
- # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
357
- # one line.
358
- def j(*objs)
359
- objs.each do |obj|
360
- puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
361
- end
362
- nil
363
- end
364
-
365
- # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
366
- # indentation and over many lines.
367
- def jj(*objs)
368
- objs.each do |obj|
369
- puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
370
- end
371
- nil
372
- end
373
-
374
- # If _object_ is string-like parse the string and return the parsed result as
375
- # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
376
- # structure object and return it.
377
- #
378
- # The _opts_ argument is passed through to generate/parse respectively, see
379
- # generate and parse for their documentation.
380
- def JSON(object, opts = {})
381
- if object.respond_to? :to_str
382
- JSON.parse(object.to_str, opts)
383
- else
384
- JSON.generate(object, opts)
385
- end
386
- end
387
- end
388
-
389
- class ::Class
390
- # Returns true, if this class can be used to create an instance
391
- # from a serialised JSON string. The class has to implement a class
392
- # method _json_create_ that expects a hash as first parameter, which includes
393
- # the required data.
394
- def json_creatable?
395
- respond_to?(:json_create)
396
- end
397
- end