light_serializer 0.0.3
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.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/.reek.yml +10 -0
- data/.rspec +1 -0
- data/.rubocop.yml +40 -0
- data/.travis.yml +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +107 -0
- data/LICENSE +21 -0
- data/README.md +4 -0
- data/bench.rb +291 -0
- data/bin/test +6 -0
- data/lib/light_serializer/hashed_object.rb +81 -0
- data/lib/light_serializer/serializer.rb +40 -0
- data/lib/light_serializer/version.rb +5 -0
- data/lib/light_serializer.rb +8 -0
- data/light_serializer.gemspec +32 -0
- metadata +186 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: cb81dbbff7d706fe602201135936435e9bef0272da3e7b9544803b044c9d1229
|
|
4
|
+
data.tar.gz: 55650dfc29a15a3e4782bf670accfe415fbcb67e590ed8d6cfdb3269ec73aa37
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 15ed1028cd9f0cc9ef11927a019338234964b1a111d79361f1a7f059eed0431cefb2729378efb6e33101515491f43f423a393870f50c63c76f11dd48d86c945e
|
|
7
|
+
data.tar.gz: 36d724d687476eaf7f579aadab4991895cc0bec741744456f1a1f319c4fa997ec7d55d7616d7bdeea812ac359fd2e899b7326cde6db130825451744e0b77d427
|
data/.gitignore
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
/.config
|
|
4
|
+
/coverage/
|
|
5
|
+
/InstalledFiles
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/spec/examples.txt
|
|
9
|
+
/test/tmp/
|
|
10
|
+
/test/version_tmp/
|
|
11
|
+
/tmp/
|
|
12
|
+
|
|
13
|
+
# Used by dotenv library to load environment variables.
|
|
14
|
+
# .env
|
|
15
|
+
|
|
16
|
+
## Specific to RubyMotion:
|
|
17
|
+
.dat*
|
|
18
|
+
.repl_history
|
|
19
|
+
build/
|
|
20
|
+
*.bridgesupport
|
|
21
|
+
build-iPhoneOS/
|
|
22
|
+
build-iPhoneSimulator/
|
|
23
|
+
|
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
|
25
|
+
#
|
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
|
29
|
+
#
|
|
30
|
+
# vendor/Pods/
|
|
31
|
+
|
|
32
|
+
## Documentation cache and generated files:
|
|
33
|
+
/.yardoc/
|
|
34
|
+
/_yardoc/
|
|
35
|
+
/doc/
|
|
36
|
+
/rdoc/
|
|
37
|
+
|
|
38
|
+
## Environment normalization:
|
|
39
|
+
/.bundle/
|
|
40
|
+
/vendor/bundle
|
|
41
|
+
/lib/bundler/man/
|
|
42
|
+
|
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
45
|
+
# Gemfile.lock
|
|
46
|
+
# .ruby-version
|
|
47
|
+
# .ruby-gemset
|
|
48
|
+
|
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
50
|
+
.rvmrc
|
data/.reek.yml
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require: rubocop-rspec
|
|
2
|
+
|
|
3
|
+
AllCops:
|
|
4
|
+
AutoCorrect: true
|
|
5
|
+
TargetRubyVersion: 2.3
|
|
6
|
+
Exclude:
|
|
7
|
+
- 'bin/*'
|
|
8
|
+
- 'vendor/*'
|
|
9
|
+
|
|
10
|
+
Style/Documentation:
|
|
11
|
+
Enabled: false
|
|
12
|
+
|
|
13
|
+
Metrics/LineLength:
|
|
14
|
+
Max: 120
|
|
15
|
+
|
|
16
|
+
Metrics/BlockLength:
|
|
17
|
+
Exclude:
|
|
18
|
+
- 'spec/**/*.rb'
|
|
19
|
+
|
|
20
|
+
Layout/AlignArray:
|
|
21
|
+
Enabled: false
|
|
22
|
+
|
|
23
|
+
Layout/AlignHash:
|
|
24
|
+
Enabled: true
|
|
25
|
+
EnforcedLastArgumentHashStyle: ignore_implicit
|
|
26
|
+
|
|
27
|
+
Layout/AlignParameters:
|
|
28
|
+
EnforcedStyle: with_fixed_indentation
|
|
29
|
+
|
|
30
|
+
Layout/CaseIndentation:
|
|
31
|
+
EnforcedStyle: end
|
|
32
|
+
|
|
33
|
+
Layout/DotPosition:
|
|
34
|
+
EnforcedStyle: trailing
|
|
35
|
+
|
|
36
|
+
Layout/EndAlignment:
|
|
37
|
+
EnforcedStyleAlignWith: variable
|
|
38
|
+
|
|
39
|
+
Layout/MultilineMethodCallIndentation:
|
|
40
|
+
EnforcedStyle: indented
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
light_serializer (0.0.3)
|
|
5
|
+
oj (~> 3)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
activesupport (4.2.10)
|
|
11
|
+
i18n (~> 0.7)
|
|
12
|
+
minitest (~> 5.1)
|
|
13
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
|
14
|
+
tzinfo (~> 1.1)
|
|
15
|
+
ast (2.4.0)
|
|
16
|
+
axiom-types (0.1.1)
|
|
17
|
+
descendants_tracker (~> 0.0.4)
|
|
18
|
+
ice_nine (~> 0.11.0)
|
|
19
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
|
20
|
+
byebug (10.0.2)
|
|
21
|
+
codeclimate-engine-rb (0.4.1)
|
|
22
|
+
virtus (~> 1.0)
|
|
23
|
+
coderay (1.1.2)
|
|
24
|
+
coercible (1.0.0)
|
|
25
|
+
descendants_tracker (~> 0.0.1)
|
|
26
|
+
concurrent-ruby (1.0.5)
|
|
27
|
+
descendants_tracker (0.0.4)
|
|
28
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
|
29
|
+
diff-lcs (1.3)
|
|
30
|
+
equalizer (0.0.11)
|
|
31
|
+
factory_bot (4.11.1)
|
|
32
|
+
activesupport (>= 3.0.0)
|
|
33
|
+
i18n (0.9.5)
|
|
34
|
+
concurrent-ruby (~> 1.0)
|
|
35
|
+
ice_nine (0.11.2)
|
|
36
|
+
jaro_winkler (1.5.1)
|
|
37
|
+
kwalify (0.7.2)
|
|
38
|
+
method_source (0.9.0)
|
|
39
|
+
minitest (5.11.3)
|
|
40
|
+
oj (3.7.0)
|
|
41
|
+
parallel (1.12.1)
|
|
42
|
+
parser (2.5.1.2)
|
|
43
|
+
ast (~> 2.4.0)
|
|
44
|
+
powerpack (0.1.2)
|
|
45
|
+
pry (0.11.3)
|
|
46
|
+
coderay (~> 1.1.0)
|
|
47
|
+
method_source (~> 0.9.0)
|
|
48
|
+
pry-byebug (3.6.0)
|
|
49
|
+
byebug (~> 10.0)
|
|
50
|
+
pry (~> 0.10)
|
|
51
|
+
rainbow (3.0.0)
|
|
52
|
+
rake (10.5.0)
|
|
53
|
+
reek (5.1.0)
|
|
54
|
+
codeclimate-engine-rb (~> 0.4.0)
|
|
55
|
+
kwalify (~> 0.7.0)
|
|
56
|
+
parser (>= 2.5.0.0, < 2.6, != 2.5.1.1)
|
|
57
|
+
rainbow (>= 2.0, < 4.0)
|
|
58
|
+
rspec (3.8.0)
|
|
59
|
+
rspec-core (~> 3.8.0)
|
|
60
|
+
rspec-expectations (~> 3.8.0)
|
|
61
|
+
rspec-mocks (~> 3.8.0)
|
|
62
|
+
rspec-core (3.8.0)
|
|
63
|
+
rspec-support (~> 3.8.0)
|
|
64
|
+
rspec-expectations (3.8.1)
|
|
65
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
66
|
+
rspec-support (~> 3.8.0)
|
|
67
|
+
rspec-mocks (3.8.0)
|
|
68
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
69
|
+
rspec-support (~> 3.8.0)
|
|
70
|
+
rspec-support (3.8.0)
|
|
71
|
+
rubocop (0.59.2)
|
|
72
|
+
jaro_winkler (~> 1.5.1)
|
|
73
|
+
parallel (~> 1.10)
|
|
74
|
+
parser (>= 2.5, != 2.5.1.1)
|
|
75
|
+
powerpack (~> 0.1)
|
|
76
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
77
|
+
ruby-progressbar (~> 1.7)
|
|
78
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
79
|
+
rubocop-rspec (1.30.0)
|
|
80
|
+
rubocop (>= 0.58.0)
|
|
81
|
+
ruby-progressbar (1.10.0)
|
|
82
|
+
thread_safe (0.3.6)
|
|
83
|
+
tzinfo (1.2.5)
|
|
84
|
+
thread_safe (~> 0.1)
|
|
85
|
+
unicode-display_width (1.4.0)
|
|
86
|
+
virtus (1.0.5)
|
|
87
|
+
axiom-types (~> 0.1)
|
|
88
|
+
coercible (~> 1.0)
|
|
89
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
|
90
|
+
equalizer (~> 0.0, >= 0.0.9)
|
|
91
|
+
|
|
92
|
+
PLATFORMS
|
|
93
|
+
ruby
|
|
94
|
+
|
|
95
|
+
DEPENDENCIES
|
|
96
|
+
bundler (~> 1.0)
|
|
97
|
+
factory_bot (~> 4.11)
|
|
98
|
+
light_serializer!
|
|
99
|
+
pry-byebug (~> 3.0)
|
|
100
|
+
rake (~> 10.0)
|
|
101
|
+
reek (~> 5.1)
|
|
102
|
+
rspec (~> 3.0)
|
|
103
|
+
rubocop (~> 0.59.2)
|
|
104
|
+
rubocop-rspec (~> 1.30)
|
|
105
|
+
|
|
106
|
+
BUNDLED WITH
|
|
107
|
+
1.16.2
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Pavel Rodionov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
data/bench.rb
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
|
2
|
+
require 'surrealist'
|
|
3
|
+
require_relative 'lib/light_serializer'
|
|
4
|
+
require 'benchmark/ips'
|
|
5
|
+
require 'active_record'
|
|
6
|
+
require 'active_model'
|
|
7
|
+
require 'active_model_serializers'
|
|
8
|
+
require 'blueprinter'
|
|
9
|
+
require 'pry'
|
|
10
|
+
|
|
11
|
+
ActiveRecord::Base.establish_connection(
|
|
12
|
+
adapter: 'sqlite3',
|
|
13
|
+
database: ':memory:'
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
ActiveRecord::Migration.verbose = false
|
|
17
|
+
ActiveRecord::Schema.define do
|
|
18
|
+
create_table :users do |table|
|
|
19
|
+
table.column :name, :string
|
|
20
|
+
table.column :email, :string
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
create_table :authors do |table|
|
|
24
|
+
table.column :name, :string
|
|
25
|
+
table.column :last_name, :string
|
|
26
|
+
table.column :age, :int
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
create_table :books do |table|
|
|
30
|
+
table.column :title, :string
|
|
31
|
+
table.column :year, :string
|
|
32
|
+
table.belongs_to :author, foreign_key: true
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
ActiveModelSerializers.config.adapter = :json
|
|
37
|
+
|
|
38
|
+
def random_name
|
|
39
|
+
('a'..'z').to_a.shuffle.join('').first(10).capitalize
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class User < ActiveRecord::Base
|
|
43
|
+
include Surrealist
|
|
44
|
+
|
|
45
|
+
json_schema { { name: String, email: String } }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class UserLightSerializer < LightSerializer::Serializer
|
|
49
|
+
attributes(:name, :email)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
class UserSerializer < ActiveModel::Serializer
|
|
53
|
+
attributes :name, :email
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class UserSurrealistSerializer < Surrealist::Serializer
|
|
57
|
+
json_schema { { name: String, email: String } }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
class UserAMSSerializer < ActiveModel::Serializer
|
|
61
|
+
attributes :name, :email
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
class UserBlueprint < Blueprinter::Base
|
|
65
|
+
fields :name, :email
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
### Associations ###
|
|
69
|
+
|
|
70
|
+
class BookLightSerializer < LightSerializer::Serializer
|
|
71
|
+
attributes(:title, :year)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
class AuthorLightSerializer < LightSerializer::Serializer
|
|
75
|
+
attributes(
|
|
76
|
+
:name,
|
|
77
|
+
:last_name,
|
|
78
|
+
:full_name,
|
|
79
|
+
:age,
|
|
80
|
+
books: BookLightSerializer
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
class AuthorSurrealistSerializer < Surrealist::Serializer
|
|
85
|
+
json_schema do
|
|
86
|
+
{ name: String, last_name: String, full_name: String, age: Integer, books: Array }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def books
|
|
90
|
+
object.books.to_a
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def full_name
|
|
94
|
+
"#{object.name} #{object.last_name}"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
class BookSurrealistSerializer < Surrealist::Serializer
|
|
99
|
+
json_schema { { title: String, year: String } }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
class BookAMSSerializer < ActiveModel::Serializer
|
|
103
|
+
attributes :title, :year
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
class BookBlueprint < Blueprinter::Base
|
|
107
|
+
fields :title, :year
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
class AuthorAMSSerializer < ActiveModel::Serializer
|
|
111
|
+
attributes :name, :last_name, :full_name, :age
|
|
112
|
+
has_many :books, serializer: BookAMSSerializer
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
class AuthorBlueprint < Blueprinter::Base
|
|
116
|
+
fields :name, :last_name, :age
|
|
117
|
+
field :full_name do |author|
|
|
118
|
+
"#{author.name} #{author.last_name}"
|
|
119
|
+
end
|
|
120
|
+
association :books, blueprint: BookBlueprint
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
class Author < ActiveRecord::Base
|
|
124
|
+
include Surrealist
|
|
125
|
+
surrealize_with AuthorSurrealistSerializer
|
|
126
|
+
|
|
127
|
+
has_many :books
|
|
128
|
+
|
|
129
|
+
def full_name
|
|
130
|
+
"#{name} #{last_name}"
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
class Book < ActiveRecord::Base
|
|
135
|
+
include Surrealist
|
|
136
|
+
surrealize_with BookSurrealistSerializer
|
|
137
|
+
|
|
138
|
+
belongs_to :author, required: true
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
N = 3000
|
|
142
|
+
N.times { User.create!(name: random_name, email: "#{random_name}@test.com") }
|
|
143
|
+
(N / 2).times { Author.create!(name: random_name, last_name: random_name, age: rand(80)) }
|
|
144
|
+
N.times { Book.create!(title: random_name, year: "19#{rand(10..99)}", author_id: rand(1..N / 2)) }
|
|
145
|
+
|
|
146
|
+
def sort(obj)
|
|
147
|
+
case obj
|
|
148
|
+
when Array then obj.map { |el| sort(el) }
|
|
149
|
+
when Hash then obj.transform_values { |v| sort(v) }
|
|
150
|
+
else obj
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def check_correctness(serializers)
|
|
155
|
+
results = serializers.map(&:call).map { |r| sort(JSON.parse(r)) }
|
|
156
|
+
raise 'Results are not the same' if results.uniq.size > 1
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def benchmark(names, serializers)
|
|
160
|
+
check_correctness(serializers)
|
|
161
|
+
|
|
162
|
+
Benchmark.ips do |x|
|
|
163
|
+
x.config(time: 5, warmup: 2)
|
|
164
|
+
|
|
165
|
+
names.zip(serializers).each { |name, proc| x.report(name, &proc) }
|
|
166
|
+
|
|
167
|
+
x.compare!
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def benchmark_instance(ams_arg: '', oj_arg: '')
|
|
172
|
+
user = User.find(rand(1..N))
|
|
173
|
+
|
|
174
|
+
names = ["AMS#{[ams_arg, oj_arg].join(' ')}: instance",
|
|
175
|
+
'Surrealist: instance through .surrealize',
|
|
176
|
+
'Light: instance through .to_json',
|
|
177
|
+
'Surrealist: instance through Surrealist::Serializer',
|
|
178
|
+
"ActiveModel::Serializers::JSON#{oj_arg} instance",
|
|
179
|
+
"Blueprinter#{oj_arg}"]
|
|
180
|
+
|
|
181
|
+
serializers = [-> { UserAMSSerializer.new(user).to_json },
|
|
182
|
+
-> { user.surrealize },
|
|
183
|
+
-> { UserLightSerializer.new(user).to_json },
|
|
184
|
+
-> { UserSurrealistSerializer.new(user).surrealize },
|
|
185
|
+
-> { user.to_json(only: %i[name email]) },
|
|
186
|
+
-> { UserBlueprint.render(user) }]
|
|
187
|
+
|
|
188
|
+
benchmark(names, serializers)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def benchmark_collection(ams_arg: '', oj_arg: '')
|
|
192
|
+
users = User.all
|
|
193
|
+
|
|
194
|
+
names = ["AMS#{[ams_arg, oj_arg].join(' ')}: collection",
|
|
195
|
+
'Surrealist: collection through Surrealist.surrealize_collection()',
|
|
196
|
+
'Light: collection through .to_json',
|
|
197
|
+
'Surrealist: collection through Surrealist::Serializer',
|
|
198
|
+
"ActiveModel::Serializers::JSON#{oj_arg} collection",
|
|
199
|
+
"Blueprinter collection#{oj_arg}"]
|
|
200
|
+
|
|
201
|
+
serializers = [lambda do
|
|
202
|
+
ActiveModel::Serializer::CollectionSerializer.new(
|
|
203
|
+
users, root: nil, serializer: UserAMSSerializer
|
|
204
|
+
).to_json
|
|
205
|
+
end,
|
|
206
|
+
-> { Surrealist.surrealize_collection(users) },
|
|
207
|
+
-> { UserLightSerializer.new(users).to_json },
|
|
208
|
+
-> { UserSurrealistSerializer.new(users).surrealize },
|
|
209
|
+
-> { users.to_json(only: %i[name email]) },
|
|
210
|
+
-> { UserBlueprint.render(users) }]
|
|
211
|
+
|
|
212
|
+
benchmark(names, serializers)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def benchmark_associations_instance
|
|
216
|
+
instance = Author.find(rand((1..(N / 2))))
|
|
217
|
+
|
|
218
|
+
names = ['AMS (associations): instance',
|
|
219
|
+
'Surrealist (associations): instance through .surrealize',
|
|
220
|
+
'Light (associations): instance through .to_json',
|
|
221
|
+
'Surrealist (associations): instance through Surrealist::Serializer',
|
|
222
|
+
'ActiveModel::Serializers::JSON (associations)',
|
|
223
|
+
'Blueprinter (associations)']
|
|
224
|
+
|
|
225
|
+
serializers = [-> { AuthorAMSSerializer.new(instance).to_json },
|
|
226
|
+
-> { instance.surrealize },
|
|
227
|
+
-> { AuthorLightSerializer.new(instance).to_json },
|
|
228
|
+
-> { AuthorSurrealistSerializer.new(instance).surrealize },
|
|
229
|
+
lambda do
|
|
230
|
+
instance.to_json(only: %i[name last_name age], methods: %i[full_name],
|
|
231
|
+
include: { books: { only: %i[title year] } })
|
|
232
|
+
end,
|
|
233
|
+
-> { AuthorBlueprint.render(instance) }]
|
|
234
|
+
|
|
235
|
+
benchmark(names, serializers)
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def benchmark_associations_collection
|
|
239
|
+
collection = Author.all
|
|
240
|
+
|
|
241
|
+
names = ['AMS (associations): collection',
|
|
242
|
+
'Surrealist (associations): collection through Surrealist.surrealize_collection()',
|
|
243
|
+
'Light (associations): collection through .to_json',
|
|
244
|
+
'Surrealist (associations): collection through Surrealist::Serializer',
|
|
245
|
+
'ActiveModel::Serializers::JSON (associations): collection',
|
|
246
|
+
'Blueprinter (associations): collection']
|
|
247
|
+
|
|
248
|
+
serializers = [lambda do
|
|
249
|
+
ActiveModel::Serializer::CollectionSerializer.new(
|
|
250
|
+
collection, root: nil, serializer: AuthorAMSSerializer
|
|
251
|
+
).to_json
|
|
252
|
+
end,
|
|
253
|
+
-> { Surrealist.surrealize_collection(collection) },
|
|
254
|
+
-> { AuthorLightSerializer.new(collection).to_json },
|
|
255
|
+
-> { AuthorSurrealistSerializer.new(collection).surrealize },
|
|
256
|
+
lambda do
|
|
257
|
+
collection.to_json(only: %i[name last_name age], methods: %i[full_name],
|
|
258
|
+
include: { books: { only: %i[title year] } })
|
|
259
|
+
end,
|
|
260
|
+
-> { AuthorBlueprint.render(collection) }]
|
|
261
|
+
|
|
262
|
+
benchmark(names, serializers)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# Default configuration
|
|
266
|
+
benchmark_instance
|
|
267
|
+
benchmark_collection
|
|
268
|
+
|
|
269
|
+
# With AMS logger turned off
|
|
270
|
+
puts "\n------- Turning off AMS logger -------\n"
|
|
271
|
+
ActiveModelSerializers.logger.level = Logger::Severity::UNKNOWN
|
|
272
|
+
|
|
273
|
+
benchmark_instance(ams_arg: '(without logging)')
|
|
274
|
+
benchmark_collection(ams_arg: '(without logging)')
|
|
275
|
+
|
|
276
|
+
# Associations
|
|
277
|
+
benchmark_associations_instance
|
|
278
|
+
benchmark_associations_collection
|
|
279
|
+
|
|
280
|
+
puts "\n------- Enabling Oj.optimize_rails() & Blueprinter config.generator = Oj -------\n"
|
|
281
|
+
Oj.optimize_rails
|
|
282
|
+
Blueprinter.configure do |config|
|
|
283
|
+
config.generator = Oj
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
benchmark_instance(ams_arg: '(without logging)', oj_arg: '(with Oj)')
|
|
287
|
+
benchmark_collection(ams_arg: '(without logging)', oj_arg: '(with Oj)')
|
|
288
|
+
|
|
289
|
+
# Associations
|
|
290
|
+
benchmark_associations_instance
|
|
291
|
+
benchmark_associations_collection
|
data/bin/test
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LightSerializer
|
|
4
|
+
class HashedObject
|
|
5
|
+
attr_reader :raw_object, :serializer
|
|
6
|
+
|
|
7
|
+
def self.get(*args)
|
|
8
|
+
new(*args).get
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize(raw_object, serializer)
|
|
12
|
+
@raw_object = raw_object
|
|
13
|
+
@serializer = serializer
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def get
|
|
17
|
+
with_custom_root do
|
|
18
|
+
if raw_object.is_a?(Enumerable)
|
|
19
|
+
raw_object.map { |entity| transform_to_hash(entity) }
|
|
20
|
+
else
|
|
21
|
+
transform_to_hash(raw_object)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def with_custom_root
|
|
29
|
+
custom_root ? { custom_root.to_sym => yield } : yield
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def custom_root
|
|
33
|
+
@custom_root ||= serializer.root
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def transform_to_hash(object)
|
|
37
|
+
serializer.class.attributes.each_with_object({}) do |attribute, result|
|
|
38
|
+
obtain_values(attribute, object, result)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def obtain_values(attribute, object, result)
|
|
43
|
+
if attribute.is_a?(Hash)
|
|
44
|
+
values_from_nested_resource(attribute, object, result)
|
|
45
|
+
else
|
|
46
|
+
values_from_current_resource(attribute, object, result)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def values_from_nested_resource(attribute, object, result)
|
|
51
|
+
attribute_name = attribute.keys.last
|
|
52
|
+
nested_serializer = attribute.values.last
|
|
53
|
+
value = obtain_value(object, attribute_name)
|
|
54
|
+
result[attribute_name] = nested_serializer.new(value).to_hash
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def values_from_current_resource(attribute, object, result)
|
|
58
|
+
value = obtain_value(object, attribute)
|
|
59
|
+
|
|
60
|
+
result[attribute] = if value.is_a?(Array)
|
|
61
|
+
values_from_for_array(attribute, value, result)
|
|
62
|
+
else
|
|
63
|
+
value
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def obtain_value(object, attribute)
|
|
68
|
+
if serializer.respond_to?(attribute)
|
|
69
|
+
serializer.public_send(attribute)
|
|
70
|
+
elsif object.respond_to?(attribute)
|
|
71
|
+
object.public_send(attribute)
|
|
72
|
+
else
|
|
73
|
+
object
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def values_from_for_array(attribute, value, result)
|
|
78
|
+
value.map { |entity| obtain_values(attribute, entity, result) }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'oj'
|
|
4
|
+
require 'light_serializer/hashed_object'
|
|
5
|
+
|
|
6
|
+
module LightSerializer
|
|
7
|
+
class Serializer
|
|
8
|
+
attr_reader :object, :root
|
|
9
|
+
|
|
10
|
+
def self.attributes(*new_attributes)
|
|
11
|
+
return @attributes if new_attributes.empty?
|
|
12
|
+
|
|
13
|
+
@attributes = @attributes ? @attributes + new_attributes : new_attributes
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.inherited(subclass)
|
|
17
|
+
subclass.attributes(*attributes)
|
|
18
|
+
super(subclass)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(object, root: nil)
|
|
22
|
+
@object = object
|
|
23
|
+
@root = root
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_json
|
|
27
|
+
Oj.dump(hashed_object, mode: :compat)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def to_hash
|
|
31
|
+
hashed_object
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def hashed_object
|
|
37
|
+
HashedObject.get(object, self)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
|
|
6
|
+
require 'light_serializer/version'
|
|
7
|
+
|
|
8
|
+
Gem::Specification.new do |spec|
|
|
9
|
+
spec.name = 'light_serializer'
|
|
10
|
+
spec.version = LightSerializer::VERSION
|
|
11
|
+
spec.authors = ['krim']
|
|
12
|
+
spec.email = ['pasha.rod@mail.ru']
|
|
13
|
+
|
|
14
|
+
spec.summary = 'Light and Fast Serializer'
|
|
15
|
+
spec.homepage = 'https://github.com/krim/light_serializer'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
|
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
spec.required_ruby_version = '>= 2.3'
|
|
23
|
+
spec.add_dependency 'oj', '~> 3'
|
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.0'
|
|
25
|
+
spec.add_development_dependency 'factory_bot', '~> 4.11'
|
|
26
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.0'
|
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
28
|
+
spec.add_development_dependency 'reek', '~> 5.1'
|
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
30
|
+
spec.add_development_dependency 'rubocop', '~> 0.59.2'
|
|
31
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.30'
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: light_serializer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- krim
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-11-04 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: oj
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: factory_bot
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '4.11'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '4.11'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: pry-byebug
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rake
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '10.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '10.0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: reek
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '5.1'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '5.1'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rspec
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '3.0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '3.0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: rubocop
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - "~>"
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: 0.59.2
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - "~>"
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: 0.59.2
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: rubocop-rspec
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - "~>"
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '1.30'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - "~>"
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '1.30'
|
|
139
|
+
description:
|
|
140
|
+
email:
|
|
141
|
+
- pasha.rod@mail.ru
|
|
142
|
+
executables: []
|
|
143
|
+
extensions: []
|
|
144
|
+
extra_rdoc_files: []
|
|
145
|
+
files:
|
|
146
|
+
- ".gitignore"
|
|
147
|
+
- ".reek.yml"
|
|
148
|
+
- ".rspec"
|
|
149
|
+
- ".rubocop.yml"
|
|
150
|
+
- ".travis.yml"
|
|
151
|
+
- Gemfile
|
|
152
|
+
- Gemfile.lock
|
|
153
|
+
- LICENSE
|
|
154
|
+
- README.md
|
|
155
|
+
- bench.rb
|
|
156
|
+
- bin/test
|
|
157
|
+
- lib/light_serializer.rb
|
|
158
|
+
- lib/light_serializer/hashed_object.rb
|
|
159
|
+
- lib/light_serializer/serializer.rb
|
|
160
|
+
- lib/light_serializer/version.rb
|
|
161
|
+
- light_serializer.gemspec
|
|
162
|
+
homepage: https://github.com/krim/light_serializer
|
|
163
|
+
licenses:
|
|
164
|
+
- MIT
|
|
165
|
+
metadata: {}
|
|
166
|
+
post_install_message:
|
|
167
|
+
rdoc_options: []
|
|
168
|
+
require_paths:
|
|
169
|
+
- lib
|
|
170
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
171
|
+
requirements:
|
|
172
|
+
- - ">="
|
|
173
|
+
- !ruby/object:Gem::Version
|
|
174
|
+
version: '2.3'
|
|
175
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
|
+
requirements:
|
|
177
|
+
- - ">="
|
|
178
|
+
- !ruby/object:Gem::Version
|
|
179
|
+
version: '0'
|
|
180
|
+
requirements: []
|
|
181
|
+
rubyforge_project:
|
|
182
|
+
rubygems_version: 2.7.6
|
|
183
|
+
signing_key:
|
|
184
|
+
specification_version: 4
|
|
185
|
+
summary: Light and Fast Serializer
|
|
186
|
+
test_files: []
|