oj 2.17.3 → 2.17.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,32 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
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
- }
@@ -1,111 +0,0 @@
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)
@@ -1,11 +0,0 @@
1
- %w(lib ext test).each do |dir|
2
- $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
3
- end
4
- require 'oj'
5
- require 'json'
6
-
7
- Oj::Doc.open([{:name => "T-Shirt"}].to_json) do |doc|
8
- doc.each_child do |child|
9
- p child.fetch("name")
10
- end
11
- end
data/test/io.rb DELETED
@@ -1,48 +0,0 @@
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]'))
@@ -1,27 +0,0 @@
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
@@ -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
-