edward-crazy_ivan 0.2.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.
Files changed (133) hide show
  1. data/.gitignore +7 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +94 -0
  4. data/Rakefile +63 -0
  5. data/TODO +33 -0
  6. data/VERSION +1 -0
  7. data/bin/crazy_ivan +114 -0
  8. data/crazy_ivan.gemspec +182 -0
  9. data/lib/crazy_ivan.rb +5 -0
  10. data/lib/html_asset_crush.rb +56 -0
  11. data/lib/report_assembler.rb +78 -0
  12. data/lib/test_runner.rb +71 -0
  13. data/templates/css/ci.css +11 -0
  14. data/templates/index.html +105 -0
  15. data/templates/javascript/json-template.js +544 -0
  16. data/templates/javascript/prototype.js +4917 -0
  17. data/test/crazy_ivan_test.rb +4 -0
  18. data/test/test_helper.rb +9 -0
  19. data/vendor/json-1.1.7/CHANGES +119 -0
  20. data/vendor/json-1.1.7/GPL +340 -0
  21. data/vendor/json-1.1.7/README +78 -0
  22. data/vendor/json-1.1.7/RUBY +58 -0
  23. data/vendor/json-1.1.7/Rakefile +270 -0
  24. data/vendor/json-1.1.7/TODO +1 -0
  25. data/vendor/json-1.1.7/VERSION +1 -0
  26. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  27. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  28. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  29. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  30. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  31. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  32. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  33. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  34. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  35. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  36. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  37. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  38. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  39. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  40. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  41. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  42. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  43. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  44. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  45. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  46. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  47. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  48. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  49. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  50. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  51. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  52. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  53. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  54. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  55. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  56. data/vendor/json-1.1.7/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  57. data/vendor/json-1.1.7/benchmarks/generator_benchmark.rb +165 -0
  58. data/vendor/json-1.1.7/benchmarks/parser_benchmark.rb +197 -0
  59. data/vendor/json-1.1.7/bin/edit_json.rb +9 -0
  60. data/vendor/json-1.1.7/bin/prettify_json.rb +75 -0
  61. data/vendor/json-1.1.7/data/example.json +1 -0
  62. data/vendor/json-1.1.7/data/index.html +38 -0
  63. data/vendor/json-1.1.7/data/prototype.js +4184 -0
  64. data/vendor/json-1.1.7/doc-templates/main.txt +283 -0
  65. data/vendor/json-1.1.7/ext/json/ext/generator/extconf.rb +11 -0
  66. data/vendor/json-1.1.7/ext/json/ext/generator/generator.c +919 -0
  67. data/vendor/json-1.1.7/ext/json/ext/generator/unicode.c +182 -0
  68. data/vendor/json-1.1.7/ext/json/ext/generator/unicode.h +53 -0
  69. data/vendor/json-1.1.7/ext/json/ext/parser/extconf.rb +11 -0
  70. data/vendor/json-1.1.7/ext/json/ext/parser/parser.c +1829 -0
  71. data/vendor/json-1.1.7/ext/json/ext/parser/parser.rl +686 -0
  72. data/vendor/json-1.1.7/ext/json/ext/parser/unicode.c +154 -0
  73. data/vendor/json-1.1.7/ext/json/ext/parser/unicode.h +58 -0
  74. data/vendor/json-1.1.7/install.rb +26 -0
  75. data/vendor/json-1.1.7/lib/json/Array.xpm +21 -0
  76. data/vendor/json-1.1.7/lib/json/FalseClass.xpm +21 -0
  77. data/vendor/json-1.1.7/lib/json/Hash.xpm +21 -0
  78. data/vendor/json-1.1.7/lib/json/Key.xpm +73 -0
  79. data/vendor/json-1.1.7/lib/json/NilClass.xpm +21 -0
  80. data/vendor/json-1.1.7/lib/json/Numeric.xpm +28 -0
  81. data/vendor/json-1.1.7/lib/json/String.xpm +96 -0
  82. data/vendor/json-1.1.7/lib/json/TrueClass.xpm +21 -0
  83. data/vendor/json-1.1.7/lib/json/add/core.rb +135 -0
  84. data/vendor/json-1.1.7/lib/json/add/rails.rb +58 -0
  85. data/vendor/json-1.1.7/lib/json/common.rb +354 -0
  86. data/vendor/json-1.1.7/lib/json/editor.rb +1371 -0
  87. data/vendor/json-1.1.7/lib/json/ext.rb +15 -0
  88. data/vendor/json-1.1.7/lib/json/json.xpm +1499 -0
  89. data/vendor/json-1.1.7/lib/json/pure/generator.rb +430 -0
  90. data/vendor/json-1.1.7/lib/json/pure/parser.rb +269 -0
  91. data/vendor/json-1.1.7/lib/json/pure.rb +77 -0
  92. data/vendor/json-1.1.7/lib/json/version.rb +8 -0
  93. data/vendor/json-1.1.7/lib/json.rb +10 -0
  94. data/vendor/json-1.1.7/tests/fixtures/fail1.json +1 -0
  95. data/vendor/json-1.1.7/tests/fixtures/fail10.json +1 -0
  96. data/vendor/json-1.1.7/tests/fixtures/fail11.json +1 -0
  97. data/vendor/json-1.1.7/tests/fixtures/fail12.json +1 -0
  98. data/vendor/json-1.1.7/tests/fixtures/fail13.json +1 -0
  99. data/vendor/json-1.1.7/tests/fixtures/fail14.json +1 -0
  100. data/vendor/json-1.1.7/tests/fixtures/fail18.json +1 -0
  101. data/vendor/json-1.1.7/tests/fixtures/fail19.json +1 -0
  102. data/vendor/json-1.1.7/tests/fixtures/fail2.json +1 -0
  103. data/vendor/json-1.1.7/tests/fixtures/fail20.json +1 -0
  104. data/vendor/json-1.1.7/tests/fixtures/fail21.json +1 -0
  105. data/vendor/json-1.1.7/tests/fixtures/fail22.json +1 -0
  106. data/vendor/json-1.1.7/tests/fixtures/fail23.json +1 -0
  107. data/vendor/json-1.1.7/tests/fixtures/fail24.json +1 -0
  108. data/vendor/json-1.1.7/tests/fixtures/fail25.json +1 -0
  109. data/vendor/json-1.1.7/tests/fixtures/fail27.json +2 -0
  110. data/vendor/json-1.1.7/tests/fixtures/fail28.json +2 -0
  111. data/vendor/json-1.1.7/tests/fixtures/fail3.json +1 -0
  112. data/vendor/json-1.1.7/tests/fixtures/fail4.json +1 -0
  113. data/vendor/json-1.1.7/tests/fixtures/fail5.json +1 -0
  114. data/vendor/json-1.1.7/tests/fixtures/fail6.json +1 -0
  115. data/vendor/json-1.1.7/tests/fixtures/fail7.json +1 -0
  116. data/vendor/json-1.1.7/tests/fixtures/fail8.json +1 -0
  117. data/vendor/json-1.1.7/tests/fixtures/fail9.json +1 -0
  118. data/vendor/json-1.1.7/tests/fixtures/pass1.json +56 -0
  119. data/vendor/json-1.1.7/tests/fixtures/pass15.json +1 -0
  120. data/vendor/json-1.1.7/tests/fixtures/pass16.json +1 -0
  121. data/vendor/json-1.1.7/tests/fixtures/pass17.json +1 -0
  122. data/vendor/json-1.1.7/tests/fixtures/pass2.json +1 -0
  123. data/vendor/json-1.1.7/tests/fixtures/pass26.json +1 -0
  124. data/vendor/json-1.1.7/tests/fixtures/pass3.json +6 -0
  125. data/vendor/json-1.1.7/tests/test_json.rb +312 -0
  126. data/vendor/json-1.1.7/tests/test_json_addition.rb +164 -0
  127. data/vendor/json-1.1.7/tests/test_json_fixtures.rb +34 -0
  128. data/vendor/json-1.1.7/tests/test_json_generate.rb +106 -0
  129. data/vendor/json-1.1.7/tests/test_json_rails.rb +146 -0
  130. data/vendor/json-1.1.7/tests/test_json_unicode.rb +62 -0
  131. data/vendor/json-1.1.7/tools/fuzz.rb +139 -0
  132. data/vendor/json-1.1.7/tools/server.rb +61 -0
  133. metadata +186 -0
@@ -0,0 +1,430 @@
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
+ '/' => '\/',
38
+ } # :nodoc:
39
+
40
+ # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
41
+ # UTF16 big endian characters as \u????, and return it.
42
+ if String.method_defined?(:force_encoding)
43
+ def utf8_to_json(string) # :nodoc:
44
+ string = string.dup
45
+ string << '' # XXX workaround: avoid buffer sharing
46
+ string.force_encoding(Encoding::ASCII_8BIT)
47
+ string.gsub!(/["\\\/\x0-\x1f]/) { MAP[$&] }
48
+ string.gsub!(/(
49
+ (?:
50
+ [\xc2-\xdf][\x80-\xbf] |
51
+ [\xe0-\xef][\x80-\xbf]{2} |
52
+ [\xf0-\xf4][\x80-\xbf]{3}
53
+ )+ |
54
+ [\x80-\xc1\xf5-\xff] # invalid
55
+ )/nx) { |c|
56
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
57
+ s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
58
+ s.gsub!(/.{4}/n, '\\\\u\&')
59
+ }
60
+ string.force_encoding(Encoding::UTF_8)
61
+ string
62
+ rescue Iconv::Failure => e
63
+ raise GeneratorError, "Caught #{e.class}: #{e}"
64
+ end
65
+ else
66
+ def utf8_to_json(string) # :nodoc:
67
+ string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] }
68
+ string.gsub!(/(
69
+ (?:
70
+ [\xc2-\xdf][\x80-\xbf] |
71
+ [\xe0-\xef][\x80-\xbf]{2} |
72
+ [\xf0-\xf4][\x80-\xbf]{3}
73
+ )+ |
74
+ [\x80-\xc1\xf5-\xff] # invalid
75
+ )/nx) { |c|
76
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
77
+ s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
78
+ s.gsub!(/.{4}/n, '\\\\u\&')
79
+ }
80
+ string
81
+ rescue Iconv::Failure => e
82
+ raise GeneratorError, "Caught #{e.class}: #{e}"
83
+ end
84
+ end
85
+ module_function :utf8_to_json
86
+
87
+ module Pure
88
+ module Generator
89
+ # This class is used to create State instances, that are use to hold data
90
+ # while generating a JSON text from a a Ruby data structure.
91
+ class State
92
+ # Creates a State object from _opts_, which ought to be Hash to create
93
+ # a new State instance configured by _opts_, something else to create
94
+ # an unconfigured instance. If _opts_ is a State object, it is just
95
+ # returned.
96
+ def self.from_state(opts)
97
+ case opts
98
+ when self
99
+ opts
100
+ when Hash
101
+ new(opts)
102
+ else
103
+ new
104
+ end
105
+ end
106
+
107
+ # Instantiates a new State object, configured by _opts_.
108
+ #
109
+ # _opts_ can have the following keys:
110
+ #
111
+ # * *indent*: a string used to indent levels (default: ''),
112
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
113
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
114
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
115
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
116
+ # * *check_circular*: true if checking for circular data structures
117
+ # should be done (the default), false otherwise.
118
+ # * *check_circular*: true if checking for circular data structures
119
+ # should be done, false (the default) otherwise.
120
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
121
+ # generated, otherwise an exception is thrown, if these values are
122
+ # encountered. This options defaults to false.
123
+ def initialize(opts = {})
124
+ @seen = {}
125
+ @indent = ''
126
+ @space = ''
127
+ @space_before = ''
128
+ @object_nl = ''
129
+ @array_nl = ''
130
+ @check_circular = true
131
+ @allow_nan = false
132
+ configure opts
133
+ end
134
+
135
+ # This string is used to indent levels in the JSON text.
136
+ attr_accessor :indent
137
+
138
+ # This string is used to insert a space between the tokens in a JSON
139
+ # string.
140
+ attr_accessor :space
141
+
142
+ # This string is used to insert a space before the ':' in JSON objects.
143
+ attr_accessor :space_before
144
+
145
+ # This string is put at the end of a line that holds a JSON object (or
146
+ # Hash).
147
+ attr_accessor :object_nl
148
+
149
+ # This string is put at the end of a line that holds a JSON array.
150
+ attr_accessor :array_nl
151
+
152
+ # This integer returns the maximum level of data structure nesting in
153
+ # the generated JSON, max_nesting = 0 if no maximum is checked.
154
+ attr_accessor :max_nesting
155
+
156
+ def check_max_nesting(depth) # :nodoc:
157
+ return if @max_nesting.zero?
158
+ current_nesting = depth + 1
159
+ current_nesting > @max_nesting and
160
+ raise NestingError, "nesting of #{current_nesting} is too deep"
161
+ end
162
+
163
+ # Returns true, if circular data structures should be checked,
164
+ # otherwise returns false.
165
+ def check_circular?
166
+ @check_circular
167
+ end
168
+
169
+ # Returns true if NaN, Infinity, and -Infinity should be considered as
170
+ # valid JSON and output.
171
+ def allow_nan?
172
+ @allow_nan
173
+ end
174
+
175
+ # Returns _true_, if _object_ was already seen during this generating
176
+ # run.
177
+ def seen?(object)
178
+ @seen.key?(object.__id__)
179
+ end
180
+
181
+ # Remember _object_, to find out if it was already encountered (if a
182
+ # cyclic data structure is if a cyclic data structure is rendered).
183
+ def remember(object)
184
+ @seen[object.__id__] = true
185
+ end
186
+
187
+ # Forget _object_ for this generating run.
188
+ def forget(object)
189
+ @seen.delete object.__id__
190
+ end
191
+
192
+ # Configure this State instance with the Hash _opts_, and return
193
+ # itself.
194
+ def configure(opts)
195
+ @indent = opts[:indent] if opts.key?(:indent)
196
+ @space = opts[:space] if opts.key?(:space)
197
+ @space_before = opts[:space_before] if opts.key?(:space_before)
198
+ @object_nl = opts[:object_nl] if opts.key?(:object_nl)
199
+ @array_nl = opts[:array_nl] if opts.key?(:array_nl)
200
+ @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
201
+ @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
202
+ if !opts.key?(:max_nesting) # defaults to 19
203
+ @max_nesting = 19
204
+ elsif opts[:max_nesting]
205
+ @max_nesting = opts[:max_nesting]
206
+ else
207
+ @max_nesting = 0
208
+ end
209
+ self
210
+ end
211
+
212
+ # Returns the configuration instance variables as a hash, that can be
213
+ # passed to the configure method.
214
+ def to_h
215
+ result = {}
216
+ for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting]
217
+ result[iv.intern] = instance_variable_get("@#{iv}")
218
+ end
219
+ result
220
+ end
221
+ end
222
+
223
+ module GeneratorMethods
224
+ module Object
225
+ # Converts this object to a string (calling #to_s), converts
226
+ # it to a JSON string, and returns the result. This is a fallback, if no
227
+ # special method #to_json was defined for some object.
228
+ def to_json(*) to_s.to_json end
229
+ end
230
+
231
+ module Hash
232
+ # Returns a JSON string containing a JSON object, that is unparsed from
233
+ # this Hash instance.
234
+ # _state_ is a JSON::State object, that can also be used to configure the
235
+ # produced JSON string output further.
236
+ # _depth_ is used to find out nesting depth, to indent accordingly.
237
+ def to_json(state = nil, depth = 0, *)
238
+ if state
239
+ state = JSON.state.from_state(state)
240
+ state.check_max_nesting(depth)
241
+ json_check_circular(state) { json_transform(state, depth) }
242
+ else
243
+ json_transform(state, depth)
244
+ end
245
+ end
246
+
247
+ private
248
+
249
+ def json_check_circular(state)
250
+ if state and state.check_circular?
251
+ state.seen?(self) and raise JSON::CircularDatastructure,
252
+ "circular data structures not supported!"
253
+ state.remember self
254
+ end
255
+ yield
256
+ ensure
257
+ state and state.forget self
258
+ end
259
+
260
+ def json_shift(state, depth)
261
+ state and not state.object_nl.empty? or return ''
262
+ state.indent * depth
263
+ end
264
+
265
+ def json_transform(state, depth)
266
+ delim = ','
267
+ if state
268
+ delim << state.object_nl
269
+ result = '{'
270
+ result << state.object_nl
271
+ result << map { |key,value|
272
+ s = json_shift(state, depth + 1)
273
+ s << key.to_s.to_json(state, depth + 1)
274
+ s << state.space_before
275
+ s << ':'
276
+ s << state.space
277
+ s << value.to_json(state, depth + 1)
278
+ }.join(delim)
279
+ result << state.object_nl
280
+ result << json_shift(state, depth)
281
+ result << '}'
282
+ else
283
+ result = '{'
284
+ result << map { |key,value|
285
+ key.to_s.to_json << ':' << value.to_json
286
+ }.join(delim)
287
+ result << '}'
288
+ end
289
+ result
290
+ end
291
+ end
292
+
293
+ module Array
294
+ # Returns a JSON string containing a JSON array, that is unparsed from
295
+ # this Array instance.
296
+ # _state_ is a JSON::State object, that can also be used to configure the
297
+ # produced JSON string output further.
298
+ # _depth_ is used to find out nesting depth, to indent accordingly.
299
+ def to_json(state = nil, depth = 0, *)
300
+ if state
301
+ state = JSON.state.from_state(state)
302
+ state.check_max_nesting(depth)
303
+ json_check_circular(state) { json_transform(state, depth) }
304
+ else
305
+ json_transform(state, depth)
306
+ end
307
+ end
308
+
309
+ private
310
+
311
+ def json_check_circular(state)
312
+ if state and state.check_circular?
313
+ state.seen?(self) and raise JSON::CircularDatastructure,
314
+ "circular data structures not supported!"
315
+ state.remember self
316
+ end
317
+ yield
318
+ ensure
319
+ state and state.forget self
320
+ end
321
+
322
+ def json_shift(state, depth)
323
+ state and not state.array_nl.empty? or return ''
324
+ state.indent * depth
325
+ end
326
+
327
+ def json_transform(state, depth)
328
+ delim = ','
329
+ if state
330
+ delim << state.array_nl
331
+ result = '['
332
+ result << state.array_nl
333
+ result << map { |value|
334
+ json_shift(state, depth + 1) << value.to_json(state, depth + 1)
335
+ }.join(delim)
336
+ result << state.array_nl
337
+ result << json_shift(state, depth)
338
+ result << ']'
339
+ else
340
+ '[' << map { |value| value.to_json }.join(delim) << ']'
341
+ end
342
+ end
343
+ end
344
+
345
+ module Integer
346
+ # Returns a JSON string representation for this Integer number.
347
+ def to_json(*) to_s end
348
+ end
349
+
350
+ module Float
351
+ # Returns a JSON string representation for this Float number.
352
+ def to_json(state = nil, *)
353
+ case
354
+ when infinite?
355
+ if !state || state.allow_nan?
356
+ to_s
357
+ else
358
+ raise GeneratorError, "#{self} not allowed in JSON"
359
+ end
360
+ when nan?
361
+ if !state || state.allow_nan?
362
+ to_s
363
+ else
364
+ raise GeneratorError, "#{self} not allowed in JSON"
365
+ end
366
+ else
367
+ to_s
368
+ end
369
+ end
370
+ end
371
+
372
+ module String
373
+ # This string should be encoded with UTF-8 A call to this method
374
+ # returns a JSON string encoded with UTF16 big endian characters as
375
+ # \u????.
376
+ def to_json(*)
377
+ '"' << JSON.utf8_to_json(self) << '"'
378
+ end
379
+
380
+ # Module that holds the extinding methods if, the String module is
381
+ # included.
382
+ module Extend
383
+ # Raw Strings are JSON Objects (the raw bytes are stored in an array for the
384
+ # key "raw"). The Ruby String can be created by this module method.
385
+ def json_create(o)
386
+ o['raw'].pack('C*')
387
+ end
388
+ end
389
+
390
+ # Extends _modul_ with the String::Extend module.
391
+ def self.included(modul)
392
+ modul.extend Extend
393
+ end
394
+
395
+ # This method creates a raw object hash, that can be nested into
396
+ # other data structures and will be unparsed as a raw string. This
397
+ # method should be used, if you want to convert raw strings to JSON
398
+ # instead of UTF-8 strings, e. g. binary data.
399
+ def to_json_raw_object
400
+ {
401
+ JSON.create_id => self.class.name,
402
+ 'raw' => self.unpack('C*'),
403
+ }
404
+ end
405
+
406
+ # This method creates a JSON text from the result of
407
+ # a call to to_json_raw_object of this String.
408
+ def to_json_raw(*args)
409
+ to_json_raw_object.to_json(*args)
410
+ end
411
+ end
412
+
413
+ module TrueClass
414
+ # Returns a JSON string for true: 'true'.
415
+ def to_json(*) 'true' end
416
+ end
417
+
418
+ module FalseClass
419
+ # Returns a JSON string for false: 'false'.
420
+ def to_json(*) 'false' end
421
+ end
422
+
423
+ module NilClass
424
+ # Returns a JSON string for nil: 'null'.
425
+ def to_json(*) 'null' end
426
+ end
427
+ end
428
+ end
429
+ end
430
+ end
@@ -0,0 +1,269 @@
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
+ # * *create_additions*: If set to false, the Parser doesn't create
64
+ # additions even if a matchin class and create_id was found. This option
65
+ # defaults to true.
66
+ # * *object_class*: Defaults to Hash
67
+ # * *array_class*: Defaults to Array
68
+ def initialize(source, opts = {})
69
+ super
70
+ if !opts.key?(:max_nesting) # defaults to 19
71
+ @max_nesting = 19
72
+ elsif opts[:max_nesting]
73
+ @max_nesting = opts[:max_nesting]
74
+ else
75
+ @max_nesting = 0
76
+ end
77
+ @allow_nan = !!opts[:allow_nan]
78
+ ca = true
79
+ ca = opts[:create_additions] if opts.key?(:create_additions)
80
+ @create_id = ca ? JSON.create_id : nil
81
+ @object_class = opts[:object_class] || Hash
82
+ @array_class = opts[:array_class] || Array
83
+ end
84
+
85
+ alias source string
86
+
87
+ # Parses the current JSON string _source_ and returns the complete data
88
+ # structure as a result.
89
+ def parse
90
+ reset
91
+ obj = nil
92
+ until eos?
93
+ case
94
+ when scan(OBJECT_OPEN)
95
+ obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
96
+ @current_nesting = 1
97
+ obj = parse_object
98
+ when scan(ARRAY_OPEN)
99
+ obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
100
+ @current_nesting = 1
101
+ obj = parse_array
102
+ when skip(IGNORE)
103
+ ;
104
+ else
105
+ raise ParserError, "source '#{peek(20)}' not in JSON!"
106
+ end
107
+ end
108
+ obj or raise ParserError, "source did not contain any JSON!"
109
+ obj
110
+ end
111
+
112
+ private
113
+
114
+ # Unescape characters in strings.
115
+ UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
116
+ UNESCAPE_MAP.update({
117
+ ?" => '"',
118
+ ?\\ => '\\',
119
+ ?/ => '/',
120
+ ?b => "\b",
121
+ ?f => "\f",
122
+ ?n => "\n",
123
+ ?r => "\r",
124
+ ?t => "\t",
125
+ ?u => nil,
126
+ })
127
+
128
+ def parse_string
129
+ if scan(STRING)
130
+ return '' if self[1].empty?
131
+ string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
132
+ if u = UNESCAPE_MAP[$&[1]]
133
+ u
134
+ else # \uXXXX
135
+ bytes = ''
136
+ i = 0
137
+ while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
138
+ bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
139
+ i += 1
140
+ end
141
+ JSON::UTF16toUTF8.iconv(bytes)
142
+ end
143
+ end
144
+ if string.respond_to?(:force_encoding)
145
+ string.force_encoding(Encoding::UTF_8)
146
+ end
147
+ string
148
+ else
149
+ UNPARSED
150
+ end
151
+ rescue Iconv::Failure => e
152
+ raise GeneratorError, "Caught #{e.class}: #{e}"
153
+ end
154
+
155
+ def parse_value
156
+ case
157
+ when scan(FLOAT)
158
+ Float(self[1])
159
+ when scan(INTEGER)
160
+ Integer(self[1])
161
+ when scan(TRUE)
162
+ true
163
+ when scan(FALSE)
164
+ false
165
+ when scan(NULL)
166
+ nil
167
+ when (string = parse_string) != UNPARSED
168
+ string
169
+ when scan(ARRAY_OPEN)
170
+ @current_nesting += 1
171
+ ary = parse_array
172
+ @current_nesting -= 1
173
+ ary
174
+ when scan(OBJECT_OPEN)
175
+ @current_nesting += 1
176
+ obj = parse_object
177
+ @current_nesting -= 1
178
+ obj
179
+ when @allow_nan && scan(NAN)
180
+ NaN
181
+ when @allow_nan && scan(INFINITY)
182
+ Infinity
183
+ when @allow_nan && scan(MINUS_INFINITY)
184
+ MinusInfinity
185
+ else
186
+ UNPARSED
187
+ end
188
+ end
189
+
190
+ def parse_array
191
+ raise NestingError, "nesting of #@current_nesting is to deep" if
192
+ @max_nesting.nonzero? && @current_nesting > @max_nesting
193
+ result = @array_class.new
194
+ delim = false
195
+ until eos?
196
+ case
197
+ when (value = parse_value) != UNPARSED
198
+ delim = false
199
+ result << value
200
+ skip(IGNORE)
201
+ if scan(COLLECTION_DELIMITER)
202
+ delim = true
203
+ elsif match?(ARRAY_CLOSE)
204
+ ;
205
+ else
206
+ raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!"
207
+ end
208
+ when scan(ARRAY_CLOSE)
209
+ if delim
210
+ raise ParserError, "expected next element in array at '#{peek(20)}'!"
211
+ end
212
+ break
213
+ when skip(IGNORE)
214
+ ;
215
+ else
216
+ raise ParserError, "unexpected token in array at '#{peek(20)}'!"
217
+ end
218
+ end
219
+ result
220
+ end
221
+
222
+ def parse_object
223
+ raise NestingError, "nesting of #@current_nesting is to deep" if
224
+ @max_nesting.nonzero? && @current_nesting > @max_nesting
225
+ result = @object_class.new
226
+ delim = false
227
+ until eos?
228
+ case
229
+ when (string = parse_string) != UNPARSED
230
+ skip(IGNORE)
231
+ unless scan(PAIR_DELIMITER)
232
+ raise ParserError, "expected ':' in object at '#{peek(20)}'!"
233
+ end
234
+ skip(IGNORE)
235
+ unless (value = parse_value).equal? UNPARSED
236
+ result[string] = value
237
+ delim = false
238
+ skip(IGNORE)
239
+ if scan(COLLECTION_DELIMITER)
240
+ delim = true
241
+ elsif match?(OBJECT_CLOSE)
242
+ ;
243
+ else
244
+ raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!"
245
+ end
246
+ else
247
+ raise ParserError, "expected value in object at '#{peek(20)}'!"
248
+ end
249
+ when scan(OBJECT_CLOSE)
250
+ if delim
251
+ raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
252
+ end
253
+ if @create_id and klassname = result[@create_id]
254
+ klass = JSON.deep_const_get klassname
255
+ break unless klass and klass.json_creatable?
256
+ result = klass.json_create(result)
257
+ end
258
+ break
259
+ when skip(IGNORE)
260
+ ;
261
+ else
262
+ raise ParserError, "unexpected token in object at '#{peek(20)}'!"
263
+ end
264
+ end
265
+ result
266
+ end
267
+ end
268
+ end
269
+ end