oj 2.13.1 → 2.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,6 +46,7 @@ extern "C" {
46
46
  #endif
47
47
 
48
48
  #include "stdint.h"
49
+ #include "stdbool.h"
49
50
 
50
51
  #if USE_PTHREAD_MUTEX
51
52
  #include <pthread.h>
@@ -114,11 +115,12 @@ typedef enum {
114
115
  } StreamWriterType;
115
116
 
116
117
  typedef struct _DumpOpts {
117
- const char *indent;
118
- const char *before_sep;
119
- const char *after_sep;
120
- const char *hash_nl;
121
- const char *array_nl;
118
+ bool use;
119
+ char indent_str[16];
120
+ char before_sep[16];
121
+ char after_sep[16];
122
+ char hash_nl[16];
123
+ char array_nl[16];
122
124
  uint8_t indent_size;
123
125
  uint8_t before_size;
124
126
  uint8_t after_size;
@@ -127,26 +129,26 @@ typedef struct _DumpOpts {
127
129
  } *DumpOpts;
128
130
 
129
131
  typedef struct _Options {
130
- int indent; // indention for dump, default 2
131
- char circular; // YesNo
132
- char auto_define; // YesNo
133
- char sym_key; // YesNo
134
- char escape_mode; // Escape_Mode
135
- char mode; // Mode
136
- char class_cache; // YesNo
137
- char time_format; // TimeFormat
138
- char bigdec_as_num; // YesNo
139
- char bigdec_load; // BigLoad
140
- char to_json; // YesNo
141
- char nilnil; // YesNo
142
- char allow_gc; // allow GC during parse
143
- char quirks_mode; // allow single JSON values instead of documents
144
- const char *create_id; // 0 or string
145
- size_t create_id_len; // length of create_id
146
- int sec_prec; // second precision when dumping time
147
- DumpOpts dump_opts;
148
- char float_prec; // float precision, linked to float_fmt
149
- char float_fmt[7]; // float format for dumping, if empty use Ruby
132
+ int indent; // indention for dump, default 2
133
+ char circular; // YesNo
134
+ char auto_define; // YesNo
135
+ char sym_key; // YesNo
136
+ char escape_mode; // Escape_Mode
137
+ char mode; // Mode
138
+ char class_cache; // YesNo
139
+ char time_format; // TimeFormat
140
+ char bigdec_as_num; // YesNo
141
+ char bigdec_load; // BigLoad
142
+ char to_json; // YesNo
143
+ char nilnil; // YesNo
144
+ char allow_gc; // allow GC during parse
145
+ char quirks_mode; // allow single JSON values instead of documents
146
+ const char *create_id; // 0 or string
147
+ size_t create_id_len; // length of create_id
148
+ int sec_prec; // second precision when dumping time
149
+ char float_prec; // float precision, linked to float_fmt
150
+ char float_fmt[7]; // float format for dumping, if empty use Ruby
151
+ struct _DumpOpts dump_opts;
150
152
  } *Options;
151
153
 
152
154
  typedef struct _Out {
@@ -720,16 +720,26 @@ oj_num_as_value(NumInfo ni) {
720
720
  double d = (double)ni->i * (double)ni->div + (double)ni->num;
721
721
  int x = ni->exp - ni->di;
722
722
 
723
- d = round(d);
724
- if (0 < x) {
725
- d *= pow(10.0L, x);
726
- } else if (0 > x) {
727
- d /= pow(10.0L, -x);
728
- }
729
- if (ni->neg) {
730
- d = -d;
723
+ // Rounding sometimes cuts off the last digit even if there are only
724
+ // 15 digits. This attempts to fix those few cases where this
725
+ // occurs.
726
+ if ((double)INT64_MAX > d && (int64_t)d != (ni->i * ni->div + ni->num)) {
727
+ rnum = rb_funcall(oj_bigdecimal_class, oj_new_id, 1, rb_str_new(ni->str, ni->len));
728
+ if (ni->no_big) {
729
+ rnum = rb_funcall(rnum, rb_intern("to_f"), 0);
730
+ }
731
+ } else {
732
+ d = round(d);
733
+ if (0 < x) {
734
+ d *= pow(10.0L, x);
735
+ } else if (0 > x) {
736
+ d /= pow(10.0L, -x);
737
+ }
738
+ if (ni->neg) {
739
+ d = -d;
740
+ }
741
+ rnum = rb_float_new(d);
731
742
  }
732
- rnum = rb_float_new(d);
733
743
  }
734
744
  }
735
745
  return rnum;
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '2.13.1'
4
+ VERSION = '2.14.0'
5
5
  end
@@ -115,27 +115,10 @@ class Juice < Minitest::Test
115
115
  end
116
116
  =end
117
117
  def test_set_options
118
- orig ={
119
- :indent=>0,
120
- :second_precision=>9,
121
- :circular=>false,
122
- :class_cache=>true,
123
- :auto_define=>false,
124
- :symbol_keys=>false,
125
- :bigdecimal_as_decimal=>true,
126
- :use_to_json=>true,
127
- :nilnil=>false,
128
- :allow_gc=>true,
129
- :quirks_mode=>true,
130
- :float_precision=>15,
131
- :mode=>:object,
132
- :escape_mode=>:json,
133
- :time_format=>:unix,
134
- :bigdecimal_load=>:auto,
135
- :create_id=>'json_class'}
136
- o2 = {
137
- :indent=>4,
138
- :second_precision=>7,
118
+ orig = Oj.default_options()
119
+ alt ={
120
+ :indent=>" - ",
121
+ :second_precision=>5,
139
122
  :circular=>true,
140
123
  :class_cache=>false,
141
124
  :auto_define=>true,
@@ -144,19 +127,26 @@ class Juice < Minitest::Test
144
127
  :use_to_json=>false,
145
128
  :nilnil=>true,
146
129
  :allow_gc=>false,
147
- :quirks_mode=>true,
148
- :float_precision=>15,
149
- :mode=>:compat,
150
- :escape_mode=>:json,
151
- :time_format=>:ruby,
152
- :bigdecimal_load=>:bigdecimal,
153
- :create_id=>nil}
154
- o3 = { :indent => 4 }
155
- Oj.default_options = o2
130
+ :quirks_mode=>false,
131
+ :float_precision=>13,
132
+ :mode=>:strict,
133
+ :escape_mode=>:ascii,
134
+ :time_format=>:unix_zone,
135
+ :bigdecimal_load=>:float,
136
+ :create_id=>'classy',
137
+ :space=>'z',
138
+ :array_nl=>'a',
139
+ :object_nl=>'o',
140
+ :space_before=>'b',
141
+ }
142
+ Oj.default_options = alt
156
143
  opts = Oj.default_options()
157
- assert_equal(o2, opts);
158
- Oj.default_options = o3 # see if it throws an exception
144
+ assert_equal(alt, opts);
145
+
159
146
  Oj.default_options = orig # return to original
147
+ # verify back to original
148
+ opts = Oj.default_options()
149
+ assert_equal(orig, opts);
160
150
  end
161
151
 
162
152
  def test_nil
@@ -260,6 +250,31 @@ class Juice < Minitest::Test
260
250
  assert_equal(json, json2)
261
251
  end
262
252
 
253
+ def test_dump_options
254
+ json = Oj.dump({ 'a' => 1, 'b' => [true, false]},
255
+ :mode => :compat,
256
+ :indent => "--",
257
+ :array_nl => "\n",
258
+ :object_nl => "#\n",
259
+ :space => "*",
260
+ :space_before => "~")
261
+ assert(%{{#
262
+ --"a"~:*1,#
263
+ --"b"~:*[
264
+ ----true,
265
+ ----false
266
+ --]#
267
+ }} == json ||
268
+ %{{#
269
+ --"b"~:*[
270
+ ----true,
271
+ ----false
272
+ --],#
273
+ --"a"~:*1#
274
+ }} == json)
275
+
276
+ end
277
+
263
278
  def test_null_char
264
279
  assert_raises(Oj::ParseError) { Oj.load("\"\0\"") }
265
280
  assert_raises(Oj::ParseError) { Oj.load("\"\\\0\"") }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.13.1
4
+ version: 2.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-16 00:00:00.000000000 Z
11
+ date: 2015-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -161,7 +161,6 @@ files:
161
161
  - test/test_various.rb
162
162
  - test/test_writer.rb
163
163
  - test/write_timebars.rb
164
- - test/zip.rb
165
164
  homepage: http://www.ohler.com/oj
166
165
  licenses:
167
166
  - MIT
@@ -248,5 +247,4 @@ test_files:
248
247
  - test/test_various.rb
249
248
  - test/test_writer.rb
250
249
  - test/write_timebars.rb
251
- - test/zip.rb
252
250
  has_rdoc: true
@@ -1,34 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
-
8
- require 'zlib'
9
-
10
- File.open('test.json.gz', 'r') do |file|
11
- Zlib::GzipReader.wrap(file) do |f2|
12
- puts "*** f2: #{f2}"
13
- Oj.load(f2) do |val|
14
- puts val
15
- end
16
- end
17
- end
18
-
19
- =begin
20
- And a json file with the following contents (then gzipped):
21
-
22
- {"a":2}
23
- {"b":2}
24
- The output is:
25
-
26
- {"a"=>2}
27
- {"b"=>2}
28
- bin/test:8:in `load': undefined method `new' for #<EOFError: end of file reached> (NoMethodError)
29
- from bin/test:8:in `block (2 levels) in <main>'
30
- from bin/test:7:in `wrap'
31
- from bin/test:7:in `block in <main>'
32
- from bin/test:6:in `open'
33
- from bin/test:6:in `<main>'
34
- =end