oj 3.13.6 → 3.13.10

Sign up to get free protection for your applications and to get access to all the features.
data/ext/oj/usual.c CHANGED
@@ -537,7 +537,7 @@ static void add_float_key(ojParser p) {
537
537
  static void add_float_as_big(ojParser p) {
538
538
  char buf[64];
539
539
 
540
- // fails on ubuntu
540
+ // snprintf fails on ubuntu and macOS for long double
541
541
  // snprintf(buf, sizeof(buf), "%Lg", p->num.dub);
542
542
  sprintf(buf, "%Lg", p->num.dub);
543
543
  push(p, rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new2(buf)));
@@ -546,7 +546,9 @@ static void add_float_as_big(ojParser p) {
546
546
  static void add_float_as_big_key(ojParser p) {
547
547
  char buf[64];
548
548
 
549
- snprintf(buf, sizeof(buf), "%Lg", p->num.dub);
549
+ // snprintf fails on ubuntu and macOS for long double
550
+ // snprintf(buf, sizeof(buf), "%Lg", p->num.dub);
551
+ sprintf(buf, "%Lg", p->num.dub);
550
552
  push_key(p);
551
553
  push2(p, rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new2(buf)));
552
554
  }
data/ext/oj/wab.c CHANGED
@@ -128,7 +128,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
128
128
  for (i = 0; i <= cnt; i++) {
129
129
  assure_size(out, size);
130
130
  fill_indent(out, d2);
131
- oj_dump_wab_val(rb_ary_entry(a, i), d2, out);
131
+ oj_dump_wab_val(RARRAY_AREF(a, i), d2, out);
132
132
  if (i < cnt) {
133
133
  *out->cur++ = ',';
134
134
  }
data/lib/oj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.13.6'
4
+ VERSION = '3.13.10'
5
5
  end
data/pages/JsonGem.md CHANGED
@@ -1,3 +1,18 @@
1
+ # JSON Quickstart
2
+
3
+ To have Oj universally "take over" many methods on the JSON constant (`load`, `parse`, etc.) with
4
+ their faster Oj counterparts, in a mode that is compatible with the json gem:
5
+
6
+ ```ruby
7
+ Oj.mimic_JSON()
8
+ ```
9
+
10
+ If the project does not already use the json gem, `JSON` will become available.
11
+ If the project does require the json gem, `Oj.mimic_JSON()` should be invoked after the
12
+ json gem has been required.
13
+
14
+ For more details and options, read on...
15
+
1
16
  # Oj JSON Gem Compatibility
2
17
 
3
18
  The `:compat` mode mimics the json gem. The json gem is built around the use
data/pages/Modes.md CHANGED
@@ -39,7 +39,8 @@ if a non-native type is encountered instead of raising an Exception.
39
39
  The `:compat` mode mimics the json gem. The json gem is built around the use
40
40
  of the `to_json(*)` method defined for a class. Oj attempts to provide the
41
41
  same functionality by being a drop in replacement with a few
42
- exceptions. [{file:JsonGem.md}](JsonGem.md) includes more details on
42
+ exceptions. To universally replace many `JSON` methods with their faster Oj counterparts,
43
+ simply run `Oj.mimic_json`. [{file:JsonGem.md}](JsonGem.md) includes more details on
43
44
  compatibility and use.
44
45
 
45
46
  ## :rails Mode
@@ -108,11 +109,11 @@ information.
108
109
  | :float_precision | Fixnum | x | x | | | | x | |
109
110
  | :hash_class | Class | | | x | x | | x | |
110
111
  | :ignore | Array | | | | | x | x | |
111
- | :indent | Integer | x | x | 3 | 4 | x | x | x |
112
+ | :indent | Integer | x | x | 4 | 4 | x | x | x |
112
113
  | :indent_str | String | | | x | x | | x | |
113
114
  | :integer_range | Range | x | x | x | x | x | x | x |
114
115
  | :match_string | Hash | | | x | x | | x | |
115
- | :max_nesting | Fixnum | 4 | 4 | x | | 5 | 4 | |
116
+ | :max_nesting | Fixnum | 5 | 5 | x | | 5 | 5 | |
116
117
  | :mode | Symbol | - | - | - | - | - | - | |
117
118
  | :nan | Symbol | | | | | | x | |
118
119
  | :nilnil | Boolean | | | | | | x | |
@@ -140,6 +141,8 @@ information.
140
141
  3. By default the bigdecimal_as decimal is not set and the default encoding
141
142
  for Rails is as a string. Setting the value to true will encode a
142
143
  BigDecimal as a number which breaks compatibility.
144
+ Note: after version 3.11.3 both `Oj.generate` and `JSON.generate`
145
+ will not honour this option in Rails Mode, detais on https://github.com/ohler55/oj/pull/716.
143
146
 
144
147
  4. The integer indent value in the default options will be honored by since
145
148
  the json gem expects a String type the indent in calls to 'to_json()',
data/pages/Rails.md CHANGED
@@ -1,3 +1,15 @@
1
+ # Rails Quickstart
2
+
3
+ To universally replace Rails' use of the json gem with Oj, and also
4
+ have Oj "take over" many methods on the JSON constant (`load`, `parse`, etc.) with
5
+ their faster Oj counterparts, add this to an initializer:
6
+
7
+ ```ruby
8
+ Oj.optimize_rails()
9
+ ```
10
+
11
+ For more details and options, read on...
12
+
1
13
  # Oj Rails Compatibility
2
14
 
3
15
  The `:rails` mode mimics the ActiveSupport version 5 encoder. Rails and
data/test/bug.rb ADDED
@@ -0,0 +1,16 @@
1
+ $: << '.'
2
+ $: << File.join(File.dirname(__FILE__), "../lib")
3
+ $: << File.join(File.dirname(__FILE__), "../ext")
4
+
5
+
6
+ #require 'bundler/setup'
7
+ require 'oj'
8
+ require 'active_support'
9
+ require 'active_support/time_with_zone'
10
+ require 'tzinfo'
11
+
12
+ puts ActiveSupport::TimeWithZone
13
+
14
+ json = File.read('./bug.json')
15
+
16
+ Oj.load(json)
data/test/foo.rb CHANGED
@@ -1,13 +1,35 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ $: << '.'
4
+ $: << File.join(File.dirname(__FILE__), "../lib")
5
+ $: << File.join(File.dirname(__FILE__), "../ext")
6
+
7
+ require 'rails'
3
8
  require 'oj'
4
9
 
5
- Oj::default_options = {cache_str: 0, cache_keys: true, mode: :strict}
10
+ $data = {:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d}
11
+
12
+ def encode
13
+ p "JSON.generate: #{JSON.generate($data)}"
14
+ p "Oj.generate: #{Oj.generate($data)}"
15
+ p "Oj.dump: #{Oj.dump($data)}"
16
+ p "to_json: #{$data.to_json}"
17
+ p "ActiveSupport::JSON.encode: #{ActiveSupport::JSON.encode($data)}"
18
+ end
19
+
20
+ puts "With Oj version (#{Oj::VERSION})"
6
21
 
7
- puts "Ruby version: #{RUBY_VERSION}"
8
- puts "Oj version: #{Oj::VERSION}"
22
+ puts
23
+ puts "Before optimizing"
24
+ encode
9
25
 
10
- puts "cache_keys: #{Oj::default_options[:cache_keys]}"
11
- puts "cache_str: #{Oj::default_options[:cache_str]}"
26
+ Oj.optimize_rails
27
+ Oj.default_options = {
28
+ mode: :rails,
29
+ bigdecimal_as_decimal: true,
30
+ bigdecimal_load: true
31
+ }
12
32
 
13
- Oj.load('{"":""}').each_pair {|k,v| puts "k.frozen?: #{k.frozen?}\nv.frozen?: #{v.frozen?}"}
33
+ puts
34
+ puts "After optimizing"
35
+ encode
data/test/test_fast.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: utf-8
2
+ # coding: utf-8
3
+ # frozen_string_literal: true
3
4
 
4
5
  $: << File.dirname(__FILE__)
5
6
 
@@ -36,6 +37,17 @@ class DocTest < Minitest::Test
36
37
  end
37
38
  end
38
39
 
40
+ def test_leaf_of_existing_path
41
+ json = %{{"foo": 1, "fizz": true}}
42
+ Oj::Doc.open(json) do |doc|
43
+ %w(/foo/bar /fizz/bar).each do |path|
44
+ assert_nil(doc.fetch(path))
45
+ assert_equal(:default, doc.fetch(path, :default))
46
+ refute(doc.exists?(path))
47
+ end
48
+ end
49
+ end
50
+
39
51
  def test_true
40
52
  json = %{true}
41
53
  Oj::Doc.open(json) do |doc|
@@ -282,11 +294,11 @@ class DocTest < Minitest::Test
282
294
  ['/nothing', nil],
283
295
  ['/array/10', nil],
284
296
  ].each do |path,val|
285
- if val.nil?
286
- assert_nil(doc.fetch(path))
287
- else
297
+ if val.nil?
298
+ assert_nil(doc.fetch(path))
299
+ else
288
300
  assert_equal(val, doc.fetch(path))
289
- end
301
+ end
290
302
  end
291
303
  end
292
304
  # verify empty hash and arrays return nil when a member is requested
@@ -313,7 +325,7 @@ class DocTest < Minitest::Test
313
325
  end
314
326
  end
315
327
 
316
- def test_exisits
328
+ def test_exists
317
329
  Oj::Doc.open(@json1) do |doc|
318
330
  [['/array/1', true],
319
331
  ['/array/1', true],
@@ -322,7 +334,7 @@ class DocTest < Minitest::Test
322
334
  ['/array/3', false],
323
335
  ['/nothing', false],
324
336
  ].each do |path,val|
325
- assert_equal(val, doc.exists?(path))
337
+ assert_equal(val, doc.exists?(path), "failed for #{path.inspect}")
326
338
  end
327
339
  end
328
340
  end
@@ -384,6 +396,19 @@ class DocTest < Minitest::Test
384
396
  end
385
397
  end
386
398
 
399
+ def test_nested_each_child
400
+ h = {}
401
+ Oj::Doc.open('{"a":1,"c":[2],"d":3}') do |doc|
402
+ doc.each_child('/') do |child|
403
+ h[child.path] = child.fetch
404
+ child.each_child do |grandchild|
405
+ h[grandchild.path] = grandchild.fetch
406
+ end
407
+ end
408
+ end
409
+ assert_equal({"/a"=>1, "/c"=>[2], "/c/1"=>2, "/d"=>3}, h)
410
+ end
411
+
387
412
  def test_size
388
413
  Oj::Doc.open('[1,2,3]') do |doc|
389
414
  assert_equal(4, doc.size)
@@ -480,6 +505,11 @@ class DocTest < Minitest::Test
480
505
  assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
481
506
  end
482
507
 
508
+ def test_doc_empty
509
+ result = Oj::Doc.open("") { |doc| doc.each_child {} }
510
+ assert_nil(result)
511
+ end
512
+
483
513
  def test_comment
484
514
  json = %{{
485
515
  "x"/*one*/:/*two*/true,//three
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: 3.13.6
4
+ version: 3.13.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-11 00:00:00.000000000 Z
11
+ date: 2021-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -201,6 +201,7 @@ files:
201
201
  - test/activesupport6/time_zone_test_helpers.rb
202
202
  - test/bar.rb
203
203
  - test/baz.rb
204
+ - test/bug.rb
204
205
  - test/files.rb
205
206
  - test/foo.rb
206
207
  - test/helper.rb
@@ -332,6 +333,7 @@ test_files:
332
333
  - test/activesupport6/time_zone_test_helpers.rb
333
334
  - test/bar.rb
334
335
  - test/baz.rb
336
+ - test/bug.rb
335
337
  - test/files.rb
336
338
  - test/foo.rb
337
339
  - test/helper.rb