juso 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +19 -0
- data/README.md +9 -3
- data/benchmark/collection.rb +472 -0
- data/benchmark/single_resource.rb +26 -28
- data/lib/juso/version.rb +1 -1
- data/lib/juso.rb +4 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a51c9bb514c29a43c32b28bfe8bc8b7e1be09c7cbfaa7f6e06e21c8f75bcc059
|
4
|
+
data.tar.gz: 243229044555a5d619327549801d2a6fdcd4b5272ddbb32beadd521e745035b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d9f185580a6f4537aa41e7c18d4db70a69f2f8c11e4b47af1127df01322c0c1f1612278312910bb06076e6b679efc85906cfcf224a99683a9dfefd81eef9f2d
|
7
|
+
data.tar.gz: acf60f8aee9a93f15f2ed461a40b427e10bedf4b10ec273bc7884d7f5f5dd2ab2bb987d3bb550d19d70d4ea5d9bceb1af2619d71f43d29b096e790f3dbd7ef09
|
data/.github/workflows/main.yml
CHANGED
@@ -31,3 +31,22 @@ jobs:
|
|
31
31
|
run: |
|
32
32
|
bundle exec rails db:setup
|
33
33
|
bundle exec rake test
|
34
|
+
|
35
|
+
benchmark:
|
36
|
+
runs-on: ubuntu-latest
|
37
|
+
timeout-minutes: 10
|
38
|
+
|
39
|
+
steps:
|
40
|
+
- uses: actions/checkout@v2
|
41
|
+
|
42
|
+
- name: Set up Ruby
|
43
|
+
uses: ruby/setup-ruby@v1
|
44
|
+
with:
|
45
|
+
ruby-version: 3.0.2
|
46
|
+
bundler-cache: true
|
47
|
+
|
48
|
+
- name: Run benchmark (collection)
|
49
|
+
run: ruby benchmark/collection.rb
|
50
|
+
|
51
|
+
- name: Run benchmark (single)
|
52
|
+
run: ruby benchmark/single_resource.rb
|
data/README.md
CHANGED
@@ -89,8 +89,10 @@ juso メソッドは以下のインスタンスしか返してはいけません
|
|
89
89
|
- False Class
|
90
90
|
- Hash Class
|
91
91
|
- Array Class
|
92
|
+
- - ActiveRecord::Relation
|
92
93
|
- Juso::Serializable を include したクラス
|
93
|
-
- Date / DateTime
|
94
|
+
- Date / DateTime
|
95
|
+
- - ActiveSupport::TimeWithZone
|
94
96
|
|
95
97
|
再帰的に juso の処理が適用されるため、Array の要素や Hash の value も同様のルールが適用されます
|
96
98
|
|
@@ -111,8 +113,12 @@ class UserSerializer
|
|
111
113
|
def juso(context)
|
112
114
|
{
|
113
115
|
id: @user.id,
|
114
|
-
|
115
|
-
|
116
|
+
|
117
|
+
# use PostSerializer#juso method. Each post passes into PostSerializer object.
|
118
|
+
posts: Juso.wrap(@user.posts, PostSerializer),
|
119
|
+
|
120
|
+
# use Team#juso method
|
121
|
+
team: @user.team,
|
116
122
|
}
|
117
123
|
end
|
118
124
|
end
|
@@ -0,0 +1,472 @@
|
|
1
|
+
# original: https://github.com/okuramasafumi/alba/blob/main/benchmark/collection.rb
|
2
|
+
|
3
|
+
# Benchmark script to run varieties of JSON serializers
|
4
|
+
# Fetch juso from local, otherwise fetch latest from RubyGems
|
5
|
+
|
6
|
+
# --- Bundle dependencies ---
|
7
|
+
|
8
|
+
require "bundler/inline"
|
9
|
+
|
10
|
+
gemfile(true) do
|
11
|
+
source "https://rubygems.org"
|
12
|
+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
13
|
+
|
14
|
+
gem "active_model_serializers"
|
15
|
+
gem "activerecord", "6.1.3"
|
16
|
+
gem "alba"
|
17
|
+
gem "juso", path: '../'
|
18
|
+
gem "benchmark-ips"
|
19
|
+
gem "benchmark-memory"
|
20
|
+
gem "blueprinter"
|
21
|
+
gem "jbuilder"
|
22
|
+
gem "jserializer"
|
23
|
+
gem "jsonapi-serializer" # successor of fast_jsonapi
|
24
|
+
gem "multi_json"
|
25
|
+
gem "panko_serializer"
|
26
|
+
gem "pg"
|
27
|
+
gem "primalize"
|
28
|
+
gem "oj"
|
29
|
+
gem "representable"
|
30
|
+
gem "simple_ams"
|
31
|
+
gem "sqlite3"
|
32
|
+
end
|
33
|
+
|
34
|
+
# --- Test data model setup ---
|
35
|
+
|
36
|
+
require "pg"
|
37
|
+
require "active_record"
|
38
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
39
|
+
require "logger"
|
40
|
+
require "oj"
|
41
|
+
require "sqlite3"
|
42
|
+
Oj.optimize_rails
|
43
|
+
|
44
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
45
|
+
# ActiveRecord::Base.logger = Logger.new($stdout)
|
46
|
+
|
47
|
+
ActiveRecord::Schema.define do
|
48
|
+
create_table :posts, force: true do |t|
|
49
|
+
t.string :body
|
50
|
+
end
|
51
|
+
|
52
|
+
create_table :comments, force: true do |t|
|
53
|
+
t.integer :post_id
|
54
|
+
t.string :body
|
55
|
+
t.integer :commenter_id
|
56
|
+
end
|
57
|
+
|
58
|
+
create_table :users, force: true do |t|
|
59
|
+
t.string :name
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Post < ActiveRecord::Base
|
64
|
+
has_many :comments
|
65
|
+
has_many :commenters, through: :comments, class_name: 'User', source: :commenter
|
66
|
+
|
67
|
+
def attributes
|
68
|
+
{id: nil, body: nil, commenter_names: commenter_names}
|
69
|
+
end
|
70
|
+
|
71
|
+
def commenter_names
|
72
|
+
commenters.pluck(:name)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Comment < ActiveRecord::Base
|
77
|
+
belongs_to :post
|
78
|
+
belongs_to :commenter, class_name: 'User'
|
79
|
+
|
80
|
+
def attributes
|
81
|
+
{id: nil, body: nil}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class User < ActiveRecord::Base
|
86
|
+
has_many :comments
|
87
|
+
end
|
88
|
+
|
89
|
+
# --- Juso serializers ---
|
90
|
+
|
91
|
+
require "juso"
|
92
|
+
|
93
|
+
class Comment
|
94
|
+
include ::Juso::Serializable
|
95
|
+
|
96
|
+
def juso(context)
|
97
|
+
{id: id, body: body}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class Post
|
102
|
+
include ::Juso::Serializable
|
103
|
+
|
104
|
+
def juso(context)
|
105
|
+
{id: id, body: body, commenter_names: commenter_names, comments: comments}
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# --- Alba serializers ---
|
110
|
+
|
111
|
+
require "alba"
|
112
|
+
|
113
|
+
class AlbaCommentResource
|
114
|
+
include ::Alba::Resource
|
115
|
+
attributes :id, :body
|
116
|
+
end
|
117
|
+
|
118
|
+
class AlbaPostResource
|
119
|
+
include ::Alba::Resource
|
120
|
+
attributes :id, :body
|
121
|
+
attribute :commenter_names do |post|
|
122
|
+
post.commenters.pluck(:name)
|
123
|
+
end
|
124
|
+
many :comments, resource: AlbaCommentResource
|
125
|
+
end
|
126
|
+
|
127
|
+
# --- ActiveModelSerializer serializers ---
|
128
|
+
|
129
|
+
require "active_model_serializers"
|
130
|
+
|
131
|
+
ActiveModelSerializers.logger = Logger.new(nil)
|
132
|
+
|
133
|
+
class AMSCommentSerializer < ActiveModel::Serializer
|
134
|
+
attributes :id, :body
|
135
|
+
end
|
136
|
+
|
137
|
+
class AMSPostSerializer < ActiveModel::Serializer
|
138
|
+
attributes :id, :body
|
139
|
+
attribute :commenter_names
|
140
|
+
has_many :comments, serializer: AMSCommentSerializer
|
141
|
+
|
142
|
+
def commenter_names
|
143
|
+
object.commenters.pluck(:name)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# --- Blueprint serializers ---
|
148
|
+
|
149
|
+
require "blueprinter"
|
150
|
+
|
151
|
+
class CommentBlueprint < Blueprinter::Base
|
152
|
+
fields :id, :body
|
153
|
+
end
|
154
|
+
|
155
|
+
class PostBlueprint < Blueprinter::Base
|
156
|
+
fields :id, :body, :commenter_names
|
157
|
+
association :comments, blueprint: CommentBlueprint
|
158
|
+
|
159
|
+
def commenter_names
|
160
|
+
commenters.pluck(:name)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# --- JBuilder serializers ---
|
165
|
+
|
166
|
+
require "jbuilder"
|
167
|
+
|
168
|
+
class Post
|
169
|
+
def to_builder
|
170
|
+
Jbuilder.new do |post|
|
171
|
+
post.call(self, :id, :body, :commenter_names, :comments)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def commenter_names
|
176
|
+
commenters.pluck(:name)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
class Comment
|
181
|
+
def to_builder
|
182
|
+
Jbuilder.new do |comment|
|
183
|
+
comment.call(self, :id, :body)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# --- Jserializer serializers ---
|
189
|
+
|
190
|
+
require 'jserializer'
|
191
|
+
|
192
|
+
class JserializerCommentSerializer < Jserializer::Base
|
193
|
+
attributes :id, :body
|
194
|
+
end
|
195
|
+
|
196
|
+
class JserializerPostSerializer < Jserializer::Base
|
197
|
+
attributes :id, :body, :commenter_names
|
198
|
+
has_many :comments, serializer: JserializerCommentSerializer
|
199
|
+
def commenter_names
|
200
|
+
object.commenters.pluck(:name)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# --- JSONAPI:Serializer serializers / (successor of fast_jsonapi) ---
|
205
|
+
|
206
|
+
class JsonApiStandardCommentSerializer
|
207
|
+
include JSONAPI::Serializer
|
208
|
+
|
209
|
+
attribute :id
|
210
|
+
attribute :body
|
211
|
+
end
|
212
|
+
|
213
|
+
class JsonApiStandardPostSerializer
|
214
|
+
include JSONAPI::Serializer
|
215
|
+
|
216
|
+
# set_type :post # optional
|
217
|
+
attribute :id
|
218
|
+
attribute :body
|
219
|
+
attribute :commenter_names
|
220
|
+
|
221
|
+
attribute :comments do |post|
|
222
|
+
post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) }
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
# --- JSONAPI:Serializer serializers that format the code the same flat way as the other gems here ---
|
227
|
+
|
228
|
+
# code to convert from JSON:API output to "flat" JSON, like the other serializers build
|
229
|
+
class JsonApiSameFormatSerializer
|
230
|
+
include JSONAPI::Serializer
|
231
|
+
|
232
|
+
def as_json(*_options)
|
233
|
+
hash = serializable_hash
|
234
|
+
|
235
|
+
if hash[:data].is_a? Hash
|
236
|
+
hash[:data][:attributes]
|
237
|
+
|
238
|
+
elsif hash[:data].is_a? Array
|
239
|
+
hash[:data].pluck(:attributes)
|
240
|
+
|
241
|
+
elsif hash[:data].nil?
|
242
|
+
{ }
|
243
|
+
|
244
|
+
else
|
245
|
+
raise "unexpected data type #{hash[:data].class}"
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
class JsonApiSameFormatCommentSerializer < JsonApiSameFormatSerializer
|
251
|
+
attribute :id
|
252
|
+
attribute :body
|
253
|
+
end
|
254
|
+
|
255
|
+
class JsonApiSameFormatPostSerializer < JsonApiSameFormatSerializer
|
256
|
+
attribute :id
|
257
|
+
attribute :body
|
258
|
+
attribute :commenter_names
|
259
|
+
|
260
|
+
attribute :comments do |post|
|
261
|
+
post.comments.map { |comment| JsonApiSameFormatCommentSerializer.new(comment) }
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
# --- Panko serializers ---
|
266
|
+
#
|
267
|
+
|
268
|
+
require "panko_serializer"
|
269
|
+
|
270
|
+
class PankoCommentSerializer < Panko::Serializer
|
271
|
+
attributes :id, :body
|
272
|
+
end
|
273
|
+
|
274
|
+
|
275
|
+
class PankoPostSerializer < Panko::Serializer
|
276
|
+
attributes :id, :body, :commenter_names
|
277
|
+
|
278
|
+
has_many :comments, serializer: PankoCommentSerializer
|
279
|
+
|
280
|
+
def commenter_names
|
281
|
+
object.comments.pluck(:name)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
# --- Primalize serializers ---
|
286
|
+
#
|
287
|
+
class PrimalizeCommentResource < Primalize::Single
|
288
|
+
attributes id: integer, body: string
|
289
|
+
end
|
290
|
+
|
291
|
+
class PrimalizePostResource < Primalize::Single
|
292
|
+
alias post object
|
293
|
+
|
294
|
+
attributes(
|
295
|
+
id: integer,
|
296
|
+
body: string,
|
297
|
+
comments: array(primalize(PrimalizeCommentResource)),
|
298
|
+
commenter_names: array(string),
|
299
|
+
)
|
300
|
+
|
301
|
+
def commenter_names
|
302
|
+
post.commenters.pluck(:name)
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
class PrimalizePostsResource < Primalize::Many
|
307
|
+
attributes posts: enumerable(PrimalizePostResource)
|
308
|
+
end
|
309
|
+
|
310
|
+
# --- Representable serializers ---
|
311
|
+
|
312
|
+
require "representable"
|
313
|
+
|
314
|
+
class CommentRepresenter < Representable::Decorator
|
315
|
+
include Representable::JSON
|
316
|
+
|
317
|
+
property :id
|
318
|
+
property :body
|
319
|
+
end
|
320
|
+
|
321
|
+
class PostsRepresenter < Representable::Decorator
|
322
|
+
include Representable::JSON::Collection
|
323
|
+
|
324
|
+
items class: Post do
|
325
|
+
property :id
|
326
|
+
property :body
|
327
|
+
property :commenter_names
|
328
|
+
collection :comments
|
329
|
+
end
|
330
|
+
|
331
|
+
def commenter_names
|
332
|
+
commenters.pluck(:name)
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
# --- SimpleAMS serializers ---
|
337
|
+
|
338
|
+
require "simple_ams"
|
339
|
+
|
340
|
+
class SimpleAMSCommentSerializer
|
341
|
+
include SimpleAMS::DSL
|
342
|
+
|
343
|
+
attributes :id, :body
|
344
|
+
end
|
345
|
+
|
346
|
+
class SimpleAMSPostSerializer
|
347
|
+
include SimpleAMS::DSL
|
348
|
+
|
349
|
+
attributes :id, :body
|
350
|
+
attribute :commenter_names
|
351
|
+
has_many :comments, serializer: SimpleAMSCommentSerializer
|
352
|
+
|
353
|
+
def commenter_names
|
354
|
+
object.commenters.pluck(:name)
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
# --- Test data creation ---
|
359
|
+
|
360
|
+
100.times do |i|
|
361
|
+
post = Post.create!(body: "post#{i}")
|
362
|
+
user1 = User.create!(name: "John#{i}")
|
363
|
+
user2 = User.create!(name: "Jane#{i}")
|
364
|
+
10.times do |n|
|
365
|
+
post.comments.create!(commenter: user1, body: "Comment1_#{i}_#{n}")
|
366
|
+
post.comments.create!(commenter: user2, body: "Comment2_#{i}_#{n}")
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
posts = Post.all.to_a
|
371
|
+
|
372
|
+
# --- Store the serializers in procs ---
|
373
|
+
|
374
|
+
alba = Proc.new { AlbaPostResource.new(posts).serialize }
|
375
|
+
alba_inline = Proc.new do
|
376
|
+
Alba.serialize(posts) do
|
377
|
+
attributes :id, :body
|
378
|
+
attribute :commenter_names do |post|
|
379
|
+
post.commenters.pluck(:name)
|
380
|
+
end
|
381
|
+
many :comments do
|
382
|
+
attributes :id, :body
|
383
|
+
end
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
juso = Proc.new do
|
388
|
+
Juso.generate(posts)
|
389
|
+
end
|
390
|
+
|
391
|
+
ams = Proc.new { ActiveModelSerializers::SerializableResource.new(posts, {}).as_json }
|
392
|
+
blueprinter = Proc.new { PostBlueprint.render(posts) }
|
393
|
+
jbuilder = Proc.new do
|
394
|
+
Jbuilder.new do |json|
|
395
|
+
json.array!(posts) do |post|
|
396
|
+
json.post post.to_builder
|
397
|
+
end
|
398
|
+
end.target!
|
399
|
+
end
|
400
|
+
jserializer = Proc.new { JserializerPostSerializer.new(posts, is_collection: true).to_json }
|
401
|
+
jsonapi = proc { JsonApiStandardPostSerializer.new(posts).to_json }
|
402
|
+
jsonapi_same_format = proc { JsonApiSameFormatPostSerializer.new(posts).to_json }
|
403
|
+
panko = proc { Panko::ArraySerializer.new(posts, each_serializer: PankoPostSerializer).to_json }
|
404
|
+
primalize = proc { PrimalizePostsResource.new(posts: posts).to_json }
|
405
|
+
rails = Proc.new do
|
406
|
+
ActiveSupport::JSON.encode(posts.map{ |post| post.serializable_hash(include: :comments) })
|
407
|
+
end
|
408
|
+
representable = Proc.new { PostsRepresenter.new(posts).to_json }
|
409
|
+
simple_ams = Proc.new { SimpleAMS::Renderer::Collection.new(posts, serializer: SimpleAMSPostSerializer).to_json }
|
410
|
+
|
411
|
+
# --- Execute the serializers to check their output ---
|
412
|
+
|
413
|
+
puts "Serializer outputs ----------------------------------"
|
414
|
+
{
|
415
|
+
alba: alba,
|
416
|
+
alba_inline: alba_inline,
|
417
|
+
juso: juso,
|
418
|
+
ams: ams,
|
419
|
+
blueprinter: blueprinter,
|
420
|
+
jbuilder: jbuilder, # different order
|
421
|
+
jserializer: jserializer,
|
422
|
+
jsonapi: jsonapi, # nested JSON:API format
|
423
|
+
jsonapi_same_format: jsonapi_same_format,
|
424
|
+
panko: panko,
|
425
|
+
primalize: primalize,
|
426
|
+
rails: rails,
|
427
|
+
representable: representable,
|
428
|
+
simple_ams: simple_ams,
|
429
|
+
}.each { |name, serializer| puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}" }
|
430
|
+
|
431
|
+
# --- Run the benchmarks ---
|
432
|
+
|
433
|
+
require 'benchmark/ips'
|
434
|
+
Benchmark.ips do |x|
|
435
|
+
x.report(:alba, &alba)
|
436
|
+
x.report(:alba_inline, &alba_inline)
|
437
|
+
x.report(:juso, &juso)
|
438
|
+
x.report(:ams, &ams)
|
439
|
+
x.report(:blueprinter, &blueprinter)
|
440
|
+
x.report(:jbuilder, &jbuilder)
|
441
|
+
x.report(:jserializer, &jserializer)
|
442
|
+
x.report(:jsonapi, &jsonapi)
|
443
|
+
x.report(:jsonapi_same_format, &jsonapi_same_format)
|
444
|
+
x.report(:panko, &panko)
|
445
|
+
x.report(:primalize, &primalize)
|
446
|
+
x.report(:rails, &rails)
|
447
|
+
x.report(:representable, &representable)
|
448
|
+
x.report(:simple_ams, &simple_ams)
|
449
|
+
|
450
|
+
x.compare!
|
451
|
+
end
|
452
|
+
|
453
|
+
|
454
|
+
require 'benchmark/memory'
|
455
|
+
Benchmark.memory do |x|
|
456
|
+
x.report(:alba, &alba)
|
457
|
+
x.report(:alba_inline, &alba_inline)
|
458
|
+
x.report(:juso, &juso)
|
459
|
+
x.report(:ams, &ams)
|
460
|
+
x.report(:blueprinter, &blueprinter)
|
461
|
+
x.report(:jbuilder, &jbuilder)
|
462
|
+
x.report(:jserializer, &jserializer)
|
463
|
+
x.report(:jsonapi, &jsonapi)
|
464
|
+
x.report(:jsonapi_same_format, &jsonapi_same_format)
|
465
|
+
x.report(:panko, &panko)
|
466
|
+
x.report(:primalize, &primalize)
|
467
|
+
x.report(:rails, &rails)
|
468
|
+
x.report(:representable, &representable)
|
469
|
+
x.report(:simple_ams, &simple_ams)
|
470
|
+
|
471
|
+
x.compare!
|
472
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# original from https://github.com/okuramasafumi/alba/blob/main/benchmark/single_resource.rb
|
2
2
|
|
3
3
|
# Benchmark script to run varieties of JSON serializers
|
4
|
-
# Fetch
|
4
|
+
# Fetch juso from local, otherwise fetch latest from RubyGems
|
5
5
|
|
6
6
|
# --- Bundle dependencies ---
|
7
7
|
|
@@ -22,10 +22,10 @@ gemfile(true) do
|
|
22
22
|
gem "jserializer"
|
23
23
|
gem "jsonapi-serializer" # successor of fast_jsonapi
|
24
24
|
gem "multi_json"
|
25
|
-
|
26
|
-
|
25
|
+
gem "panko_serializer"
|
26
|
+
gem "pg"
|
27
27
|
gem "primalize"
|
28
|
-
|
28
|
+
gem "oj"
|
29
29
|
gem "representable"
|
30
30
|
gem "simple_ams"
|
31
31
|
gem "sqlite3"
|
@@ -33,14 +33,14 @@ end
|
|
33
33
|
|
34
34
|
# --- Test data model setup ---
|
35
35
|
|
36
|
-
|
36
|
+
require "pg"
|
37
37
|
require "active_record"
|
38
|
-
|
38
|
+
require "active_record/connection_adapters/postgresql_adapter"
|
39
39
|
require "active_record/connection_adapters/sqlite3_adapter"
|
40
40
|
require "logger"
|
41
|
-
|
41
|
+
require "oj"
|
42
42
|
require "sqlite3"
|
43
|
-
|
43
|
+
Oj.optimize_rails
|
44
44
|
|
45
45
|
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
46
46
|
# ActiveRecord::Base.logger = Logger.new($stdout)
|
@@ -264,22 +264,22 @@ end
|
|
264
264
|
|
265
265
|
# --- Panko serializers ---
|
266
266
|
#
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
267
|
+
require "panko_serializer"
|
268
|
+
|
269
|
+
class PankoCommentSerializer < Panko::Serializer
|
270
|
+
attributes :id, :body
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
class PankoPostSerializer < Panko::Serializer
|
275
|
+
attributes :id, :body, :commenter_names
|
276
|
+
|
277
|
+
has_many :comments, serializer: PankoCommentSerializer
|
278
|
+
|
279
|
+
def commenter_names
|
280
|
+
object.comments.pluck(:name)
|
281
|
+
end
|
282
|
+
end
|
283
283
|
|
284
284
|
# --- Primalize serializers ---
|
285
285
|
#
|
@@ -361,8 +361,6 @@ post.reload
|
|
361
361
|
|
362
362
|
juso = Proc.new do
|
363
363
|
Juso.generate(post)
|
364
|
-
rescue
|
365
|
-
binding.irb
|
366
364
|
end
|
367
365
|
|
368
366
|
alba = Proc.new { AlbaPostResource.new(post).serialize }
|
@@ -383,8 +381,8 @@ jbuilder = Proc.new { post.to_builder.target! }
|
|
383
381
|
jserializer = Proc.new { JserializerPostSerializer.new(post).to_json }
|
384
382
|
jsonapi = proc { JsonApiStandardPostSerializer.new(post).to_json }
|
385
383
|
jsonapi_same_format = proc { JsonApiSameFormatPostSerializer.new(post).to_json }
|
386
|
-
|
387
|
-
panko = proc { 'nop' }
|
384
|
+
panko = proc { PankoPostSerializer.new.serialize_to_json(post) }
|
385
|
+
# panko = proc { 'nop' }
|
388
386
|
primalize = proc { PrimalizePostResource.new(post).to_json }
|
389
387
|
rails = Proc.new { ActiveSupport::JSON.encode(post.serializable_hash(include: :comments)) }
|
390
388
|
representable = Proc.new { PostRepresenter.new(post).to_json }
|
data/lib/juso/version.rb
CHANGED
data/lib/juso.rb
CHANGED
@@ -27,11 +27,11 @@ module Juso
|
|
27
27
|
end
|
28
28
|
|
29
29
|
# Juso.generate generates json string
|
30
|
-
def self.generate(object, context
|
30
|
+
def self.generate(object, context = Context.new)
|
31
31
|
JSON.fast_generate(_g(object, context))
|
32
32
|
end
|
33
33
|
|
34
|
-
# generate returns
|
34
|
+
# generate returns object (as json)
|
35
35
|
def self._g(object, context)
|
36
36
|
case object
|
37
37
|
when nil, Numeric, String, true, false
|
@@ -46,7 +46,7 @@ module Juso
|
|
46
46
|
when *collection_classes
|
47
47
|
return object.to_a.map { |o| _g(o, context) }
|
48
48
|
when *date_classes
|
49
|
-
return object.iso8601
|
49
|
+
return object.iso8601(context.options[:juso_time_n_digits] || 0)
|
50
50
|
else
|
51
51
|
# TODO: fallback to respond_to?(:juso) and warn?
|
52
52
|
|
@@ -87,5 +87,5 @@ module Juso
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
private_class_method :default_collection_classes, :default_date_classes
|
90
|
+
private_class_method :default_collection_classes, :default_date_classes
|
91
91
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: juso
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ykpythemind
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- LICENSE.txt
|
41
41
|
- README.md
|
42
42
|
- Rakefile
|
43
|
+
- benchmark/collection.rb
|
43
44
|
- benchmark/single_resource.rb
|
44
45
|
- bin/console
|
45
46
|
- bin/setup
|