jrjackson 0.4.17-java → 0.4.19-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/jrjackson.gemspec +1 -2
  3. data/lib/com/fasterxml/jackson/core/jackson-annotations/2.15.4/jackson-annotations-2.15.4.jar +0 -0
  4. data/lib/com/fasterxml/jackson/core/jackson-core/2.15.4/jackson-core-2.15.4.jar +0 -0
  5. data/lib/com/fasterxml/jackson/core/jackson-databind/2.15.4/jackson-databind-2.15.4.jar +0 -0
  6. data/lib/com/fasterxml/jackson/module/jackson-module-afterburner/{2.14.1/jackson-module-afterburner-2.14.1.jar → 2.15.4/jackson-module-afterburner-2.15.4.jar} +0 -0
  7. data/lib/jrjackson/build_info.rb +8 -8
  8. data/lib/jrjackson/jars/jrjackson-1.2.36.jar +0 -0
  9. data/lib/jrjackson_jars.rb +8 -8
  10. data/pom.xml +47 -10
  11. metadata +14 -79
  12. data/.gitignore +0 -16
  13. data/.jrubyr_c +0 -433
  14. data/Gemfile +0 -12
  15. data/Mavenfile +0 -25
  16. data/Rakefile +0 -39
  17. data/alt_bench.rb +0 -46
  18. data/changelog.md +0 -181
  19. data/lib/com/fasterxml/jackson/core/jackson-annotations/2.14.1/jackson-annotations-2.14.1.jar +0 -0
  20. data/lib/com/fasterxml/jackson/core/jackson-core/2.14.1/jackson-core-2.14.1.jar +0 -0
  21. data/lib/com/fasterxml/jackson/core/jackson-databind/2.14.1/jackson-databind-2.14.1.jar +0 -0
  22. data/lib/jrjackson/jars/jrjackson-1.2.34.jar +0 -0
  23. data/profiling/profiled.rb +0 -15
  24. data/run_all_individual_bench.sh +0 -25
  25. data/run_jruby_individual_bench.sh +0 -20
  26. data/run_mri_individual_bench.sh +0 -7
  27. data/src/main/java/com/jrjackson/IParseHandler.java +0 -53
  28. data/src/main/java/com/jrjackson/JavaBigDecimalValueConverter.java +0 -17
  29. data/src/main/java/com/jrjackson/JavaBigIntValueConverter.java +0 -17
  30. data/src/main/java/com/jrjackson/JavaConverter.java +0 -10
  31. data/src/main/java/com/jrjackson/JavaFloatValueConverter.java +0 -16
  32. data/src/main/java/com/jrjackson/JavaHandler.java +0 -118
  33. data/src/main/java/com/jrjackson/JavaLongValueConverter.java +0 -16
  34. data/src/main/java/com/jrjackson/JjParse.java +0 -139
  35. data/src/main/java/com/jrjackson/JrJacksonBase.java +0 -152
  36. data/src/main/java/com/jrjackson/JrJacksonJava.java +0 -81
  37. data/src/main/java/com/jrjackson/JrJacksonRaw.java +0 -108
  38. data/src/main/java/com/jrjackson/JrJacksonRuby.java +0 -89
  39. data/src/main/java/com/jrjackson/JrJacksonSaj.java +0 -26
  40. data/src/main/java/com/jrjackson/JrJacksonSch.java +0 -25
  41. data/src/main/java/com/jrjackson/JrJacksonService.java +0 -38
  42. data/src/main/java/com/jrjackson/JrParse.java +0 -142
  43. data/src/main/java/com/jrjackson/ParseError.java +0 -16
  44. data/src/main/java/com/jrjackson/RubyAnySerializer.java +0 -291
  45. data/src/main/java/com/jrjackson/RubyBigDecimalValueConverter.java +0 -18
  46. data/src/main/java/com/jrjackson/RubyBigIntValueConverter.java +0 -21
  47. data/src/main/java/com/jrjackson/RubyConverter.java +0 -12
  48. data/src/main/java/com/jrjackson/RubyDateFormat.java +0 -34
  49. data/src/main/java/com/jrjackson/RubyFloatValueConverter.java +0 -18
  50. data/src/main/java/com/jrjackson/RubyHandler.java +0 -119
  51. data/src/main/java/com/jrjackson/RubyIntValueConverter.java +0 -18
  52. data/src/main/java/com/jrjackson/RubyJacksonModule.java +0 -80
  53. data/src/main/java/com/jrjackson/RubyKeyConverter.java +0 -12
  54. data/src/main/java/com/jrjackson/RubyNameConverter.java +0 -9
  55. data/src/main/java/com/jrjackson/RubyObjectDeserializer.java +0 -182
  56. data/src/main/java/com/jrjackson/RubyStringConverter.java +0 -18
  57. data/src/main/java/com/jrjackson/RubyStringKeyConverter.java +0 -15
  58. data/src/main/java/com/jrjackson/RubyStringNameConverter.java +0 -12
  59. data/src/main/java/com/jrjackson/RubySymbolKeyConverter.java +0 -15
  60. data/src/main/java/com/jrjackson/RubySymbolNameConverter.java +0 -12
  61. data/src/main/java/com/jrjackson/RubyUtils.java +0 -149
  62. data/src/main/java/com/jrjackson/SajParse.java +0 -169
  63. data/src/main/java/com/jrjackson/SchParse.java +0 -209
  64. data/src/main/java/com/jrjackson/StreamParse.java +0 -66
  65. data/src/test/java/com/jrjackson/RubyAnySerializerTest.java +0 -56
  66. data/test/jrjackson_test.rb +0 -578
@@ -1,578 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- $LOAD_PATH << File.expand_path('../../lib', __FILE__)
4
-
5
- require "java"
6
-
7
- require 'securerandom'
8
-
9
- require 'test/unit'
10
- require 'thread'
11
- require 'bigdecimal'
12
- require 'jrjackson'
13
- require 'stringio'
14
- require 'time'
15
- require 'date'
16
-
17
- class JrJacksonTest < Test::Unit::TestCase
18
- class Test::Unit::CustomObj
19
- end
20
-
21
- class Test::Unit::NullObj < BasicObject
22
- def nil?() true; end
23
- end
24
-
25
- class ToJsonData
26
- attr_reader :one, :two, :six
27
- def initialize(a,b,c)
28
- @one, @two, @six = a,b,c
29
- end
30
- def to_h
31
- {'one' => one, 'two' => two, 'six' => six}
32
- end
33
- def to_json_data
34
- [one, two, six]
35
- end
36
- end
37
-
38
- class CustomToH
39
- attr_accessor :one, :two, :six
40
- def initialize(a,b,c)
41
- @one, @two, @six = a,b,c
42
- end
43
- def to_h
44
- {'one' => one, 'two' => two, 'six' => six}
45
- end
46
- end
47
-
48
- class CustomToHash
49
- attr_accessor :one, :two, :six
50
- def initialize(a,b,c)
51
- @one, @two, @six = a,b,c
52
- end
53
- def to_hash
54
- {'one' => one, 'two' => two, 'six' => six}
55
- end
56
- end
57
-
58
- class CustomToJson
59
- attr_accessor :one, :two, :six
60
- def initialize(a,b,c)
61
- @one, @two, @six = a,b,c
62
- end
63
- def to_json
64
- %Q|{"one":#{one},"two":#{two},"six":#{six}}|
65
- end
66
- end
67
-
68
- class CustomToTime
69
- def initialize(tm = Time.now)
70
- @now = tm
71
- end
72
- def to_time
73
- @now.to_time.utc
74
- end
75
- end
76
-
77
- CustomStruct = Struct.new(:one, :two, :six)
78
-
79
- class StrangeError < RuntimeError
80
-
81
- end
82
-
83
- class ScHandler
84
- attr_accessor :calls
85
-
86
- def initialize(arr = [])
87
- @calls = arr
88
- end
89
-
90
- def hash_start()
91
- @calls << [:hash_start]
92
- {}
93
- end
94
-
95
- def hash_end()
96
- @calls << [:hash_end]
97
- end
98
-
99
- def hash_key(key)
100
- @calls << [:hash_key, key]
101
- return 'too' if 'two' == key
102
- return :symbol if 'symbol' == key
103
- key
104
- end
105
-
106
- def array_start()
107
- @calls << [:array_start]
108
- []
109
- end
110
-
111
- def array_end()
112
- @calls << [:array_end]
113
- end
114
-
115
- def add_value(value)
116
- @calls << [:add_value, value]
117
- end
118
-
119
- def hash_set(h, key, value)
120
- # h[key] = value
121
- @calls << [:hash_set, key, value]
122
- end
123
-
124
- def array_append(a, value)
125
- # a.push(value)
126
- @calls << [:array_append, value]
127
- end
128
- end
129
-
130
- JsonString = %Q|{
131
- "array": [
132
- {
133
- "num" : 3,
134
- "string": "message",
135
- "hash" : {
136
- "h2" : {
137
- "a" : [ 1, 2, 3 ]
138
- }
139
- }
140
- }
141
- ],
142
- "boolean" : true
143
- }|
144
-
145
- def test_sc_parse
146
- array = []
147
- handler = ScHandler.new(array)
148
- JrJackson::Json.sc_load(handler, JsonString)
149
- assert_equal(
150
- [
151
- [:hash_start],
152
- [:array_start],
153
- [:hash_start],
154
- [:hash_key, 'num'],
155
- [:hash_set, "num", 3],
156
- [:hash_key, 'string'],
157
- [:hash_set, "string", "message"],
158
- [:hash_start],
159
- [:hash_start],
160
- [:array_start],
161
- [:array_append, 1],
162
- [:array_append, 2],
163
- [:array_append, 3],
164
- [:array_end],
165
- [:hash_key, "a"],
166
- [:hash_set, "a", []],
167
- [:hash_end],
168
- [:hash_key, "h2"],
169
- [:hash_set, "h2", {}],
170
- [:hash_end],
171
- [:hash_key, "hash"],
172
- [:hash_set, "hash", {}],
173
- [:hash_end],
174
- [:array_append, {}],
175
- [:array_end],
176
- [:hash_key, "array"],
177
- [:hash_set, "array", []],
178
- [:hash_key, 'boolean'],
179
- [:hash_set, "boolean", true],
180
- [:hash_end],
181
- [:add_value, {}]
182
- ],
183
- handler.calls
184
- )
185
- end
186
-
187
- class SjHandler
188
- attr_reader :calls
189
- def initialize(arr = []) @calls = arr; end
190
- def hash_start(key) @calls << [:hash_start, key]; end
191
- def hash_end(key) @calls << [:hash_end, key]; end
192
- def array_start(key) @calls << [:array_start, key]; end
193
- def array_end(key) @calls << [:array_end, key]; end
194
- def add_value(value, key) @calls << [:add_value, value, key]; end
195
- def error(message, line, column) @calls << [:error, message, line, column]; end
196
- end
197
-
198
- def test_sj_parse
199
- handler = SjHandler.new()
200
- JrJackson::Json.sj_load(handler, JsonString)
201
- assert_equal(
202
- [
203
- [:hash_start, nil],
204
- [:array_start, 'array'],
205
- [:hash_start, nil],
206
- [:add_value, 3, 'num'],
207
- [:add_value, 'message', 'string'],
208
- [:hash_start, 'hash'],
209
- [:hash_start, 'h2'],
210
- [:array_start, 'a'],
211
- [:add_value, 1, nil],
212
- [:add_value, 2, nil],
213
- [:add_value, 3, nil],
214
- [:array_end, 'a'],
215
- [:hash_end, 'h2'],
216
- [:hash_end, 'hash'],
217
- [:hash_end, nil],
218
- [:array_end, 'array'],
219
- [:add_value, true, 'boolean'],
220
- [:hash_end, nil]
221
- ],
222
- handler.calls
223
- )
224
- end
225
-
226
- def test_to_json_data
227
- object = ToJsonData.new("uno", :two, 6.0)
228
- expected = "[1,[\"uno\",\"two\",6.0]]"
229
- actual = JrJackson::Json.dump([1, object])
230
- assert_equal expected, actual
231
- end
232
-
233
- def test_datetime
234
- h = {datetime: DateTime.parse("2014-01-27T18:24:46+01:00")}
235
- expected = '{"datetime":"2014-01-27 17:24:46 +0000"}'
236
- actual = JrJackson::Json.dump(h)
237
- assert_equal expected, actual
238
- end
239
-
240
- def test_dump_date_in_array
241
- expected = "[\"2016-04-10\"]"
242
- td = Date.new(2016, 4, 10)
243
- actual = JrJackson::Json.generate([td])
244
- assert_equal(actual, expected)
245
- end
246
-
247
- def test_threading
248
- q1, q2, q3 = Queue.new, Queue.new, Queue.new
249
-
250
- s1 = %Q|{"a":2.5, "b":0.214, "c":3.4567, "d":-3.4567}|
251
- th1 = Thread.new(s1) do |string|
252
- q1 << JrJackson::Json.load(string, {use_bigdecimal: true, raw: true})
253
- end
254
- th2 = Thread.new(s1) do |string|
255
- q2 << JrJackson::Json.load(string, {use_bigdecimal: true, symbolize_keys: true})
256
- end
257
- th3 = Thread.new(s1) do |string|
258
- q3 << JrJackson::Json.load(string, {use_bigdecimal: false, symbolize_keys: true})
259
- end
260
- a1, a2, a3 = q1.pop, q2.pop, q3.pop
261
- assert_equal ["a", "b", "c", "d"], a1.keys
262
- assert a1.values.all? {|v| v.is_a?(Java::JavaMath::BigDecimal)}, "Expected all values to be Java::JavaMath::BigDecimal"
263
- assert_equal [:a, :b, :c, :d], a2.keys
264
- assert a2.values.all? {|v| v.is_a?(BigDecimal)}, "Expected all values to be BigDecimal"
265
- assert a3.values.all? {|v| v.is_a?(Float)}, "Expected all values to be Float"
266
- end
267
-
268
- def test_deserialize_JSON_with_UTF8_characters
269
- json_string = JrJackson::Json.dump({"utf8" => "żółć"})
270
- expected = {utf8: "żółć"}
271
- actual = JrJackson::Json.load(json_string, :symbolize_keys => true)
272
-
273
- assert_equal expected, actual
274
- end
275
-
276
- def test_deserialize_JSON_with_two_entries
277
- json_string = JrJackson::Json.dump({'foo' => 'foo_value', 'bar' => 'bar_value'})
278
- expected = {foo: 'foo_value', bar: 'bar_value'}
279
- actual = JrJackson::Json.load(json_string, :symbolize_keys => true)
280
- assert_equal expected, actual
281
- end
282
-
283
- def test_serialize_non_json_datatypes_as_values
284
- dt = Time.now.utc
285
- today = Date.today
286
- co1 = CustomToH.new("uno", :two, 6.0)
287
- co2 = CustomToHash.new("uno", :two, 6.0)
288
- co3 = CustomToJson.new(1.0, 2, 6.0)
289
- co4 = CustomStruct.new(1, 2, 6)
290
- co5 = CustomToTime.new(today)
291
- source = {'sym' => :a_symbol, 'dt' => dt, 'co1' => co1, 'co2' => co2, 'co3' => co3, 'co4' => co4, 'co5' => co5}
292
- json_string = JrJackson::Json.dump(source)
293
- expected = {
294
- :sym => "a_symbol",
295
- :dt => dt.strftime('%F %T %z'),
296
- :co1 => {:one => "uno", :two => "two", :six => 6.0 },
297
- :co2 => {:one => "uno", :two => "two", :six => 6.0 },
298
- :co3 => {:one => 1.0, :two => 2.0, :six => 6.0 },
299
- :co4 => [1, 2, 6],
300
- :co5 => today.to_time.utc.strftime('%F %T %z')
301
- }
302
- actual = JrJackson::Json.load(json_string, :symbolize_keys => true)
303
- assert_equal expected, actual
304
- end
305
-
306
- def test_raw_serialize_base_classes
307
- # String
308
- assert_equal JrJackson::Json.dump("foo"), "\"foo\""
309
-
310
- # Hash and implementations of the Java Hash interface
311
- assert_equal JrJackson::Json.dump({"foo" => 1}), "{\"foo\":1}"
312
- assert_equal JrJackson::Json.dump(Java::JavaUtil::HashMap.new({"foo" => 1})), "{\"foo\":1}"
313
- assert_equal JrJackson::Json.dump(Java::JavaUtil::LinkedHashMap.new({"foo" => 1})), "{\"foo\":1}"
314
-
315
- # Array and implementations of the Java List interface
316
- assert_equal JrJackson::Json.dump(["foo", 1]), "[\"foo\",1]"
317
- assert_equal JrJackson::Json.dump(Java::JavaUtil::ArrayList.new(["foo", 1])), "[\"foo\",1]"
318
- assert_equal JrJackson::Json.dump(Java::JavaUtil::LinkedList.new(["foo", 1])), "[\"foo\",1]"
319
- assert_equal JrJackson::Json.dump(Java::JavaUtil::Vector.new(["foo", 1])), "[\"foo\",1]"
320
-
321
- # true/false
322
- assert_equal JrJackson::Json.dump(true), "true"
323
- assert_equal JrJackson::Json.dump(false), "false"
324
-
325
- # nil
326
- assert_equal JrJackson::Json.dump(nil), "null"
327
- end
328
-
329
- def test_serialize_date
330
- # default date format
331
- time_string = "2014-06-10 18:18:40 EDT"
332
- source_time = Time.parse(time_string)
333
- serialized_output = JrJackson::Json.dump({"time" => source_time})
334
- other_time = Time.parse(serialized_output.split('"')[-2])
335
- assert_equal other_time.to_f, source_time.to_f
336
- end
337
-
338
- def test_serialize_date_date_format
339
-
340
- time = Time.new(2014,6,10,18,18,40, "-04:00")
341
- # using date_format option
342
- assert_equal "{\"time\":\"2014-06-10\"}", JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd", :timezone => "UTC")
343
- assert_match(/\{"time"\:"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}[+-]\d{4}"\}/, JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd'T'HH:mm:ss.SSSZ"))
344
- end
345
-
346
- def test_serialize_date_date_format_timezone
347
-
348
- time = Time.new(2014,6,10,18,18,40, "-04:00")
349
- # using date_format and timezone options
350
- assert_equal "{\"time\":\"2014-06-10T22:18:40.000+0000\"}", JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd'T'HH:mm:ss.SSSZ", :timezone => "UTC")
351
- # iso8601 date_format and timezone
352
- assert_equal "{\"time\":\"2014-06-10T22:18:40.000Z\"}", JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd'T'HH:mm:ss.SSSX", :timezone => "UTC")
353
- end
354
-
355
- def test_can_parse_returning_java_objects
356
- expected = {"arr"=>[2, 3, 4],
357
- "flo"=>0.333E1,
358
- "moo"=>"bar",
359
- "utf8"=>"żółć",
360
- "zzz"=>{"bar"=>-9}}
361
- json = '{"utf8":"żółć", "moo": "bar", "zzz": {"bar":-9}, "arr": [2,3,4], "flo": 3.33}'
362
-
363
- actual = JrJackson::Json.parse_java(json)
364
-
365
- assert_equal Java::JavaUtil::HashMap, actual.class
366
- assert_equal Java::JavaUtil::ArrayList, actual["arr"].class
367
- assert_equal Java::JavaMath::BigDecimal, actual["flo"].class
368
- assert_equal "3.33", actual["flo"].to_s
369
- assert_equal "bar", actual["moo"]
370
- assert_equal "żółć", actual["utf8"]
371
- assert_equal Java::JavaUtil::HashMap, actual["zzz"].class
372
- if 1.class == Integer
373
- # avoid deprecation warning as Ruby 2.4 unifies Fixnum and Bignum into Integer
374
- assert_equal Integer, actual["zzz"]["bar"].class
375
- else
376
- assert_equal Bignum, actual["zzz"]["bar"].class
377
- end
378
- assert_equal(-9, actual["zzz"]["bar"])
379
- end
380
-
381
- def test_can_parse_returning_ruby_objects_string_keys
382
- expected = {
383
- "a"=>"żółć", # string
384
- "b"=>true, # boolean
385
- "c"=>12345, # number
386
- "d"=>
387
- [true,
388
- [false,
389
- [-123456789, nil],
390
- 0.39676E1,
391
- ["Something else.", false],
392
- nil]], # mix it up array
393
- "e"=>{"zero"=>nil, "one"=>1, "two"=>2, "three"=>[3], "four"=>[0, 1, 2, 3, 4]}, # hash
394
- "żółć"=>nil,# nil
395
- "h"=>{"a"=>{"b"=>{"c"=>{"d"=>{"e"=>{"f"=>{"g"=>nil}}}}}}},# deep hash, not that deep
396
- "i"=>[[[[[[[nil]]]]]]] # deep array, again, not that deep
397
- }
398
- json = JrJackson::Json.dump(expected)
399
- actual = JrJackson::Json.parse_ruby(json)
400
- assert_equal expected, actual
401
- actual = JrJackson::Ruby.parse(json, {})
402
- assert_equal expected, actual
403
- end
404
-
405
- def test_can_parse_returning_ruby_objects_symbol_keys
406
- expected = {:a=>"Alpha",
407
- :b=>true,
408
- :c=>12345,
409
- :d=>
410
- [true, [false, [-123456789, nil], 3.9676, ["Something else.", false], nil]],
411
- :e=>{:zero=>nil, :one=>1, :two=>2, :three=>[3], :four=>[0, 1, 2, 3, 4]},
412
- :f=>nil,
413
- :h=>{:a=>{:b=>{:c=>{:d=>{:e=>{:f=>{:g=>nil}}}}}}},
414
- :i=>[[[[[[[nil]]]]]]]
415
- }
416
- json = JrJackson::Json.dump(expected)
417
- actual = JrJackson::Ruby.parse_sym(json, {})
418
- assert_equal expected, actual
419
- end
420
-
421
- def test_can_parse_nulls
422
- expected = {"foo" => nil}
423
- json = '{"foo":null}'
424
- actual = JrJackson::Json.parse(json)
425
- assert_equal expected, actual
426
- end
427
-
428
- def test_stringio
429
- expected = {"foo" => 5, "utf8" => "żółć"}
430
- json = ::StringIO.new('{"foo":5, "utf8":"żółć"}')
431
- actual = JrJackson::Json.load_ruby(json)
432
- assert_equal expected, actual
433
- end
434
-
435
- def test_ruby_io
436
- expected = {"foo" => 5, "bar" => 6, "utf8" => "żółć"}
437
- json, w = IO.pipe
438
- w.write('{"foo":5, "bar":6, "utf8":"żółć"}')
439
- w.close
440
- actual = JrJackson::Json.load(json)
441
- assert_equal expected, actual
442
- end
443
-
444
- def test_bad_utf
445
- assert_raise JrJackson::ParseError do
446
- JrJackson::Json.load("\x82\xAC\xEF")
447
- end
448
- end
449
-
450
- def test_can_parse_bignum
451
- expected = 12345678901234567890123456789
452
- json = '{"foo":12345678901234567890123456789}'
453
-
454
- actual = JrJackson::Json.parse(json)['foo']
455
- assert_equal expected, actual
456
- end
457
-
458
- def test_can_parse_big_decimals
459
- expected = BigDecimal('0.12345678901234567890123456789')
460
- json = '{"foo":0.12345678901234567890123456789}'
461
-
462
- actual = JrJackson::Json.parse(json, :use_bigdecimal => true)['foo']
463
- assert_bigdecimal_equal expected, actual
464
-
465
- actual = JrJackson::Json.parse(json, :use_bigdecimal => true, :symbolize_keys => true)[:foo]
466
- assert_bigdecimal_equal expected, actual
467
-
468
- actual = JrJackson::Java.parse(json, {})['foo']
469
- assert_bigdecimal_similar expected, actual
470
-
471
- actual = JrJackson::Json.parse(json, :use_bigdecimal => true, :raw => true)['foo']
472
- assert_bigdecimal_similar expected, actual
473
-
474
- end
475
-
476
- def test_can_serialize_object
477
- obj = Object.new
478
- actual = JrJackson::Json.dump({"foo" => obj})
479
- assert_equal "{\"foo\":\"#{obj}\"}", actual
480
- end
481
-
482
- def test_can_serialize_basic_object
483
- obj = BasicObject.new
484
- actual = JrJackson::Json.dump({"foo" => obj})
485
- assert_equal "{\"foo\":\"#<BasicObject>\"}", actual
486
- end
487
-
488
- def test_can_serialize_basic_object_subclass
489
- obj = Test::Unit::NullObj.new
490
- actual = JrJackson::Json.dump({"foo" => obj})
491
- assert_equal "{\"foo\":\"#<Test::Unit::NullObj>\"}", actual
492
- end
493
-
494
- def test_can_serialize_custom_object
495
- obj = Test::Unit::CustomObj.new
496
- actual = JrJackson::Json.dump({"foo" => obj})
497
- assert_equal "{\"foo\":\"#{obj}\"}", actual
498
- end
499
-
500
- def test_supports_pretty_printing
501
- object = {"foo" => 5, "utf8" => "żółć"}
502
- actual = JrJackson::Json.dump(object, :pretty => true)
503
- assert_equal "{\n \"foo\" : 5,\n \"utf8\" : \"żółć\"\n}", actual
504
- end
505
-
506
- def test_can_serialise_non_string_keys
507
- object = {5 => "foo"}
508
- actual = JrJackson::Json.dump(object)
509
- assert_equal "{\"5\":\"foo\"}", actual
510
- end
511
-
512
- def test_can_serialise_regex_match_data
513
- re = %r|(foo)(bar)(baz)|
514
- m = re.match('foobarbaz')
515
- object = {'matched' => m[2]}
516
- actual = JrJackson::Json.dump(object)
517
- assert_equal "{\"matched\":\"bar\"}", actual
518
- end
519
-
520
- def test_can_mix_java_and_ruby_objects
521
- json = '{"utf8":"żółć", "moo": "bar", "arr": [2,3,4], "flo": 3.33}'
522
- timeobj = Time.new(2015,11,11,11,11,11,0).utc
523
- expected = '{"mixed":{"arr":[2,3,4],"utf8":"żółć","flo":3.33,"zzz":{"one":1.0,"two":2,"six":6.0},"moo":"bar"},"time":"2015-11-11 11:11:11 +0000"}'
524
- object = JrJackson::Json.parse_java(json)
525
- object["zzz"] = CustomToJson.new(1.0, 2, 6.0)
526
- mixed = {"mixed" => object}
527
- mixed['time'] = timeobj
528
- actual = JrJackson::Json.dump(mixed)
529
- assert_equal expected, actual
530
- end
531
-
532
- def test_can_serialize_exceptions
533
- e = StrangeError.new("Something immensely bad happened")
534
- object = {'error' => e}
535
- actual = JrJackson::Json.dump(object)
536
- assert_equal "{\"error\":\"#{e.inspect}\"}", actual
537
- end
538
-
539
- def test_can_serialize_class
540
- object = {"foo" => BasicObject}
541
- actual = JrJackson::Json.dump(object)
542
- assert_equal "{\"foo\":\"#{BasicObject.inspect}\"}", actual
543
- end
544
-
545
- def test_can_handle_big_numbers
546
- object = {"foo" => 2**63, "bar" => 65536}
547
- actual = JrJackson::Json.dump(object)
548
- assert_equal "{\"foo\":9223372036854775808,\"bar\":65536}", actual
549
- end
550
-
551
-
552
- # This test failed more often than not before fixing the underlying code
553
- # and would fail every time if `100_000.times` was changed to `loop`
554
- def test_concurrent_dump
555
- now = Time.now
556
- num_threads = 100
557
-
558
- threads = num_threads.times.map do |i|
559
- Thread.new do
560
- 100_000.times { JrJackson::Json.dump("a" => now) }
561
- end
562
- end
563
- threads.each(&:join)
564
- end
565
-
566
- # -----------------------------
567
-
568
- def assert_bigdecimal_equal(expected, actual)
569
- assert_equal expected, actual
570
- assert_equal expected.class, actual.class
571
- assert_equal BigDecimal, actual.class
572
- end
573
-
574
- def assert_bigdecimal_similar(expected, actual)
575
- assert_equal BigDecimal(expected.to_s).round(11), BigDecimal(actual.to_s).round(11)
576
- assert_equal Java::JavaMath::BigDecimal, actual.class
577
- end
578
- end