oj 2.0.2 → 2.0.3

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.

Files changed (5) hide show
  1. data/README.md +2 -10
  2. data/ext/oj/dump.c +20 -6
  3. data/lib/oj/version.rb +1 -1
  4. data/test/tests.rb +22 -0
  5. metadata +2 -2
data/README.md CHANGED
@@ -32,17 +32,9 @@ A fast JSON parser and Object marshaller as a Ruby gem.
32
32
 
33
33
  ## <a name="release">Release Notes</a>
34
34
 
35
- ### Release 2.0.2
35
+ ### Release 2.0.3
36
36
 
37
- - Fixed bug in Oj.load where loading a hash with symbold keys and also turning on symbolize keys would try to symbolize a symbol.
38
-
39
- ### Release 2.0.1
40
-
41
- - BigDecimals now dump to a string in compat mode thanks to cgriego.
42
-
43
- - High precision time (nano time) can be turned off for better compatibility with other JSON parsers.
44
-
45
- - Times before 1970 now encode correctly.
37
+ - Fixed round off error in time format when rounding up to the next second.
46
38
 
47
39
  ## <a name="description">Description</a>
48
40
 
@@ -929,6 +929,7 @@ dump_time(VALUE obj, Out out) {
929
929
  long size;
930
930
  char *dot = b - 10;
931
931
  int neg = 0;
932
+ long one = 1000000000;
932
933
  #if HAS_RB_TIME_TIMESPEC
933
934
  struct timespec ts = rb_time_timespec(obj);
934
935
  time_t sec = ts.tv_sec;
@@ -960,8 +961,13 @@ dump_time(VALUE obj, Out out) {
960
961
  for (i = 9 - out->opts->sec_prec; 0 < i; i--) {
961
962
  dot++;
962
963
  nsec = (nsec + 5) / 10;
964
+ one /= 10;
963
965
  }
964
966
  }
967
+ if (one <= nsec) {
968
+ nsec -= one;
969
+ sec++;
970
+ }
965
971
  for (; dot < b; b--, nsec /= 10) {
966
972
  *b = '0' + (nsec % 10);
967
973
  }
@@ -998,6 +1004,7 @@ static void
998
1004
  dump_xml_time(VALUE obj, Out out) {
999
1005
  char buf[64];
1000
1006
  struct tm *tm;
1007
+ long one = 1000000000;
1001
1008
  #if HAS_RB_TIME_TIMESPEC
1002
1009
  struct timespec ts = rb_time_timespec(obj);
1003
1010
  time_t sec = ts.tv_sec;
@@ -1017,6 +1024,18 @@ dump_xml_time(VALUE obj, Out out) {
1017
1024
  if (out->end - out->cur <= 36) {
1018
1025
  grow(out, 36);
1019
1026
  }
1027
+ if (9 > out->opts->sec_prec) {
1028
+ int i;
1029
+
1030
+ for (i = 9 - out->opts->sec_prec; 0 < i; i--) {
1031
+ nsec = (nsec + 5) / 10;
1032
+ one /= 10;
1033
+ }
1034
+ if (one <= nsec) {
1035
+ nsec -= one;
1036
+ sec++;
1037
+ }
1038
+ }
1020
1039
  // 2012-01-05T23:58:07.123456000+09:00
1021
1040
  //tm = localtime(&sec);
1022
1041
  sec += tz_secs;
@@ -1058,13 +1077,8 @@ dump_xml_time(VALUE obj, Out out) {
1058
1077
  int len = 35;
1059
1078
 
1060
1079
  if (9 > out->opts->sec_prec) {
1061
- int i;
1062
-
1063
1080
  format[32] = '0' + out->opts->sec_prec;
1064
- for (i = 9 - out->opts->sec_prec; 0 < i; i--) {
1065
- nsec = (nsec + 5) / 10;
1066
- len--;
1067
- }
1081
+ len -= 9 - out->opts->sec_prec;
1068
1082
  }
1069
1083
  sprintf(buf, format,
1070
1084
  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '2.0.2'
4
+ VERSION = '2.0.3'
5
5
  end
@@ -273,6 +273,9 @@ class Juice < ::Test::Unit::TestCase
273
273
  #t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
274
274
  json = Oj.dump(t, :mode => :compat, :second_precision => 5)
275
275
  assert_equal(%{1325775487.12346}, json)
276
+ t = Time.xmlschema("2012-01-05T23:58:07.999600+09:00")
277
+ json = Oj.dump(t, :mode => :compat, :second_precision => 3)
278
+ assert_equal(%{1325775488.000}, json)
276
279
  end
277
280
  def test_unix_time_compat_early
278
281
  t = Time.xmlschema("1954-01-05T00:00:00.123456789+00:00")
@@ -347,6 +350,25 @@ class Juice < ::Test::Unit::TestCase
347
350
  assert_equal(%{"2012-01-05T23:58:07.12346%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
348
351
  end
349
352
  end
353
+ def test_xml_time_compat_precision_round
354
+ begin
355
+ t = Time.new(2012, 1, 5, 23, 58, 7.9996, 32400)
356
+ json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 3)
357
+ assert_equal(%{"2012-01-05T23:58:08.000+09:00"}, json)
358
+ rescue Exception
359
+ # some Rubies (1.8.7) do not allow the timezome to be set
360
+ t = Time.local(2012, 1, 5, 23, 58, 7, 999600)
361
+ json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 3)
362
+ tz = t.utc_offset
363
+ # Ruby does not handle a %+02d properly so...
364
+ sign = '+'
365
+ if 0 > tz
366
+ sign = '-'
367
+ tz = -tz
368
+ end
369
+ assert_equal(%{"2012-01-05T23:58:08%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
370
+ end
371
+ end
350
372
  def test_xml_time_compat_zulu
351
373
  begin
352
374
  t = Time.new(2012, 1, 5, 23, 58, 7.0, 0)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-23 00:00:00.000000000 Z
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'The fastest JSON parser and object serializer. '
15
15
  email: peter@ohler.com