oj 2.11.5 → 2.12.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of oj might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +8 -11
- data/ext/oj/dump.c +55 -14
- data/ext/oj/object.c +188 -3
- data/ext/oj/oj.c +18 -6
- data/ext/oj/oj.h +4 -1
- data/ext/oj/parse.c +4 -1
- data/ext/oj/parse.h +1 -0
- data/ext/oj/sparse.c +3 -1
- data/lib/oj/version.rb +1 -1
- data/test/bug.rb +40 -51
- data/test/bug2.rb +10 -0
- data/test/io.rb +48 -0
- data/test/{test_range.rb → mod.rb} +6 -9
- data/test/struct.rb +29 -0
- data/test/test_compat.rb +62 -0
- data/test/test_file.rb +14 -3
- data/test/test_object.rb +185 -17
- data/test/test_serializer.rb +59 -0
- data/test/test_various.rb +5 -15
- data/test/write_timebars.rb +31 -0
- data/test/zip.rb +34 -0
- metadata +10 -7
- data/test/perf1.rb +0 -64
- data/test/perf2.rb +0 -76
- data/test/perf_obj_old.rb +0 -213
- data/test/test_bigd.rb +0 -63
data/ext/oj/oj.h
CHANGED
@@ -82,6 +82,7 @@ typedef enum {
|
|
82
82
|
|
83
83
|
typedef enum {
|
84
84
|
UnixTime = 'u',
|
85
|
+
UnixZTime = 'z',
|
85
86
|
XmlTime = 'x',
|
86
87
|
RubyTime = 'r'
|
87
88
|
} TimeFormat;
|
@@ -247,7 +248,6 @@ extern VALUE oj_stream_writer_class;
|
|
247
248
|
extern VALUE oj_string_writer_class;
|
248
249
|
extern VALUE oj_stringio_class;
|
249
250
|
extern VALUE oj_struct_class;
|
250
|
-
extern VALUE oj_time_class;
|
251
251
|
|
252
252
|
extern VALUE oj_slash_string;
|
253
253
|
|
@@ -269,6 +269,7 @@ extern ID oj_instance_variables_id;
|
|
269
269
|
extern ID oj_json_create_id;
|
270
270
|
extern ID oj_length_id;
|
271
271
|
extern ID oj_new_id;
|
272
|
+
extern ID oj_parse_id;
|
272
273
|
extern ID oj_pos_id;
|
273
274
|
extern ID oj_read_id;
|
274
275
|
extern ID oj_readpartial_id;
|
@@ -283,7 +284,9 @@ extern ID oj_to_time_id;
|
|
283
284
|
extern ID oj_tv_nsec_id;
|
284
285
|
extern ID oj_tv_sec_id;
|
285
286
|
extern ID oj_tv_usec_id;
|
287
|
+
extern ID oj_utc_id;
|
286
288
|
extern ID oj_utc_offset_id;
|
289
|
+
extern ID oj_utcq_id;
|
287
290
|
extern ID oj_write_id;
|
288
291
|
|
289
292
|
#if USE_PTHREAD_MUTEX
|
data/ext/oj/parse.c
CHANGED
@@ -47,7 +47,8 @@
|
|
47
47
|
#else
|
48
48
|
#define NUM_MAX (FIXNUM_MAX >> 8)
|
49
49
|
#endif
|
50
|
-
|
50
|
+
//#define EXP_MAX 1023
|
51
|
+
#define EXP_MAX 100000
|
51
52
|
#define DEC_MAX 15
|
52
53
|
|
53
54
|
static void
|
@@ -401,6 +402,7 @@ read_num(ParseInfo pi) {
|
|
401
402
|
ni.infinity = 0;
|
402
403
|
ni.nan = 0;
|
403
404
|
ni.neg = 0;
|
405
|
+
ni.hasExp = 0;
|
404
406
|
ni.no_big = (FloatDec == pi->options.bigdec_load);
|
405
407
|
|
406
408
|
if ('-' == *pi->cur) {
|
@@ -463,6 +465,7 @@ read_num(ParseInfo pi) {
|
|
463
465
|
if ('e' == *pi->cur || 'E' == *pi->cur) {
|
464
466
|
int eneg = 0;
|
465
467
|
|
468
|
+
ni.hasExp = 1;
|
466
469
|
pi->cur++;
|
467
470
|
if ('-' == *pi->cur) {
|
468
471
|
pi->cur++;
|
data/ext/oj/parse.h
CHANGED
data/ext/oj/sparse.c
CHANGED
@@ -48,7 +48,7 @@
|
|
48
48
|
#else
|
49
49
|
#define NUM_MAX (FIXNUM_MAX >> 8)
|
50
50
|
#endif
|
51
|
-
#define EXP_MAX
|
51
|
+
#define EXP_MAX 100000
|
52
52
|
#define DEC_MAX 15
|
53
53
|
|
54
54
|
static void
|
@@ -413,6 +413,7 @@ read_num(ParseInfo pi) {
|
|
413
413
|
ni.infinity = 0;
|
414
414
|
ni.nan = 0;
|
415
415
|
ni.neg = 0;
|
416
|
+
ni.hasExp = 0;
|
416
417
|
ni.no_big = (FloatDec == pi->options.bigdec_load);
|
417
418
|
c = reader_get(&pi->rd);
|
418
419
|
if ('-' == c) {
|
@@ -467,6 +468,7 @@ read_num(ParseInfo pi) {
|
|
467
468
|
if ('e' == c || 'E' == c) {
|
468
469
|
int eneg = 0;
|
469
470
|
|
471
|
+
ni.hasExp = 1;
|
470
472
|
c = reader_get(&pi->rd);
|
471
473
|
if ('-' == c) {
|
472
474
|
c = reader_get(&pi->rd);
|
data/lib/oj/version.rb
CHANGED
data/test/bug.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
1
3
|
#!/usr/bin/env ruby
|
2
4
|
# encoding: UTF-8
|
3
5
|
|
@@ -5,60 +7,47 @@ $: << File.dirname(__FILE__)
|
|
5
7
|
|
6
8
|
require 'helper'
|
7
9
|
|
8
|
-
require 'oj'
|
9
|
-
require 'securerandom'
|
10
|
-
|
11
10
|
class Handler
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
def
|
17
|
-
|
11
|
+
def initialize
|
12
|
+
@state = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def hash_start
|
16
|
+
@state << {}
|
17
|
+
@state.last
|
18
|
+
end
|
19
|
+
|
20
|
+
def hash_end
|
21
|
+
@state.pop
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash_set(h,k,v)
|
25
|
+
h.store(k,v)
|
18
26
|
end
|
19
|
-
end
|
20
27
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
name = "/tmp/#{SecureRandom.uuid}"
|
25
|
-
`mkfifo #{name}`
|
26
|
-
if fork
|
27
|
-
open(name, 'r+') do |read_io|
|
28
|
-
p "start reading #{read_io.stat.ftype}"
|
29
|
-
Oj.sc_parse(Handler.new, read_io) {|v| p v}
|
30
|
-
p "stop reading"
|
31
|
-
end
|
32
|
-
else
|
33
|
-
open(name, 'w+') do |write_io|
|
34
|
-
p "start writing #{write_io.stat.ftype} autoclose: #{write_io.autoclose?}"
|
35
|
-
write_io.write json
|
36
|
-
write_io.write json
|
37
|
-
p "stop writing"
|
38
|
-
end
|
39
|
-
sleep(1) # make it obvious that there are two threads
|
40
|
-
open(name, 'w+') do |write_io|
|
41
|
-
p "start writing #{write_io.stat.ftype}"
|
42
|
-
write_io.write json
|
43
|
-
write_io.write json
|
44
|
-
p "stop writing"
|
45
|
-
end
|
28
|
+
def array_start
|
29
|
+
@state << []
|
30
|
+
@state.last
|
46
31
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
write_io.write json
|
60
|
-
p "stop writing"
|
61
|
-
write_io.close
|
62
|
-
end
|
32
|
+
|
33
|
+
|
34
|
+
def array_end
|
35
|
+
@state.pop
|
36
|
+
end
|
37
|
+
|
38
|
+
def array_append(a,v)
|
39
|
+
a << v
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_value(v)
|
43
|
+
p v
|
63
44
|
end
|
45
|
+
|
46
|
+
def error(message, line, column); p "ERROR: #{message}" end
|
64
47
|
end
|
48
|
+
|
49
|
+
$handler = Handler.new
|
50
|
+
|
51
|
+
IO.popen("cat tst") { |p| puts Oj.sc_parse($handler, p) }
|
52
|
+
|
53
|
+
#File.open('tst', 'r') { |file| Oj.sc_parse($handler, file) }
|
data/test/bug2.rb
ADDED
data/test/io.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
class Handler
|
9
|
+
def initialize
|
10
|
+
@state = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def hash_start
|
14
|
+
@state << {}
|
15
|
+
@state.last
|
16
|
+
end
|
17
|
+
|
18
|
+
def hash_end
|
19
|
+
@state.pop
|
20
|
+
end
|
21
|
+
|
22
|
+
def hash_set(h,k,v)
|
23
|
+
h.store(k,v)
|
24
|
+
end
|
25
|
+
|
26
|
+
def array_start
|
27
|
+
@state << []
|
28
|
+
@state.last
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def array_end
|
33
|
+
@state.pop
|
34
|
+
end
|
35
|
+
|
36
|
+
def array_append(a,v)
|
37
|
+
a << v
|
38
|
+
end
|
39
|
+
|
40
|
+
def error(message, line, column); p "ERROR: #{message}" end
|
41
|
+
end
|
42
|
+
|
43
|
+
handler = Handler.new
|
44
|
+
def handler.add_value(v)
|
45
|
+
p v
|
46
|
+
end
|
47
|
+
|
48
|
+
Oj.sc_parse(handler, StringIO.new('{"a":"b","c":[1,2,{"d":"e"}]}[4,5,6]'))
|
@@ -1,19 +1,16 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
|
-
$VERBOSE = true
|
5
|
-
|
6
4
|
%w(lib ext test).each do |dir|
|
7
5
|
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
8
6
|
end
|
9
7
|
|
10
|
-
require 'rubygems' if RUBY_VERSION.start_with?('1.8.')
|
11
8
|
require 'oj'
|
12
9
|
|
13
|
-
Oj.mimic_JSON
|
14
|
-
|
15
|
-
#puts Oj.default_options
|
16
|
-
|
17
|
-
range = ("01".."12")
|
18
10
|
|
19
|
-
|
11
|
+
Thread.new do
|
12
|
+
string_io = StringIO.new('{"foo":"bar"}')
|
13
|
+
Oj.load(string_io)
|
14
|
+
string_io.rewind
|
15
|
+
puts string_io.read
|
16
|
+
end.join
|
data/test/struct.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
# Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
|
5
|
+
# required. That can be set in the RUBYOPT environment variable.
|
6
|
+
# export RUBYOPT=-w
|
7
|
+
|
8
|
+
$VERBOSE = true
|
9
|
+
|
10
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
11
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
12
|
+
|
13
|
+
require 'oj'
|
14
|
+
|
15
|
+
A = Struct.new(:a,:b,:c,:d)
|
16
|
+
B = Struct.new(:e,:f)
|
17
|
+
|
18
|
+
obj = [A.new(55, B.new(1, 'X'), B.new(2, 'Y'), 3)]
|
19
|
+
|
20
|
+
s = Oj.dump(obj, :mode => :object)
|
21
|
+
|
22
|
+
100000.times do
|
23
|
+
Oj.load(s, :mode => :object)
|
24
|
+
# ds = Oj.dump(o, :mode => :object)
|
25
|
+
# if ds != s
|
26
|
+
# puts ds
|
27
|
+
# raise "holy crap"
|
28
|
+
# end
|
29
|
+
end
|
data/test/test_compat.rb
CHANGED
@@ -192,6 +192,68 @@ class CompatJuice < Minitest::Test
|
|
192
192
|
assert_equal(orig, bg)
|
193
193
|
end
|
194
194
|
|
195
|
+
# Time
|
196
|
+
def test_time_ruby
|
197
|
+
if RUBY_VERSION.start_with?('1.8')
|
198
|
+
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
199
|
+
else
|
200
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
201
|
+
end
|
202
|
+
expect = '"' + t.to_s + '"'
|
203
|
+
json = Oj.dump(t, :mode => :compat, :time_format => :ruby)
|
204
|
+
assert_equal(expect, json)
|
205
|
+
end
|
206
|
+
def test_time_xml
|
207
|
+
if RUBY_VERSION.start_with?('1.8')
|
208
|
+
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
209
|
+
else
|
210
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
211
|
+
end
|
212
|
+
json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 6)
|
213
|
+
assert_equal('"2015-01-05T21:37:07.123456-08:00"', json)
|
214
|
+
end
|
215
|
+
unless RUBY_VERSION.start_with?('1.8')
|
216
|
+
def test_time_xml_12345
|
217
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456, 12345)
|
218
|
+
json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 6)
|
219
|
+
assert_equal('"2015-01-05T21:37:07.123456+03:25"', json)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
def test_time_unix
|
223
|
+
if RUBY_VERSION.start_with?('1.8')
|
224
|
+
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
225
|
+
else
|
226
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
227
|
+
end
|
228
|
+
json = Oj.dump(t, :mode => :compat, :time_format => :unix, :second_precision => 6)
|
229
|
+
assert_equal('1420522627.123456', json)
|
230
|
+
end
|
231
|
+
def test_time_unix_zone
|
232
|
+
if RUBY_VERSION.start_with?('1.8')
|
233
|
+
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
234
|
+
else
|
235
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
236
|
+
end
|
237
|
+
json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
|
238
|
+
assert_equal('1420493827.123456e-28800', json)
|
239
|
+
end
|
240
|
+
unless RUBY_VERSION.start_with?('1.8')
|
241
|
+
def test_time_unix_zone_12345
|
242
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456, 12345)
|
243
|
+
json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
|
244
|
+
assert_equal('1420493827.123456e12345', json)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
def test_time_unix_zone_early
|
248
|
+
if RUBY_VERSION.start_with?('1.8')
|
249
|
+
t = Time.parse('1954-01-05T21:37:07.123456-08:00')
|
250
|
+
else
|
251
|
+
t = Time.new(1954, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
252
|
+
end
|
253
|
+
json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
|
254
|
+
assert_equal('-504498172.876544e-28800', json)
|
255
|
+
end
|
256
|
+
|
195
257
|
# Stream IO
|
196
258
|
def test_io_string
|
197
259
|
json = %{{
|
data/test/test_file.rb
CHANGED
@@ -126,12 +126,12 @@ class FileJuice < Minitest::Test
|
|
126
126
|
# Time
|
127
127
|
def test_time_object
|
128
128
|
t = Time.now()
|
129
|
-
Oj.default_options = { :mode => :object }
|
129
|
+
Oj.default_options = { :mode => :object, :time_format => :unix_zone }
|
130
130
|
dump_and_load(t, false)
|
131
131
|
end
|
132
132
|
def test_time_object_early
|
133
133
|
t = Time.xmlschema("1954-01-05T00:00:00.123456")
|
134
|
-
Oj.default_options = { :mode => :object }
|
134
|
+
Oj.default_options = { :mode => :object, :time_format => :unix_zone }
|
135
135
|
dump_and_load(t, false)
|
136
136
|
end
|
137
137
|
|
@@ -227,7 +227,18 @@ class FileJuice < Minitest::Test
|
|
227
227
|
}
|
228
228
|
puts "\n*** file: '#{File.read(filename)}'" if trace
|
229
229
|
loaded = Oj.load_file(filename)
|
230
|
-
|
230
|
+
if obj.is_a?(Time) && loaded.is_a?(Time)
|
231
|
+
assert_equal(obj.tv_sec, loaded.tv_sec)
|
232
|
+
if obj.respond_to?(:tv_nsec)
|
233
|
+
assert_equal(obj.tv_nsec, loaded.tv_nsec)
|
234
|
+
else
|
235
|
+
assert_equal(obj.tv_usec, loaded.tv_usec)
|
236
|
+
end
|
237
|
+
assert_equal(obj.utc?, loaded.utc?)
|
238
|
+
assert_equal(obj.utc_offset, loaded.utc_offset)
|
239
|
+
else
|
240
|
+
assert_equal(obj, loaded)
|
241
|
+
end
|
231
242
|
loaded
|
232
243
|
end
|
233
244
|
|
data/test/test_object.rb
CHANGED
@@ -346,32 +346,200 @@ class ObjectJuice < Minitest::Test
|
|
346
346
|
dump_and_load(obj, false)
|
347
347
|
end
|
348
348
|
|
349
|
-
def test_time
|
350
|
-
t = Time.now()
|
351
|
-
dump_and_load(t, false)
|
352
|
-
end
|
353
|
-
|
354
349
|
def test_xml_time
|
355
|
-
|
356
|
-
|
357
|
-
|
350
|
+
if RUBY_VERSION.start_with?('1.8')
|
351
|
+
t = Time.parse('2015-01-05T21:37:07.123456789-08:00')
|
352
|
+
else
|
353
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
|
354
|
+
end
|
355
|
+
# The fractional seconds are not always recreated exactly which causes a
|
356
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
357
|
+
# separately along with utc.
|
358
|
+
json = Oj.dump(t, :mode => :object, :time_format => :xmlschema)
|
359
|
+
#puts "*** json for test_xml_time '#{json}'"
|
360
|
+
loaded = Oj.object_load(json);
|
361
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
362
|
+
if t.respond_to?(:tv_nsec)
|
363
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
364
|
+
else
|
365
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
366
|
+
end
|
367
|
+
assert_equal(t.utc?, loaded.utc?)
|
368
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
358
369
|
end
|
359
370
|
|
360
|
-
def
|
361
|
-
|
362
|
-
|
363
|
-
|
371
|
+
def test_xml_time_utc
|
372
|
+
if RUBY_VERSION.start_with?('1.8')
|
373
|
+
t = Time.parse('2015-01-05T21:37:07.123456789Z')
|
374
|
+
else
|
375
|
+
t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
|
376
|
+
end
|
377
|
+
# The fractional seconds are not always recreated exactly which causes a
|
378
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
379
|
+
# separately along with utc.
|
380
|
+
json = Oj.dump(t, :mode => :object, :time_format => :xmlschema)
|
381
|
+
loaded = Oj.object_load(json);
|
382
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
383
|
+
if t.respond_to?(:tv_nsec)
|
384
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
385
|
+
else
|
386
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
387
|
+
end
|
388
|
+
assert_equal(t.utc?, loaded.utc?)
|
389
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
364
390
|
end
|
365
391
|
|
366
392
|
def test_ruby_time
|
367
|
-
|
368
|
-
|
369
|
-
|
393
|
+
if RUBY_VERSION.start_with?('1.8')
|
394
|
+
t = Time.parse('2015-01-05T21:37:07.123456789-08:00')
|
395
|
+
else
|
396
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
|
397
|
+
end
|
398
|
+
# The fractional seconds are not always recreated exactly which causes a
|
399
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
400
|
+
# separately along with utc.
|
401
|
+
json = Oj.dump(t, :mode => :object, :time_format => :ruby)
|
402
|
+
#puts "*** json for test_ruby_time '#{json}'"
|
403
|
+
loaded = Oj.object_load(json);
|
404
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
405
|
+
if t.respond_to?(:tv_nsec)
|
406
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
407
|
+
else
|
408
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
409
|
+
end
|
410
|
+
assert_equal(t.utc?, loaded.utc?)
|
411
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_ruby_time_12345
|
415
|
+
if RUBY_VERSION.start_with?('1.8')
|
416
|
+
t = Time.parse('2015-01-05T21:37:07.123456789+03:25')
|
417
|
+
else
|
418
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, 12345/60*60)
|
419
|
+
end
|
420
|
+
# The fractional seconds are not always recreated exactly which causes a
|
421
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
422
|
+
# separately along with utc.
|
423
|
+
json = Oj.dump(t, :mode => :object, :time_format => :ruby)
|
424
|
+
#puts "*** json for test_ruby_time '#{json}'"
|
425
|
+
loaded = Oj.object_load(json);
|
426
|
+
#puts "*** loaded: #{loaded}"
|
427
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
428
|
+
if t.respond_to?(:tv_nsec)
|
429
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
430
|
+
else
|
431
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
432
|
+
end
|
433
|
+
assert_equal(t.utc?, loaded.utc?)
|
434
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
435
|
+
end
|
436
|
+
|
437
|
+
def test_ruby_time_utc
|
438
|
+
if RUBY_VERSION.start_with?('1.8')
|
439
|
+
t = Time.parse('2015-01-05T21:37:07.123456789Z')
|
440
|
+
else
|
441
|
+
t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
|
442
|
+
end
|
443
|
+
# The fractional seconds are not always recreated exactly which causes a
|
444
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
445
|
+
# separately along with utc.
|
446
|
+
json = Oj.dump(t, :mode => :object, :time_format => :ruby)
|
447
|
+
#puts json
|
448
|
+
loaded = Oj.object_load(json);
|
449
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
450
|
+
if t.respond_to?(:tv_nsec)
|
451
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
452
|
+
else
|
453
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
454
|
+
end
|
455
|
+
assert_equal(t.utc?, loaded.utc?)
|
456
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
370
457
|
end
|
371
458
|
|
372
459
|
def test_time_early
|
373
|
-
|
374
|
-
|
460
|
+
if RUBY_VERSION.start_with?('1.8')
|
461
|
+
t = Time.parse('1954-01-05T21:37:07.123456789-08:00')
|
462
|
+
else
|
463
|
+
t = Time.new(1954, 1, 5, 21, 37, 7.123456789, -8 * 3600)
|
464
|
+
end
|
465
|
+
# The fractional seconds are not always recreated exactly which causes a
|
466
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
467
|
+
# separately along with utc.
|
468
|
+
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
|
469
|
+
#puts json
|
470
|
+
loaded = Oj.object_load(json);
|
471
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
472
|
+
if t.respond_to?(:tv_nsec)
|
473
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
474
|
+
else
|
475
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
476
|
+
end
|
477
|
+
assert_equal(t.utc?, loaded.utc?)
|
478
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
479
|
+
end
|
480
|
+
|
481
|
+
def test_time_unix_zone
|
482
|
+
if RUBY_VERSION.start_with?('1.8')
|
483
|
+
t = Time.parse('2015-01-05T21:37:07.123456789-08:00')
|
484
|
+
else
|
485
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
|
486
|
+
end
|
487
|
+
# The fractional seconds are not always recreated exactly which causes a
|
488
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
489
|
+
# separately along with utc.
|
490
|
+
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
|
491
|
+
#puts json
|
492
|
+
loaded = Oj.object_load(json);
|
493
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
494
|
+
if t.respond_to?(:tv_nsec)
|
495
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
496
|
+
else
|
497
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
498
|
+
end
|
499
|
+
assert_equal(t.utc?, loaded.utc?)
|
500
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
501
|
+
end
|
502
|
+
|
503
|
+
unless RUBY_VERSION.start_with?('1.8')
|
504
|
+
def test_time_unix_zone_12345
|
505
|
+
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, 12345)
|
506
|
+
# The fractional seconds are not always recreated exactly which causes a
|
507
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
508
|
+
# separately along with utc.
|
509
|
+
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
|
510
|
+
#puts json
|
511
|
+
loaded = Oj.object_load(json);
|
512
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
513
|
+
if t.respond_to?(:tv_nsec)
|
514
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
515
|
+
else
|
516
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
517
|
+
end
|
518
|
+
assert_equal(t.utc?, loaded.utc?)
|
519
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
520
|
+
end
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_time_unix_zone_utc
|
524
|
+
if RUBY_VERSION.start_with?('1.8')
|
525
|
+
t = Time.parse('2015-01-05T21:37:07.123456789Z')
|
526
|
+
else
|
527
|
+
t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
|
528
|
+
end
|
529
|
+
# The fractional seconds are not always recreated exactly which causes a
|
530
|
+
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
|
531
|
+
# separately along with utc.
|
532
|
+
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
|
533
|
+
#puts json
|
534
|
+
loaded = Oj.object_load(json);
|
535
|
+
assert_equal(t.tv_sec, loaded.tv_sec)
|
536
|
+
if t.respond_to?(:tv_nsec)
|
537
|
+
assert_equal(t.tv_nsec, loaded.tv_nsec)
|
538
|
+
else
|
539
|
+
assert_equal(t.tv_usec, loaded.tv_usec)
|
540
|
+
end
|
541
|
+
assert_equal(t.utc?, loaded.utc?)
|
542
|
+
assert_equal(t.utc_offset, loaded.utc_offset)
|
375
543
|
end
|
376
544
|
|
377
545
|
def test_json_object
|