oj 3.7.5 → 3.7.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/oj/buf.h +4 -4
- data/ext/oj/cache8.c +3 -3
- data/ext/oj/cache8.h +4 -4
- data/ext/oj/circarray.c +1 -1
- data/ext/oj/circarray.h +4 -4
- data/ext/oj/code.h +6 -6
- data/ext/oj/compat.c +5 -5
- data/ext/oj/custom.c +15 -13
- data/ext/oj/dump.c +15 -5
- data/ext/oj/dump.h +3 -3
- data/ext/oj/dump_compat.c +15 -11
- data/ext/oj/dump_leaf.c +1 -1
- data/ext/oj/dump_object.c +4 -2
- data/ext/oj/encode.h +3 -3
- data/ext/oj/err.c +1 -1
- data/ext/oj/err.h +5 -5
- data/ext/oj/fast.c +14 -14
- data/ext/oj/hash.c +7 -7
- data/ext/oj/hash.h +4 -4
- data/ext/oj/hash_test.c +2 -2
- data/ext/oj/mimic_json.c +9 -9
- data/ext/oj/object.c +3 -3
- data/ext/oj/odd.c +12 -8
- data/ext/oj/odd.h +5 -5
- data/ext/oj/oj.c +14 -12
- data/ext/oj/oj.h +23 -23
- data/ext/oj/parse.c +3 -3
- data/ext/oj/parse.h +26 -26
- data/ext/oj/rails.c +27 -23
- data/ext/oj/rails.h +3 -3
- data/ext/oj/reader.h +5 -5
- data/ext/oj/resolve.h +3 -3
- data/ext/oj/rxclass.c +5 -5
- data/ext/oj/rxclass.h +7 -7
- data/ext/oj/saj.c +2 -2
- data/ext/oj/scp.c +3 -3
- data/ext/oj/sparse.c +4 -4
- data/ext/oj/stream_writer.c +1 -1
- data/ext/oj/strict.c +6 -6
- data/ext/oj/string_writer.c +1 -1
- data/ext/oj/trace.h +8 -8
- data/ext/oj/val_stack.c +8 -2
- data/ext/oj/val_stack.h +9 -9
- data/ext/oj/wab.c +11 -7
- data/lib/oj/version.rb +1 -1
- data/test/bug.rb +51 -0
- data/test/bug2.rb +10 -0
- data/test/bug3.rb +46 -0
- data/test/bug_fast.rb +32 -0
- data/test/bug_load.rb +24 -0
- data/test/crash.rb +111 -0
- data/test/example.rb +11 -0
- data/test/foo.rb +4 -29
- data/test/io.rb +48 -0
- data/test/isolated/test_mimic_rails_datetime.rb +27 -0
- data/test/mem.rb +12 -27
- data/test/mod.rb +16 -0
- data/test/omit.rb +20 -0
- data/test/rails.rb +50 -0
- data/test/rails_datetime_test.rb +24 -0
- data/test/russian.rb +18 -0
- data/test/struct.rb +29 -0
- data/test/test_serializer.rb +59 -0
- data/test/write_timebars.rb +31 -0
- data/test/x_test.rb +185 -0
- metadata +102 -68
- data/test/big.rb +0 -15
data/test/bug3.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#!/usr/bin/env ruby
|
4
|
+
# encoding: UTF-8
|
5
|
+
|
6
|
+
$: << File.dirname(__FILE__)
|
7
|
+
%w(lib ext test).each do |dir|
|
8
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'oj'
|
12
|
+
require 'stringio'
|
13
|
+
|
14
|
+
class Parser < Oj::Saj
|
15
|
+
|
16
|
+
def parse(json)
|
17
|
+
Oj.saj_parse(self, StringIO.new(json))
|
18
|
+
end
|
19
|
+
|
20
|
+
def hash_start(key)
|
21
|
+
puts "START: #{key}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def error(message, line, column)
|
25
|
+
puts "Error callback: #{message}"
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
parser = Parser.new
|
31
|
+
|
32
|
+
begin
|
33
|
+
# truncated JSON, Oj.saj_parse raises, #error not called
|
34
|
+
parser.parse('{"foo{"bar":')
|
35
|
+
rescue Exception => e
|
36
|
+
puts "*** #{e.class}: #{e.message}"
|
37
|
+
end
|
38
|
+
|
39
|
+
puts "\n\n"
|
40
|
+
|
41
|
+
begin
|
42
|
+
# invalid JSON, doesn't raise an error
|
43
|
+
parser.parse('{"foo":{"bar":}')
|
44
|
+
rescue Exception => e
|
45
|
+
puts "*** #{e.class}: #{e.message}"
|
46
|
+
end
|
data/test/bug_fast.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
%w(lib ext test).each do |dir|
|
5
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'oj'
|
9
|
+
|
10
|
+
def create_item(doc)
|
11
|
+
#puts "#{doc.fetch('/id')}: #{doc.fetch('/labels/it/value')} - #{doc.fetch('/descriptions/it/value')}"
|
12
|
+
doc.fetch('/id')
|
13
|
+
doc.fetch('/labels/it/value')
|
14
|
+
doc.fetch('/descriptions/it/value')
|
15
|
+
end
|
16
|
+
|
17
|
+
100.times { |i|
|
18
|
+
File.open('dump_10k.json') { |f|
|
19
|
+
f.each { |line|
|
20
|
+
#Oj::Doc.open(line) { |doc|
|
21
|
+
doc = Oj::Doc.open(line)
|
22
|
+
begin
|
23
|
+
create_item(doc) if doc.fetch('/type') == 'item'
|
24
|
+
rescue Exception => e
|
25
|
+
puts "*** #{e.class}: #{e.message}"
|
26
|
+
end
|
27
|
+
doc.close
|
28
|
+
#}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
puts i
|
32
|
+
}
|
data/test/bug_load.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
%w(lib ext test).each do |dir|
|
5
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'oj'
|
9
|
+
|
10
|
+
def create_item(doc)
|
11
|
+
item_id = doc['source']
|
12
|
+
# ...
|
13
|
+
puts item_id
|
14
|
+
end
|
15
|
+
|
16
|
+
File.open('log.json') { |f|
|
17
|
+
Oj::load(f, mode: :compat) { |doc|
|
18
|
+
begin
|
19
|
+
create_item(doc) if doc['msgType'] == 1
|
20
|
+
rescue Exception => e
|
21
|
+
puts "*** #{e.class}: #{e.message}"
|
22
|
+
end
|
23
|
+
}
|
24
|
+
}
|
data/test/crash.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$VERBOSE = true
|
5
|
+
|
6
|
+
%w(lib ext test).each do |dir|
|
7
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'sqlite3'
|
11
|
+
require 'active_record'
|
12
|
+
require 'oj'
|
13
|
+
require 'representable'
|
14
|
+
require 'representable/json'
|
15
|
+
require 'multi_json'
|
16
|
+
MultiJson.use(:oj)
|
17
|
+
Oj.default_options = {mode: :object, indent: 2}
|
18
|
+
|
19
|
+
#ActiveRecord::Base.logger = Logger.new(STDERR)
|
20
|
+
|
21
|
+
ActiveRecord::Base.establish_connection(
|
22
|
+
:adapter => "sqlite3",
|
23
|
+
:database => ":memory:"
|
24
|
+
)
|
25
|
+
|
26
|
+
ActiveRecord::Schema.define do
|
27
|
+
create_table :users do |t|
|
28
|
+
t.string "first_name"
|
29
|
+
t.string "last_name"
|
30
|
+
t.string "picture"
|
31
|
+
t.integer "state", null: false
|
32
|
+
t.integer "visible", default: 1, null: false
|
33
|
+
t.integer "role", null: false
|
34
|
+
t.string "position"
|
35
|
+
t.integer "responded", default: 0
|
36
|
+
t.string "slug"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
end
|
42
|
+
User.find_or_create_by(first_name: "John", last_name: "Smith",role:1,id:1,state: 0)
|
43
|
+
User.find_or_create_by(first_name: "John", last_name: "Smith",role:1,id:2,state: 0)
|
44
|
+
|
45
|
+
|
46
|
+
class PersonDecorator < Representable::Decorator
|
47
|
+
include Representable::JSON
|
48
|
+
include Representable::Hash
|
49
|
+
|
50
|
+
property :id
|
51
|
+
|
52
|
+
property :first_name
|
53
|
+
|
54
|
+
property :last_name,
|
55
|
+
default: nil
|
56
|
+
|
57
|
+
property :name,
|
58
|
+
getter: ->(_){ "#{first_name} #{last_name}".strip },
|
59
|
+
writeable: false
|
60
|
+
|
61
|
+
property :visible,
|
62
|
+
render_filter: lambda{ |value, _| !value.zero? },
|
63
|
+
parse_filter: lambda{ |value, _| value ? 1 : 0 },
|
64
|
+
default: 1
|
65
|
+
|
66
|
+
property :role,
|
67
|
+
render_filter: lambda{ |value, _| { 1 => 'staff', 2 => 'manager' }[value].to_s },
|
68
|
+
writeable: false
|
69
|
+
|
70
|
+
property :position
|
71
|
+
|
72
|
+
nested :image, skip_render: lambda{ |options| options[:represented].picture.nil? } do
|
73
|
+
property :picture, as: :url, writeable: false
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
class PersonViewModel
|
79
|
+
|
80
|
+
def initialize(person, organization)
|
81
|
+
@users = User.all
|
82
|
+
@person = person
|
83
|
+
@organization = organization
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_json(options={})
|
87
|
+
PersonDecorator.represent(@users.to_a)
|
88
|
+
end
|
89
|
+
|
90
|
+
def json_with_decorator(options={})
|
91
|
+
PersonDecorator.new(person).to_json
|
92
|
+
end
|
93
|
+
|
94
|
+
def person
|
95
|
+
@person
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class EmployeeViewModel < PersonViewModel
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
employee = User.find(1)
|
104
|
+
emp_json = EmployeeViewModel.new(employee,nil)
|
105
|
+
puts emp_json.to_json.to_json
|
106
|
+
puts emp_json.json_with_decorator
|
107
|
+
Oj.mimic_JSON
|
108
|
+
# when i use Oj.mimic_JSON script crash
|
109
|
+
# when i dont use Oj.mimic_JSON then dont use correct decorator
|
110
|
+
puts Oj.dump(emp_json)
|
111
|
+
puts Oj.dump(emp_json.to_json)
|
data/test/example.rb
ADDED
data/test/foo.rb
CHANGED
@@ -1,32 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
2
3
|
|
3
|
-
$: <<
|
4
|
-
$: << '../lib'
|
5
|
-
$: << '../ext'
|
4
|
+
$: << File.dirname(__FILE__)
|
6
5
|
|
7
|
-
require '
|
8
|
-
|
9
|
-
|
10
|
-
class MyParser
|
11
|
-
def initialize
|
12
|
-
@current_depth = 0
|
13
|
-
end
|
14
|
-
|
15
|
-
def parse(file)
|
16
|
-
Oj.sc_parse(self, file)
|
17
|
-
end
|
18
|
-
|
19
|
-
def hash_start
|
20
|
-
puts "start"
|
21
|
-
@current_depth += 1
|
22
|
-
raise Exception.new("Hello")
|
23
|
-
{}
|
24
|
-
end
|
25
|
-
|
26
|
-
# Other Oj::ScHandler methods go below.
|
27
|
-
# The parser's purpose is to find a specific value nested deep within the streaming JSON
|
28
|
-
# and to stop parsing immediately afterward.
|
29
|
-
end
|
30
|
-
|
31
|
-
parser = MyParser.new
|
32
|
-
parser.parse(%|{"a":{"b":{"c":{}}}}|)
|
6
|
+
require 'test_strict'
|
7
|
+
require 'test_compat'
|
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]'))
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.join(File.dirname(__FILE__), '..')
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
require 'oj/active_support_helper'
|
8
|
+
|
9
|
+
class ObjectFolder < Minitest::Test
|
10
|
+
def setup
|
11
|
+
@default_options = Oj.default_options
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
Oj.default_options = @default_options
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_as_json
|
19
|
+
Oj.mimic_JSON()
|
20
|
+
dt = DateTime.now()
|
21
|
+
|
22
|
+
json = dt.to_json()
|
23
|
+
|
24
|
+
puts json
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/test/mem.rb
CHANGED
@@ -1,35 +1,20 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
2
3
|
|
3
|
-
$: <<
|
4
|
-
$: <<
|
5
|
-
$: << '../ext'
|
4
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
5
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
6
6
|
|
7
|
-
require 'objspace'
|
8
7
|
require 'oj'
|
9
|
-
require 'json'
|
10
|
-
require 'get_process_mem'
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
puts "Before - Symbols count: #{Symbol.all_symbols.size}"
|
9
|
+
h = {
|
10
|
+
'a' => 1,
|
11
|
+
'b' => 2,
|
12
|
+
'c' => 3,
|
13
|
+
'd' => 4,
|
14
|
+
'e' => 5,
|
15
|
+
}
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
GC.start
|
17
|
+
10000.times do
|
24
18
|
GC.start
|
25
|
-
|
26
|
-
|
27
|
-
puts "After - Process Memory: #{mem.mb} mb"
|
28
|
-
puts "After - Objects count: #{ObjectSpace.each_object.count}"
|
29
|
-
puts "After - Symbols count: #{Symbol.all_symbols.size}"
|
30
|
-
end
|
31
|
-
|
32
|
-
record_allocation do
|
33
|
-
data = 1_000_000.times.map { |i| "string_number_#{i}".to_sym } # array of symbols
|
34
|
-
Oj.dump(data, mode: :rails)
|
19
|
+
Oj.dump(h)
|
35
20
|
end
|
data/test/mod.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
%w(lib ext test).each do |dir|
|
5
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'oj'
|
9
|
+
|
10
|
+
|
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/omit.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
5
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
6
|
+
|
7
|
+
require 'oj'
|
8
|
+
require 'stringio'
|
9
|
+
require 'parallel'
|
10
|
+
|
11
|
+
h = { a: 1, b: nil, c: 3 }
|
12
|
+
outputs = Parallel.map((0...30).to_a, in_process: 30) do |i|
|
13
|
+
io = StringIO.new('wb+')
|
14
|
+
Oj.to_stream(io, h, omit_nil: false)
|
15
|
+
io.string
|
16
|
+
end
|
17
|
+
|
18
|
+
outputs.each do |output|
|
19
|
+
puts output
|
20
|
+
end
|
data/test/rails.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$VERBOSE = true
|
5
|
+
|
6
|
+
here = File.expand_path(File.dirname(__FILE__))
|
7
|
+
$: << File.dirname(__FILE__)
|
8
|
+
$: << File.join(File.dirname(here), 'ext')
|
9
|
+
$: << File.join(File.dirname(here), 'lib')
|
10
|
+
|
11
|
+
require "active_record"
|
12
|
+
require "minitest/autorun"
|
13
|
+
require "logger"
|
14
|
+
require 'sidekiq/testing'
|
15
|
+
require "rspec/mocks/minitest_integration"
|
16
|
+
require 'oj'
|
17
|
+
|
18
|
+
Oj.mimic_JSON
|
19
|
+
|
20
|
+
Sidekiq::Testing.inline!
|
21
|
+
|
22
|
+
# Ensure backward compatibility with Minitest 4
|
23
|
+
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
24
|
+
|
25
|
+
# This connection will do for database-independent bug reports.
|
26
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
27
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
28
|
+
|
29
|
+
ActiveRecord::Schema.define do
|
30
|
+
create_table :posts, force: true do |t|
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Post < ActiveRecord::Base
|
35
|
+
end
|
36
|
+
|
37
|
+
class MyWorker
|
38
|
+
include Sidekiq::Worker
|
39
|
+
|
40
|
+
def perform(post)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class BugTest < Minitest::Test
|
45
|
+
def test_as_json
|
46
|
+
Post.arel_table
|
47
|
+
dbl = instance_double("Post", id: 1)
|
48
|
+
MyWorker.perform_async dbl
|
49
|
+
end
|
50
|
+
end
|