oj 3.13.8 → 3.13.11

Sign up to get free protection for your applications and to get access to all the features.
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/foo.rb CHANGED
@@ -4,11 +4,74 @@ $: << '.'
4
4
  $: << File.join(File.dirname(__FILE__), "../lib")
5
5
  $: << File.join(File.dirname(__FILE__), "../ext")
6
6
 
7
- require 'oj'
7
+ require "oj"
8
+ require "socket"
9
+ require 'io/nonblock'
8
10
 
9
- parser = Oj::Parser.new(:usual, cache_keys: true, cache_strings: 30, decimal: :bigdecimal, symbol_keys: false)
10
- raw = %({"amount":0.10})
11
- parsed = parser.parse(raw)
11
+ =begin
12
+ #pid = spawn("nc -d 0.1 -l 5000", out: "/dev/null")
13
+ pid = spawn("nc -i 1 -l 7777", out: "/dev/null")
14
+ at_exit { Process.kill 9, pid }
15
+ sleep 0.2
16
+ s = Socket.tcp("localhost", 7777)
17
+ #s.nonblock = false
18
+ 1_000_000.times do |x|
19
+ Oj.to_stream(s, { x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]})
20
+ end
21
+ =end
12
22
 
13
- pp parsed
14
- puts parsed['amount'].class
23
+ =begin
24
+ IO.pipe do |r, w|
25
+ if fork
26
+ r.close
27
+ #w.nonblock = false
28
+ 1_000_000.times do |i|
29
+ begin
30
+ Oj.to_stream(w, { x: i})
31
+ rescue IOError => e
32
+ puts "*** #{i} raised #{e.class}: #{e}"
33
+ IO.select(nil, [w])
34
+ retry
35
+ end
36
+ w.puts
37
+ end
38
+ else
39
+ w.close
40
+ sleep(0.1)
41
+ r.each_line { |b|
42
+ #print b
43
+ }
44
+ r.close
45
+ Process.exit(0)
46
+ end
47
+ end
48
+ =end
49
+
50
+ IO.pipe do |r, w|
51
+ if fork
52
+ r.close
53
+ #w.nonblock = false
54
+ a = []
55
+ 10_000.times do |i|
56
+ a << i
57
+ end
58
+ begin
59
+ Oj.to_stream(w, a, indent: 2)
60
+ rescue IOError => e
61
+ puts "*** raised #{e.class}: #{e}"
62
+ puts "*** fileno: #{w.fileno}"
63
+ puts "*** is an IO?: #{w.kind_of?(IO)}"
64
+ IO.select(nil, [w])
65
+ retry
66
+ end
67
+ w.puts
68
+ else
69
+ w.close
70
+ sleep(0.5)
71
+ r.each_line { |b|
72
+ #print b
73
+ }
74
+ r.close
75
+ Process.exit(0)
76
+ end
77
+ end
data/test/test_fast.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # coding: utf-8
2
3
  # frozen_string_literal: true
3
4
 
4
5
  $: << File.dirname(__FILE__)
@@ -395,6 +396,19 @@ class DocTest < Minitest::Test
395
396
  end
396
397
  end
397
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
+
398
412
  def test_size
399
413
  Oj::Doc.open('[1,2,3]') do |doc|
400
414
  assert_equal(4, doc.size)
@@ -491,6 +505,11 @@ class DocTest < Minitest::Test
491
505
  assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
492
506
  end
493
507
 
508
+ def test_doc_empty
509
+ result = Oj::Doc.open("") { |doc| doc.each_child {} }
510
+ assert_nil(result)
511
+ end
512
+
494
513
  def test_comment
495
514
  json = %{{
496
515
  "x"/*one*/:/*two*/true,//three
data/test/test_saj.rb CHANGED
@@ -180,7 +180,7 @@ class SajTest < Minitest::Test
180
180
  assert_equal([:add_value, 12345, nil], handler.calls.first)
181
181
  type, message, line, column = handler.calls.last
182
182
  assert_equal([:error, 1, 6], [type, line, column])
183
- assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[a-z\.]+/)*saj\.c:\d+\]}, message)
183
+ assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[A-Za-z]:\/)?(?:[a-z\.]+/)*saj\.c:\d+\]}, message)
184
184
  end
185
185
 
186
186
  end
data/test/test_various.rb CHANGED
@@ -528,7 +528,7 @@ class Juice < Minitest::Test
528
528
  assert_equal(58, obj.y)
529
529
  end
530
530
 
531
- # Stream Deeply Nested
531
+ # Stream Deeply Nested
532
532
  def test_deep_nest_dump
533
533
  begin
534
534
  a = []
@@ -541,7 +541,7 @@ class Juice < Minitest::Test
541
541
  assert(false, "*** expected an exception")
542
542
  end
543
543
 
544
- # Stream IO
544
+ # Stream IO
545
545
  def test_io_string
546
546
  src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
547
547
  output = StringIO.open("", "w+")
@@ -553,6 +553,9 @@ class Juice < Minitest::Test
553
553
  end
554
554
 
555
555
  def test_io_file
556
+ # Windows does not support fork
557
+ return if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
558
+
556
559
  src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
557
560
  filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
558
561
  File.open(filename, "w") { |f|
@@ -564,6 +567,28 @@ class Juice < Minitest::Test
564
567
  assert_equal(src, obj)
565
568
  end
566
569
 
570
+ def test_io_stream
571
+ IO.pipe do |r, w|
572
+ if fork
573
+ r.close
574
+ #w.nonblock = false
575
+ a = []
576
+ 10_000.times do |i|
577
+ a << i
578
+ end
579
+ Oj.to_stream(w, a, indent: 2)
580
+ w.close
581
+ else
582
+ w.close
583
+ sleep(0.1) # to force a busy
584
+ cnt = 0
585
+ r.each_line { cnt += 1 }
586
+ r.close
587
+ Process.exit(0)
588
+ end
589
+ end
590
+ end
591
+
567
592
  # comments
568
593
  def test_comment_slash
569
594
  json = %{{
data/test/tests.rb CHANGED
@@ -18,7 +18,6 @@ require 'test_object'
18
18
  require 'test_saj'
19
19
  require 'test_scp'
20
20
  require 'test_strict'
21
- require 'test_various'
22
21
  require 'test_rails'
23
22
  require 'test_wab'
24
23
  require 'test_writer'
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.8
4
+ version: 3.13.11
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-27 00:00:00.000000000 Z
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -117,6 +117,7 @@ files:
117
117
  - ext/oj/dump_object.c
118
118
  - ext/oj/dump_strict.c
119
119
  - ext/oj/encode.h
120
+ - ext/oj/encoder.c
120
121
  - ext/oj/err.c
121
122
  - ext/oj/err.h
122
123
  - ext/oj/extconf.rb