scout 5.6.7 → 5.6.8.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/CHANGELOG.markdown +4 -0
  2. data/lib/scout/version.rb +1 -1
  3. data/vendor/json_pure/.gitignore +12 -0
  4. data/vendor/json_pure/.travis.yml +20 -0
  5. data/vendor/json_pure/CHANGES +120 -0
  6. data/vendor/json_pure/COPYING-json-jruby +57 -0
  7. data/vendor/json_pure/Gemfile +11 -0
  8. data/vendor/json_pure/README-json-jruby.markdown +33 -0
  9. data/vendor/json_pure/{README → README.rdoc} +7 -7
  10. data/vendor/json_pure/Rakefile +305 -185
  11. data/vendor/json_pure/VERSION +1 -1
  12. data/vendor/json_pure/diagrams/.keep +0 -0
  13. data/vendor/json_pure/ext/json/ext/fbuffer/fbuffer.h +181 -0
  14. data/vendor/json_pure/ext/json/ext/generator/depend +1 -0
  15. data/vendor/json_pure/ext/json/ext/generator/extconf.rb +4 -6
  16. data/vendor/json_pure/ext/json/ext/generator/generator.c +463 -369
  17. data/vendor/json_pure/ext/json/ext/generator/generator.h +44 -66
  18. data/vendor/json_pure/ext/json/ext/parser/depend +1 -0
  19. data/vendor/json_pure/ext/json/ext/parser/extconf.rb +3 -5
  20. data/vendor/json_pure/ext/json/ext/parser/parser.c +580 -311
  21. data/vendor/json_pure/ext/json/ext/parser/parser.h +14 -8
  22. data/vendor/json_pure/ext/json/ext/parser/parser.rl +242 -107
  23. data/vendor/json_pure/install.rb +8 -11
  24. data/vendor/json_pure/java/src/json/ext/ByteListTranscoder.java +167 -0
  25. data/vendor/json_pure/java/src/json/ext/Generator.java +444 -0
  26. data/vendor/json_pure/java/src/json/ext/GeneratorMethods.java +232 -0
  27. data/vendor/json_pure/java/src/json/ext/GeneratorService.java +43 -0
  28. data/vendor/json_pure/java/src/json/ext/GeneratorState.java +543 -0
  29. data/vendor/json_pure/java/src/json/ext/OptionsReader.java +114 -0
  30. data/vendor/json_pure/java/src/json/ext/Parser.java +2644 -0
  31. data/vendor/json_pure/java/src/json/ext/Parser.rl +968 -0
  32. data/vendor/json_pure/java/src/json/ext/ParserService.java +35 -0
  33. data/vendor/json_pure/java/src/json/ext/RuntimeInfo.java +121 -0
  34. data/vendor/json_pure/java/src/json/ext/StringDecoder.java +167 -0
  35. data/vendor/json_pure/java/src/json/ext/StringEncoder.java +106 -0
  36. data/vendor/json_pure/java/src/json/ext/Utils.java +89 -0
  37. data/vendor/json_pure/json-java.gemspec +23 -0
  38. data/vendor/json_pure/json.gemspec +37 -0
  39. data/vendor/json_pure/json_pure.gemspec +39 -0
  40. data/vendor/json_pure/lib/json.rb +52 -0
  41. data/vendor/json_pure/lib/json/add/bigdecimal.rb +28 -0
  42. data/vendor/json_pure/lib/json/add/complex.rb +22 -0
  43. data/vendor/json_pure/lib/json/add/core.rb +9 -146
  44. data/vendor/json_pure/lib/json/add/date.rb +34 -0
  45. data/vendor/json_pure/lib/json/add/date_time.rb +50 -0
  46. data/vendor/json_pure/lib/json/add/exception.rb +31 -0
  47. data/vendor/json_pure/lib/json/add/ostruct.rb +31 -0
  48. data/vendor/json_pure/lib/json/add/range.rb +29 -0
  49. data/vendor/json_pure/lib/json/add/rational.rb +22 -0
  50. data/vendor/json_pure/lib/json/add/regexp.rb +30 -0
  51. data/vendor/json_pure/lib/json/add/struct.rb +30 -0
  52. data/vendor/json_pure/lib/json/add/symbol.rb +25 -0
  53. data/vendor/json_pure/lib/json/add/time.rb +38 -0
  54. data/vendor/json_pure/lib/json/common.rb +157 -67
  55. data/vendor/json_pure/lib/json/ext.rb +8 -2
  56. data/vendor/json_pure/lib/json/ext/.keep +0 -0
  57. data/vendor/json_pure/lib/json/generic_object.rb +70 -0
  58. data/vendor/json_pure/lib/json/pure.rb +8 -64
  59. data/vendor/json_pure/lib/json/pure/generator.rb +183 -113
  60. data/vendor/json_pure/lib/json/pure/parser.rb +118 -66
  61. data/vendor/json_pure/lib/json/version.rb +1 -1
  62. data/vendor/json_pure/tests/fixtures/fail18.json +1 -1
  63. data/vendor/json_pure/tests/setup_variant.rb +11 -0
  64. data/vendor/json_pure/tests/test_json.rb +233 -28
  65. data/vendor/json_pure/tests/test_json_addition.rb +68 -34
  66. data/vendor/json_pure/tests/test_json_encoding.rb +11 -14
  67. data/vendor/json_pure/tests/test_json_fixtures.rb +11 -10
  68. data/vendor/json_pure/tests/test_json_generate.rb +207 -7
  69. data/vendor/json_pure/tests/test_json_generic_object.rb +75 -0
  70. data/vendor/json_pure/tests/test_json_string_matching.rb +39 -0
  71. data/vendor/json_pure/tests/test_json_unicode.rb +3 -7
  72. data/vendor/json_pure/tools/fuzz.rb +1 -1
  73. data/vendor/json_pure/tools/server.rb +1 -0
  74. metadata +87 -94
  75. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +0 -52
  76. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +0 -1000
  77. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +0 -1001
  78. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +0 -900
  79. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +0 -901
  80. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +0 -1000
  81. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +0 -1001
  82. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +0 -261
  83. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +0 -1000
  84. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +0 -1001
  85. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +0 -1000
  86. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +0 -1001
  87. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +0 -1000
  88. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +0 -1001
  89. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +0 -262
  90. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +0 -1000
  91. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +0 -1001
  92. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +0 -82
  93. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +0 -34
  94. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +0 -900
  95. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +0 -901
  96. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +0 -81
  97. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +0 -1000
  98. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +0 -1001
  99. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +0 -82
  100. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +0 -1000
  101. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +0 -1001
  102. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +0 -82
  103. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +0 -1000
  104. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +0 -1001
  105. data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +0 -82
  106. data/vendor/json_pure/benchmarks/generator2_benchmark.rb +0 -222
  107. data/vendor/json_pure/benchmarks/generator_benchmark.rb +0 -224
  108. data/vendor/json_pure/benchmarks/ohai.json +0 -1216
  109. data/vendor/json_pure/benchmarks/ohai.ruby +0 -1
  110. data/vendor/json_pure/benchmarks/parser2_benchmark.rb +0 -251
  111. data/vendor/json_pure/benchmarks/parser_benchmark.rb +0 -259
  112. data/vendor/json_pure/bin/edit_json.rb +0 -9
  113. data/vendor/json_pure/bin/prettify_json.rb +0 -75
  114. data/vendor/json_pure/lib/json/Array.xpm +0 -21
  115. data/vendor/json_pure/lib/json/FalseClass.xpm +0 -21
  116. data/vendor/json_pure/lib/json/Hash.xpm +0 -21
  117. data/vendor/json_pure/lib/json/Key.xpm +0 -73
  118. data/vendor/json_pure/lib/json/NilClass.xpm +0 -21
  119. data/vendor/json_pure/lib/json/Numeric.xpm +0 -28
  120. data/vendor/json_pure/lib/json/String.xpm +0 -96
  121. data/vendor/json_pure/lib/json/TrueClass.xpm +0 -21
  122. data/vendor/json_pure/lib/json/add/rails.rb +0 -58
  123. data/vendor/json_pure/lib/json/editor.rb +0 -1371
  124. data/vendor/json_pure/lib/json/json.xpm +0 -1499
  125. data/vendor/json_pure/tests/test_json_rails.rb +0 -144
@@ -41,7 +41,7 @@ module JSON
41
41
  [^*/]| # normal chars
42
42
  /[^*]| # slashes that do not start a nested comment
43
43
  \*[^/]| # asterisks that do not end this comment
44
- /(?=\*/) # single slash before this comment's end
44
+ /(?=\*/) # single slash before this comment's end
45
45
  )*
46
46
  \*/ # the End of this comment
47
47
  |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr
@@ -56,56 +56,28 @@ module JSON
56
56
  # keys:
57
57
  # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
58
58
  # structures. Disable depth checking with :max_nesting => false|nil|0,
59
- # it defaults to 19.
59
+ # it defaults to 100.
60
60
  # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
61
61
  # defiance of RFC 4627 to be parsed by the Parser. This option defaults
62
62
  # to false.
63
63
  # * *symbolize_names*: If set to true, returns symbols for the names
64
64
  # (keys) in a JSON object. Otherwise strings are returned, which is also
65
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.
66
+ # * *create_additions*: If set to true, the Parser creates
67
+ # additions when if a matching class and create_id was found. This
68
+ # option defaults to false.
69
69
  # * *object_class*: Defaults to Hash
70
70
  # * *array_class*: Defaults to Array
71
+ # * *quirks_mode*: Enables quirks_mode for parser, that is for example
72
+ # parsing single JSON values instead of documents is possible.
71
73
  def initialize(source, opts = {})
72
- if defined?(::Encoding)
73
- if source.encoding == Encoding::ASCII_8BIT
74
- b = source[0, 4].bytes.to_a
75
- source = case
76
- when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
77
- source.dup.force_encoding(Encoding::UTF_32BE).encode!(Encoding::UTF_8)
78
- when b.size >= 4 && b[0] == 0 && b[2] == 0
79
- source.dup.force_encoding(Encoding::UTF_16BE).encode!(Encoding::UTF_8)
80
- when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
81
- source.dup.force_encoding(Encoding::UTF_32LE).encode!(Encoding::UTF_8)
82
- when b.size >= 4 && b[1] == 0 && b[3] == 0
83
- source.dup.force_encoding(Encoding::UTF_16LE).encode!(Encoding::UTF_8)
84
- else
85
- source.dup
86
- end
87
- else
88
- source = source.encode(Encoding::UTF_8)
89
- end
90
- source.force_encoding(Encoding::ASCII_8BIT)
91
- else
92
- b = source
93
- source = case
94
- when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
95
- JSON.iconv('utf-8', 'utf-32be', b)
96
- when b.size >= 4 && b[0] == 0 && b[2] == 0
97
- JSON.iconv('utf-8', 'utf-16be', b)
98
- when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
99
- JSON.iconv('utf-8', 'utf-32le', b)
100
- when b.size >= 4 && b[1] == 0 && b[3] == 0
101
- JSON.iconv('utf-8', 'utf-16le', b)
102
- else
103
- b
104
- end
105
- end
74
+ opts ||= {}
75
+ unless @quirks_mode = opts[:quirks_mode]
76
+ source = convert_encoding source
77
+ end
106
78
  super source
107
- if !opts.key?(:max_nesting) # defaults to 19
108
- @max_nesting = 19
79
+ if !opts.key?(:max_nesting) # defaults to 100
80
+ @max_nesting = 100
109
81
  elsif opts[:max_nesting]
110
82
  @max_nesting = opts[:max_nesting]
111
83
  else
@@ -113,42 +85,111 @@ module JSON
113
85
  end
114
86
  @allow_nan = !!opts[:allow_nan]
115
87
  @symbolize_names = !!opts[:symbolize_names]
116
- ca = true
117
- ca = opts[:create_additions] if opts.key?(:create_additions)
118
- @create_id = ca ? JSON.create_id : nil
88
+ if opts.key?(:create_additions)
89
+ @create_additions = !!opts[:create_additions]
90
+ else
91
+ @create_additions = false
92
+ end
93
+ @create_id = @create_additions ? JSON.create_id : nil
119
94
  @object_class = opts[:object_class] || Hash
120
- @array_class = opts[:array_class] || Array
95
+ @array_class = opts[:array_class] || Array
96
+ @match_string = opts[:match_string]
121
97
  end
122
98
 
123
99
  alias source string
124
100
 
101
+ def quirks_mode?
102
+ !!@quirks_mode
103
+ end
104
+
105
+ def reset
106
+ super
107
+ @current_nesting = 0
108
+ end
109
+
125
110
  # Parses the current JSON string _source_ and returns the complete data
126
111
  # structure as a result.
127
112
  def parse
128
113
  reset
129
114
  obj = nil
130
- until eos?
131
- case
132
- when scan(OBJECT_OPEN)
133
- obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
134
- @current_nesting = 1
135
- obj = parse_object
136
- when scan(ARRAY_OPEN)
137
- obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
138
- @current_nesting = 1
139
- obj = parse_array
140
- when skip(IGNORE)
141
- ;
115
+ if @quirks_mode
116
+ while !eos? && skip(IGNORE)
117
+ end
118
+ if eos?
119
+ raise ParserError, "source did not contain any JSON!"
142
120
  else
143
- raise ParserError, "source '#{peek(20)}' not in JSON!"
121
+ obj = parse_value
122
+ obj == UNPARSED and raise ParserError, "source did not contain any JSON!"
144
123
  end
124
+ else
125
+ until eos?
126
+ case
127
+ when scan(OBJECT_OPEN)
128
+ obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
129
+ @current_nesting = 1
130
+ obj = parse_object
131
+ when scan(ARRAY_OPEN)
132
+ obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
133
+ @current_nesting = 1
134
+ obj = parse_array
135
+ when skip(IGNORE)
136
+ ;
137
+ else
138
+ raise ParserError, "source '#{peek(20)}' not in JSON!"
139
+ end
140
+ end
141
+ obj or raise ParserError, "source did not contain any JSON!"
145
142
  end
146
- obj or raise ParserError, "source did not contain any JSON!"
147
143
  obj
148
144
  end
149
145
 
150
146
  private
151
147
 
148
+ def convert_encoding(source)
149
+ if source.respond_to?(:to_str)
150
+ source = source.to_str
151
+ else
152
+ raise TypeError, "#{source.inspect} is not like a string"
153
+ end
154
+ if defined?(::Encoding)
155
+ if source.encoding == ::Encoding::ASCII_8BIT
156
+ b = source[0, 4].bytes.to_a
157
+ source =
158
+ case
159
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
160
+ source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
161
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
162
+ source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
163
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
164
+ source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
165
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
166
+ source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
167
+ else
168
+ source.dup
169
+ end
170
+ else
171
+ source = source.encode(::Encoding::UTF_8)
172
+ end
173
+ source.force_encoding(::Encoding::ASCII_8BIT)
174
+ else
175
+ b = source
176
+ source =
177
+ case
178
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
179
+ JSON.iconv('utf-8', 'utf-32be', b)
180
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
181
+ JSON.iconv('utf-8', 'utf-16be', b)
182
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
183
+ JSON.iconv('utf-8', 'utf-32le', b)
184
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
185
+ JSON.iconv('utf-8', 'utf-16le', b)
186
+ else
187
+ b
188
+ end
189
+ end
190
+ source
191
+ end
192
+
152
193
  # Unescape characters in strings.
153
194
  UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
154
195
  UNESCAPE_MAP.update({
@@ -160,9 +201,14 @@ module JSON
160
201
  ?n => "\n",
161
202
  ?r => "\r",
162
203
  ?t => "\t",
163
- ?u => nil,
204
+ ?u => nil,
164
205
  })
165
206
 
207
+ EMPTY_8BIT_STRING = ''
208
+ if ::String.method_defined?(:encode)
209
+ EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
210
+ end
211
+
166
212
  def parse_string
167
213
  if scan(STRING)
168
214
  return '' if self[1].empty?
@@ -170,24 +216,30 @@ module JSON
170
216
  if u = UNESCAPE_MAP[$&[1]]
171
217
  u
172
218
  else # \uXXXX
173
- bytes = ''
219
+ bytes = EMPTY_8BIT_STRING.dup
174
220
  i = 0
175
221
  while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
176
222
  bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
177
223
  i += 1
178
224
  end
179
- JSON::UTF16toUTF8.iconv(bytes)
225
+ JSON.iconv('utf-8', 'utf-16be', bytes)
180
226
  end
181
227
  end
182
228
  if string.respond_to?(:force_encoding)
183
- string.force_encoding(Encoding::UTF_8)
229
+ string.force_encoding(::Encoding::UTF_8)
230
+ end
231
+ if @create_additions and @match_string
232
+ for (regexp, klass) in @match_string
233
+ klass.json_creatable? or next
234
+ string =~ regexp and return klass.json_create(string)
235
+ end
184
236
  end
185
237
  string
186
238
  else
187
239
  UNPARSED
188
240
  end
189
- rescue Iconv::Failure => e
190
- raise GeneratorError, "Caught #{e.class}: #{e}"
241
+ rescue => e
242
+ raise ParserError, "Caught #{e.class} at '#{peek(20)}': #{e}"
191
243
  end
192
244
 
193
245
  def parse_value
@@ -288,7 +340,7 @@ module JSON
288
340
  if delim
289
341
  raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
290
342
  end
291
- if @create_id and klassname = result[@create_id]
343
+ if @create_additions and klassname = result[@create_id]
292
344
  klass = JSON.deep_const_get klassname
293
345
  break unless klass and klass.json_creatable?
294
346
  result = klass.json_create(result)
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.4.2'
3
+ VERSION = '1.8.0'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -1 +1 @@
1
- [[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
1
+ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
@@ -0,0 +1,11 @@
1
+ case ENV['JSON']
2
+ when 'pure'
3
+ $:.unshift 'lib'
4
+ require 'json/pure'
5
+ when 'ext'
6
+ $:.unshift 'ext', 'lib'
7
+ require 'json/ext'
8
+ else
9
+ $:.unshift 'ext', 'lib'
10
+ require 'json'
11
+ end
@@ -1,13 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
- # -*- coding: utf-8 -*-
2
+ # encoding: utf-8
3
3
 
4
4
  require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
10
6
  require 'stringio'
7
+ require 'tempfile'
8
+ require 'ostruct'
11
9
 
12
10
  unless Array.method_defined?(:permutation)
13
11
  begin
@@ -23,7 +21,7 @@ unless Array.method_defined?(:permutation)
23
21
  end
24
22
  end
25
23
 
26
- class TC_JSON < Test::Unit::TestCase
24
+ class TestJSON < Test::Unit::TestCase
27
25
  include JSON
28
26
 
29
27
  def setup
@@ -108,6 +106,46 @@ class TC_JSON < Test::Unit::TestCase
108
106
  assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
109
107
  end
110
108
 
109
+ def test_parse_json_primitive_values
110
+ assert_raise(JSON::ParserError) { JSON.parse('') }
111
+ assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) }
112
+ assert_raise(TypeError) { JSON::Parser.new(nil).parse }
113
+ assert_raise(TypeError) { JSON::Parser.new(nil, :quirks_mode => true).parse }
114
+ assert_raise(TypeError) { JSON.parse(nil) }
115
+ assert_raise(TypeError) { JSON.parse(nil, :quirks_mode => true) }
116
+ assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
117
+ assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ', :quirks_mode => true) }
118
+ parser = JSON::Parser.new('null')
119
+ assert_equal false, parser.quirks_mode?
120
+ assert_raise(JSON::ParserError) { parser.parse }
121
+ assert_raise(JSON::ParserError) { JSON.parse('null') }
122
+ assert_equal nil, JSON.parse('null', :quirks_mode => true)
123
+ parser = JSON::Parser.new('null', :quirks_mode => true)
124
+ assert_equal true, parser.quirks_mode?
125
+ assert_equal nil, parser.parse
126
+ assert_raise(JSON::ParserError) { JSON.parse('false') }
127
+ assert_equal false, JSON.parse('false', :quirks_mode => true)
128
+ assert_raise(JSON::ParserError) { JSON.parse('true') }
129
+ assert_equal true, JSON.parse('true', :quirks_mode => true)
130
+ assert_raise(JSON::ParserError) { JSON.parse('23') }
131
+ assert_equal 23, JSON.parse('23', :quirks_mode => true)
132
+ assert_raise(JSON::ParserError) { JSON.parse('1') }
133
+ assert_equal 1, JSON.parse('1', :quirks_mode => true)
134
+ assert_raise(JSON::ParserError) { JSON.parse('3.141') }
135
+ assert_in_delta 3.141, JSON.parse('3.141', :quirks_mode => true), 1E-3
136
+ assert_raise(JSON::ParserError) { JSON.parse('18446744073709551616') }
137
+ assert_equal 2 ** 64, JSON.parse('18446744073709551616', :quirks_mode => true)
138
+ assert_raise(JSON::ParserError) { JSON.parse('"foo"') }
139
+ assert_equal 'foo', JSON.parse('"foo"', :quirks_mode => true)
140
+ assert_raise(JSON::ParserError) { JSON.parse('NaN', :allow_nan => true) }
141
+ assert JSON.parse('NaN', :quirks_mode => true, :allow_nan => true).nan?
142
+ assert_raise(JSON::ParserError) { JSON.parse('Infinity', :allow_nan => true) }
143
+ assert JSON.parse('Infinity', :quirks_mode => true, :allow_nan => true).infinite?
144
+ assert_raise(JSON::ParserError) { JSON.parse('-Infinity', :allow_nan => true) }
145
+ assert JSON.parse('-Infinity', :quirks_mode => true, :allow_nan => true).infinite?
146
+ assert_raise(JSON::ParserError) { JSON.parse('[ 1, ]', :quirks_mode => true) }
147
+ end
148
+
111
149
  if Array.method_defined?(:permutation)
112
150
  def test_parse_more_complex_arrays
113
151
  a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
@@ -154,16 +192,68 @@ class TC_JSON < Test::Unit::TestCase
154
192
  assert_equal(@ary,
155
193
  parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
156
194
  ',[false],[true]]'))
157
- assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]
195
+ assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
158
196
  , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
159
197
  end
160
198
 
161
- class SubArray < Array; end
199
+ class SubArray < Array
200
+ def <<(v)
201
+ @shifted = true
202
+ super
203
+ end
204
+
205
+ def shifted?
206
+ @shifted
207
+ end
208
+ end
209
+
210
+ class SubArray2 < Array
211
+ def to_json(*a)
212
+ {
213
+ JSON.create_id => self.class.name,
214
+ 'ary' => to_a,
215
+ }.to_json(*a)
216
+ end
217
+
218
+ def self.json_create(o)
219
+ o.delete JSON.create_id
220
+ o['ary']
221
+ end
222
+ end
223
+
224
+ class SubArrayWrapper
225
+ def initialize
226
+ @data = []
227
+ end
228
+
229
+ attr_reader :data
162
230
 
163
- def test_parse_array_custom_class
164
- res = parse('[]', :array_class => SubArray)
165
- assert_equal([], res)
231
+ def [](index)
232
+ @data[index]
233
+ end
234
+
235
+ def <<(value)
236
+ @data << value
237
+ @shifted = true
238
+ end
239
+
240
+ def shifted?
241
+ @shifted
242
+ end
243
+ end
244
+
245
+ def test_parse_array_custom_array_derived_class
246
+ res = parse('[1,2]', :array_class => SubArray)
247
+ assert_equal([1,2], res)
166
248
  assert_equal(SubArray, res.class)
249
+ assert res.shifted?
250
+ end
251
+
252
+ def test_parse_array_custom_non_array_derived_class
253
+ res = parse('[1,2]', :array_class => SubArrayWrapper)
254
+ assert_equal([1,2], res.data)
255
+ assert_equal(SubArrayWrapper, res.class)
256
+ assert res.shifted?
167
257
  end
168
258
 
169
259
  def test_parse_object
@@ -173,12 +263,93 @@ class TC_JSON < Test::Unit::TestCase
173
263
  assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
174
264
  end
175
265
 
176
- class SubHash < Hash; end
266
+ class SubHash < Hash
267
+ def []=(k, v)
268
+ @item_set = true
269
+ super
270
+ end
271
+
272
+ def item_set?
273
+ @item_set
274
+ end
275
+ end
276
+
277
+ class SubHash2 < Hash
278
+ def to_json(*a)
279
+ {
280
+ JSON.create_id => self.class.name,
281
+ }.merge(self).to_json(*a)
282
+ end
283
+
284
+ def self.json_create(o)
285
+ o.delete JSON.create_id
286
+ self[o]
287
+ end
288
+ end
289
+
290
+ class SubOpenStruct < OpenStruct
291
+ def [](k)
292
+ __send__(k)
293
+ end
294
+
295
+ def []=(k, v)
296
+ @item_set = true
297
+ __send__("#{k}=", v)
298
+ end
177
299
 
178
- def test_parse_object_custom_class
179
- res = parse('{}', :object_class => SubHash)
180
- assert_equal({}, res)
300
+ def item_set?
301
+ @item_set
302
+ end
303
+ end
304
+
305
+ def test_parse_object_custom_hash_derived_class
306
+ res = parse('{"foo":"bar"}', :object_class => SubHash)
307
+ assert_equal({"foo" => "bar"}, res)
181
308
  assert_equal(SubHash, res.class)
309
+ assert res.item_set?
310
+ end
311
+
312
+ def test_parse_object_custom_non_hash_derived_class
313
+ res = parse('{"foo":"bar"}', :object_class => SubOpenStruct)
314
+ assert_equal "bar", res.foo
315
+ assert_equal(SubOpenStruct, res.class)
316
+ assert res.item_set?
317
+ end
318
+
319
+ def test_parse_generic_object
320
+ res = parse('{"foo":"bar", "baz":{}}', :object_class => JSON::GenericObject)
321
+ assert_equal(JSON::GenericObject, res.class)
322
+ assert_equal "bar", res.foo
323
+ assert_equal "bar", res["foo"]
324
+ assert_equal "bar", res[:foo]
325
+ assert_equal "bar", res.to_hash[:foo]
326
+ assert_equal(JSON::GenericObject, res.baz.class)
327
+ end
328
+
329
+ def test_generate_core_subclasses_with_new_to_json
330
+ obj = SubHash2["foo" => SubHash2["bar" => true]]
331
+ obj_json = JSON(obj)
332
+ obj_again = JSON.parse(obj_json, :create_additions => true)
333
+ assert_kind_of SubHash2, obj_again
334
+ assert_kind_of SubHash2, obj_again['foo']
335
+ assert obj_again['foo']['bar']
336
+ assert_equal obj, obj_again
337
+ assert_equal ["foo"], JSON(JSON(SubArray2["foo"]), :create_additions => true)
338
+ end
339
+
340
+ def test_generate_core_subclasses_with_default_to_json
341
+ assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
342
+ assert_equal '["foo"]', JSON(SubArray["foo"])
343
+ end
344
+
345
+ def test_generate_of_core_subclasses
346
+ obj = SubHash["foo" => SubHash["bar" => true]]
347
+ obj_json = JSON(obj)
348
+ obj_again = JSON(obj_json)
349
+ assert_kind_of Hash, obj_again
350
+ assert_kind_of Hash, obj_again['foo']
351
+ assert obj_again['foo']['bar']
352
+ assert_equal obj, obj_again
182
353
  end
183
354
 
184
355
  def test_parser_reset
@@ -275,12 +446,12 @@ EOT
275
446
  assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
276
447
  assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
277
448
  assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
278
- too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]'
449
+ too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
279
450
  too_deep_ary = eval too_deep
280
451
  assert_raises(JSON::NestingError) { JSON.parse too_deep }
281
452
  assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse }
282
- assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 }
283
- ok = JSON.parse too_deep, :max_nesting => 20
453
+ assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 100 }
454
+ ok = JSON.parse too_deep, :max_nesting => 101
284
455
  assert_equal too_deep_ary, ok
285
456
  ok = JSON.parse too_deep, :max_nesting => nil
286
457
  assert_equal too_deep_ary, ok
@@ -291,8 +462,8 @@ EOT
291
462
  assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
292
463
  assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
293
464
  assert_raises(JSON::NestingError) { JSON.generate too_deep_ary }
294
- assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 }
295
- ok = JSON.generate too_deep_ary, :max_nesting => 20
465
+ assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 100 }
466
+ ok = JSON.generate too_deep_ary, :max_nesting => 101
296
467
  assert_equal too_deep, ok
297
468
  ok = JSON.generate too_deep_ary, :max_nesting => nil
298
469
  assert_equal too_deep, ok
@@ -309,19 +480,38 @@ EOT
309
480
  JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
310
481
  end
311
482
 
312
- def test_load_dump
313
- too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
483
+ def test_load
484
+ assert_equal @hash, JSON.load(@json)
485
+ tempfile = Tempfile.open('json')
486
+ tempfile.write @json
487
+ tempfile.rewind
488
+ assert_equal @hash, JSON.load(tempfile)
489
+ stringio = StringIO.new(@json)
490
+ stringio.rewind
491
+ assert_equal @hash, JSON.load(stringio)
492
+ assert_equal nil, JSON.load(nil)
493
+ assert_equal nil, JSON.load('')
494
+ end
495
+
496
+ def test_load_with_options
497
+ small_hash = JSON("foo" => 'bar')
498
+ symbol_hash = { :foo => 'bar' }
499
+ assert_equal symbol_hash, JSON.load(small_hash, nil, :symbolize_names => true)
500
+ end
501
+
502
+ def test_dump
503
+ too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
314
504
  assert_equal too_deep, JSON.dump(eval(too_deep))
315
505
  assert_kind_of String, Marshal.dump(eval(too_deep))
316
- assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) }
317
- assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 19) }
318
- assert_equal too_deep, JSON.dump(eval(too_deep), 20)
319
- assert_kind_of String, Marshal.dump(eval(too_deep), 20)
506
+ assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 100) }
507
+ assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
508
+ assert_equal too_deep, JSON.dump(eval(too_deep), 101)
509
+ assert_kind_of String, Marshal.dump(eval(too_deep), 101)
320
510
  output = StringIO.new
321
511
  JSON.dump(eval(too_deep), output)
322
512
  assert_equal too_deep, output.string
323
513
  output = StringIO.new
324
- JSON.dump(eval(too_deep), output, 20)
514
+ JSON.dump(eval(too_deep), output, 101)
325
515
  assert_equal too_deep, output.string
326
516
  end
327
517
 
@@ -337,4 +527,19 @@ EOT
337
527
  json5 = JSON([orig = 1 << 64])
338
528
  assert_equal orig, JSON[json5][0]
339
529
  end
530
+
531
+ if defined?(JSON::Ext::Parser)
532
+ def test_allocate
533
+ parser = JSON::Ext::Parser.new("{}")
534
+ assert_raise(TypeError, '[ruby-core:35079]') {parser.__send__(:initialize, "{}")}
535
+ parser = JSON::Ext::Parser.allocate
536
+ assert_raise(TypeError, '[ruby-core:35079]') {parser.source}
537
+ end
538
+ end
539
+
540
+ def test_argument_encoding
541
+ source = "{}".force_encoding("ascii-8bit")
542
+ JSON::Parser.new(source)
543
+ assert_equal Encoding::ASCII_8BIT, source.encoding
544
+ end if defined?(Encoding::ASCII_8BIT)
340
545
  end