oj 3.7.6 → 3.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
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 "rails/all"
12
- require "active_model"
13
-
14
- date = DateTime.now
15
-
16
- puts date.to_json
17
-
18
- require "oj"
19
-
20
- puts "OJ.dump(DateTime.now)"
21
- puts
22
- puts Oj.dump(DateTime.now)
23
- puts
24
-
@@ -1,18 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- %w(lib ext test).each do |dir|
7
- $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
8
- end
9
-
10
- require 'oj'
11
-
12
- s = %|{"response":[{"id":7250,"from_id":1382722,"owner_id":1382722,"date":1415964230,"post_type":"post","text":"Сдается комната в 2-х комнатной квартире с декабря месяца в Люберцах. (р-он Красная Горка)\nСдается желательно семейной паре (можно рассмотреть и другие варианты). \nв комнате одна большая . Стол, Шкаф, комод.\nДля проживания все есть. в Соседней комнате проживают два парня (из Чувашии)\nДо города можно доехать на маршрутке (20 мин.) на против дома остановка, на электричке (до электрички 15-20 мин. пешком) или на автобусе\nЦена 15 тыс. за комнату + коммунальные услуги\nЗвоните 8\/903\/012-34-25 венера","comments":{"count":0},"likes":{"count":1},"reposts":{"count":0}}]}|
13
-
14
- r = Oj.load(s)
15
-
16
- text = r["response"][0]["text"]
17
- puts "#{text}"
18
- puts "#{text.encoding}"
@@ -1,29 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
7
-
8
- $VERBOSE = true
9
-
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
12
-
13
- require 'oj'
14
-
15
- A = Struct.new(:a,:b,:c,:d)
16
- B = Struct.new(:e,:f)
17
-
18
- obj = [A.new(55, B.new(1, 'X'), B.new(2, 'Y'), 3)]
19
-
20
- s = Oj.dump(obj, :mode => :object)
21
-
22
- 100000.times do
23
- Oj.load(s, :mode => :object)
24
- # ds = Oj.dump(o, :mode => :object)
25
- # if ds != s
26
- # puts ds
27
- # raise "holy crap"
28
- # end
29
- end
@@ -1,59 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- %w(lib ext test).each do |dir|
7
- $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
8
- end
9
-
10
- require 'minitest'
11
- require 'minitest/autorun'
12
- require 'oj'
13
-
14
- Oj.mimic_JSON
15
-
16
- require 'rails/all'
17
- require 'active_model'
18
- require 'active_model_serializers'
19
- require 'active_support/json'
20
-
21
- #Oj.mimic_JSON
22
-
23
- class Category
24
- include ActiveModel::Model
25
- include ActiveModel::SerializerSupport
26
-
27
- attr_accessor :id, :name
28
-
29
- def initialize(id, name)
30
- @id = id
31
- @name = name
32
- end
33
- end
34
-
35
- class CategorySerializer < ActiveModel::Serializer
36
- attributes :id, :name
37
- end
38
-
39
- class MimicRails < Minitest::Test
40
-
41
- def test_dump_object
42
- Oj.default_options= {:indent => 0}
43
- category = Category.new(1, 'test')
44
- serializer = CategorySerializer.new(category)
45
-
46
- json = serializer.to_json()
47
- puts "*** serializer.to_json() #{serializer.to_json()}"
48
- assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
49
-
50
- json = serializer.as_json()
51
- puts "*** serializer.as_json() #{serializer.as_json()}"
52
- assert_equal({"category" => {:id => 1, :name => "test"}}, json)
53
-
54
- json = JSON.dump(serializer)
55
- puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
56
- assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
57
- end
58
-
59
- end # MimicRails
@@ -1,31 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- %w(lib ext).each do |dir|
5
- $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
6
- end
7
-
8
- require 'stringio'
9
- require 'oj'
10
-
11
-
12
- filename = File.join(File.dirname(__FILE__), 'day.json')
13
- File.open(filename, "w") do |f|
14
- w = Oj::StreamWriter.new(f, :indent => -1)
15
- 390.times do |i|
16
- w.push_object()
17
- w.push_value(12, 'msgType')
18
- w.push_value(1, 'version')
19
- w.push_value(1_400_074_200 + i * 60, 'bar')
20
- w.push_value('TBC', 'source')
21
- w.push_array('timebars')
22
- w.push_object()
23
- w.push_value('aapl_24', 'asset')
24
- w.push_value(91.87, 'close')
25
- w.pop()
26
- w.pop()
27
- w.pop()
28
- end
29
- f.write("\n")
30
- end
31
-
@@ -1,185 +0,0 @@
1
-
2
- $: << File.expand_path("../../oj/ext", __FILE__)
3
- $: << File.expand_path("../../oj/lib", __FILE__)
4
-
5
- require 'rubygems'
6
- require 'sqlite3'
7
- require 'terminal-table'
8
- require 'oj'
9
- require 'rails/all'
10
- require 'benchmark'
11
- begin
12
- require 'benchmark/memory'
13
- rescue LoadError => e
14
- end
15
-
16
- OJ_1 = { mode: :object, use_as_json: false, float_precision: 16, bigdecimal_as_decimal: false, nan: :null }
17
- OJ_2 = { mode: :compat, use_as_json: false, float_precision: 16, bigdecimal_as_decimal: false, nan: :null, time_format: :xmlschema, second_precision: 3 }
18
- OJ_3 = { mode: :compat, use_as_json: true, float_precision: 16, bigdecimal_as_decimal: false, nan: :null, time_format: :xmlschema, second_precision: 3 }
19
-
20
- # test data
21
- class Colors
22
- include Enumerable
23
- def each
24
- yield 'red'
25
- yield 'green'
26
- yield 'blue'
27
- end
28
- end
29
-
30
- Struct.new('Customer', :name, :address)
31
-
32
- fork { exit 99 }
33
- Process.wait
34
-
35
- ActiveRecord::Base.logger = Logger.new(STDERR)
36
- ActiveRecord::Base.logger.level = 1
37
-
38
- ActiveRecord::Base.establish_connection(
39
- adapter: 'sqlite3',
40
- database:':memory:'
41
- )
42
-
43
- ActiveRecord::Migration.verbose = false
44
- ActiveRecord::Schema.define do
45
- create_table :users do |table|
46
- table.column :name, :string
47
- end
48
- end
49
-
50
- class User < ActiveRecord::Base
51
- validates :name, presence: true
52
- end
53
-
54
- user = User.new
55
- user.valid?
56
-
57
- TEST_DATA = {
58
- Regexp: /test/,
59
- FalseClass: false,
60
- NilClass: nil,
61
- Object: Object.new,
62
- TrueClass: true,
63
- String: 'abc',
64
- StringChinese: '二胡',
65
- Numeric: 1,
66
- Symbol: :sym,
67
- Time: Time.new,
68
- Array: [],
69
- Hash: {},
70
- HashNotEmpty: {a: 1},
71
- Date: Date.new,
72
- DateTime: DateTime.new,
73
- Enumerable: Colors.new,
74
- BigDecimal: '1'.to_d/3,
75
- BigDecimalInfinity: '0.5'.to_d/0,
76
- Struct: Struct::Customer.new('Dave', '123 Main'),
77
- Float: 1.0/3,
78
- FloatInfinity: 0.5/0,
79
- Range: (1..10),
80
- 'Process::Status': $?,
81
- 'ActiveSupport::TimeWithZone': Time.utc(2005,2,1,15,15,10).in_time_zone('Hawaii'),
82
- 'ActiveModel::Errors': user.errors,
83
- 'ActiveSupport::Duration': 1.month.ago,
84
- 'ActiveSupport::Multibyte::Chars': 'über'.mb_chars,
85
- 'ActiveRecord::Relation': User.where(name: 'aaa'),
86
-
87
- # 'ActionDispatch::Journey::GTG::TransitionTable': TODO,
88
- }
89
-
90
- # helper
91
- def compare(expected, result)
92
- if result.is_a? Exception
93
- '💀'
94
- else
95
- if expected != result
96
- puts "*** expected: #{expected}"
97
- puts "*** actual: #{result}"
98
- end
99
- expected == result ? '👌' : '❌'
100
- end
101
- end
102
-
103
- # def compare(expected, result)
104
- # if result.is_a? Exception
105
- # 'Error'
106
- # else
107
- # expected == result ? 'Ok' : 'Fail'
108
- # end
109
- # end
110
-
111
- # actual tests
112
- test_result = TEST_DATA.map do |key, val|
113
- to_json_result = val.to_json
114
-
115
- json_generate_result = begin
116
- JSON.generate(val)
117
- rescue JSON::GeneratorError => e
118
- e
119
- end
120
-
121
- Oj.default_options = OJ_1
122
- oj_dump_result_1 = begin
123
- if key == :DateTime
124
- Exception.new('Unknown error')
125
- else
126
- Oj.dump(val)
127
- end
128
- rescue NoMemoryError => e
129
- e
130
- end
131
-
132
- Oj.default_options = OJ_2
133
- oj_dump_result_2 = begin
134
- Oj.dump(val)
135
- rescue NoMemoryError => e
136
- e
137
- end
138
-
139
- Oj.default_options = OJ_3
140
- oj_dump_result_3 = begin
141
- Oj.dump(val)
142
- rescue NoMemoryError => e
143
- e
144
- end
145
-
146
- [key, {
147
- to_json_result: to_json_result,
148
- #json_generate: compare(to_json_result, json_generate_result),
149
- #oj_dump_1: compare(to_json_result, oj_dump_result_1),
150
- oj_dump_2: compare(to_json_result, oj_dump_result_2),
151
- #oj_dump_3: compare(to_json_result, oj_dump_result_3),
152
- }]
153
- end.to_h
154
-
155
- # format output
156
- rows = test_result.map do |key, val|
157
- [key, val[:json_generate], val[:oj_dump_1], val[:oj_dump_2], val[:oj_dump_3]]
158
- end
159
-
160
- puts "\nComparing Rails to_json with other JSON implementations\n"
161
- puts Terminal::Table.new headings: ['class', 'JSON.generate', 'Oj.dump (object)', 'Oj.dump (compat)', 'Oj.dump (compat, as_json)'], rows: rows
162
-
163
- Oj.default_options = OJ_3
164
-
165
- obj = TEST_DATA
166
-
167
- puts "\n"
168
-
169
- =begin
170
- if Benchmark.respond_to?(:memory)
171
- Benchmark.memory do |x|
172
- x.report('to_json:'){ 10_000.times { obj.to_json } }
173
- x.report('Oj:') { 10_000.times { Oj.dump(obj) } }
174
- x.compare!
175
- end
176
- puts "---------------------------------------------\n\n"
177
- end
178
-
179
- Benchmark.benchmark(Benchmark::CAPTION, 14, Benchmark::FORMAT) do |x|
180
- x.report('to_json:'){ 10_000.times { obj.to_json } }
181
- x.report('Oj:') { 10_000.times { Oj.dump(obj) } }
182
- end
183
- =end
184
- puts "\n"
185
-