fast-json-serializer 0.1.0
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 +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.md +7 -0
- data/README.md +407 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fast-json-serializer.gemspec +30 -0
- data/lib/fast/json/serializer/attr.rb +26 -0
- data/lib/fast/json/serializer/base.rb +77 -0
- data/lib/fast/json/serializer/version.rb +7 -0
- data/lib/fast/json/serializer.rb +9 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9262c9b36b7b32aac565c250084813fc848884395f1e99f86c35a8b717019a55
|
4
|
+
data.tar.gz: 480b1221d0a03340d205e7e03b7cc4c6a526335cfb3a87f5aeba0b4c7e57157c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c14e14015dfe5ebb666e9a0e3708d4ff320af3527e77ae907a938b884bfbd09f1f59d886869f412f4f4cabc487848f1149ac107c12c5b25f745939fd81a1bedb
|
7
|
+
data.tar.gz: 5510f2ad82be87732ec17f703cd0a1b77529b009bc9440cc2aa7366695468e0f0d677da4e5605d68c2058fda231bcb046ae48cbd640b474ea854e1ad6989cd81
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fast-json-serializer (0.1.0)
|
5
|
+
oj (~> 3.13)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (7.0.4)
|
11
|
+
activesupport (= 7.0.4)
|
12
|
+
activerecord (7.0.4)
|
13
|
+
activemodel (= 7.0.4)
|
14
|
+
activesupport (= 7.0.4)
|
15
|
+
activesupport (7.0.4)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
concurrent-ruby (1.1.10)
|
21
|
+
diff-lcs (1.5.0)
|
22
|
+
i18n (1.12.0)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
minitest (5.16.3)
|
25
|
+
oj (3.13.21)
|
26
|
+
pg (1.4.4)
|
27
|
+
rake (12.3.3)
|
28
|
+
rspec (3.11.0)
|
29
|
+
rspec-core (~> 3.11.0)
|
30
|
+
rspec-expectations (~> 3.11.0)
|
31
|
+
rspec-mocks (~> 3.11.0)
|
32
|
+
rspec-core (3.11.0)
|
33
|
+
rspec-support (~> 3.11.0)
|
34
|
+
rspec-expectations (3.11.1)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.11.0)
|
37
|
+
rspec-mocks (3.11.1)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.11.0)
|
40
|
+
rspec-support (3.11.1)
|
41
|
+
tzinfo (2.0.5)
|
42
|
+
concurrent-ruby (~> 1.0)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
activerecord (>= 5.0, < 7.1)
|
49
|
+
fast-json-serializer!
|
50
|
+
oj (~> 3.13)
|
51
|
+
pg (>= 1.1.1, < 2.0)
|
52
|
+
rake (~> 12.0)
|
53
|
+
rspec (~> 3.0)
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
2.3.7
|
data/LICENSE.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2022 Santanu Karmakar
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,407 @@
|
|
1
|
+
# Fast::Json::Serializer
|
2
|
+
|
3
|
+
This gem generates the JSON bypassing ActiveRecord object creation, which make JSON generation blazing fast! It uses `ActiveRecord::Base.connection.execute` method which returns `PG::Result`. JSON is generated by iterating over the collection.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'fast-json-serializer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install fast-json-serializer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Create a `app/serializers/application_serializer.rb` with the following code:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# app/serializers/application_serializer.rb
|
27
|
+
|
28
|
+
require 'fast/json/serializer/base'
|
29
|
+
|
30
|
+
class ApplicationSerializer < Fast::Json::Serializer::Base
|
31
|
+
end
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
Make the other serializers a subclass of the `ApplicationSerializer` class
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# app/serializers/post_serializer.rb
|
39
|
+
|
40
|
+
class PostSerializer < ApplicationSerializer
|
41
|
+
# enable FastJsonapi format by uncommenting this line
|
42
|
+
# set_type :post
|
43
|
+
|
44
|
+
attributes :id, :title, :creator_name, :creator_email
|
45
|
+
|
46
|
+
# use a ruby/rails formatter
|
47
|
+
attribute :created_at, format: :to_s
|
48
|
+
attribute :updated_at, format: :to_s
|
49
|
+
|
50
|
+
# rename the json key name
|
51
|
+
attribute :created_at_epoch, column_name: :created_at, format: :to_i
|
52
|
+
attribute :updated_at_epoch, column_name: :updated_at, format: :to_i
|
53
|
+
|
54
|
+
# use custom method
|
55
|
+
attribute :created_at_ist do |serializer, hash|
|
56
|
+
serializer.ist_time(hash['created_at'])
|
57
|
+
end
|
58
|
+
|
59
|
+
attribute :updated_at_ist do |serializer, hash|
|
60
|
+
serializer.ist_time(hash['updated_at'])
|
61
|
+
end
|
62
|
+
|
63
|
+
# a class method can be used as well
|
64
|
+
def ist_time(timestamp)
|
65
|
+
return unless timestamp
|
66
|
+
|
67
|
+
timestamp.in_time_zone("Kolkata").strftime('%A - %b %d, %Y')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
### Formatting of the JSON
|
74
|
+
|
75
|
+
Currently, it supports two formats, key-value, and FastJsonapi format
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
PostSerializer.new(Post.limit(10)).serialized_json
|
79
|
+
```
|
80
|
+
|
81
|
+
produces, with `set_type :post` enabled:
|
82
|
+
|
83
|
+
```json
|
84
|
+
{
|
85
|
+
"data": [
|
86
|
+
{
|
87
|
+
"id": 1,
|
88
|
+
"type": "post",
|
89
|
+
"attributes": {
|
90
|
+
"id": 1,
|
91
|
+
"title": "Tacos",
|
92
|
+
"creator_name": "Ella Erdman",
|
93
|
+
"creator_email": "sean.zieme@smitham.name",
|
94
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
95
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
96
|
+
"created_at_epoch": 1665640268,
|
97
|
+
"updated_at_epoch": 1665640268,
|
98
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
99
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
100
|
+
}
|
101
|
+
},
|
102
|
+
{
|
103
|
+
"id": 2,
|
104
|
+
"type": "post",
|
105
|
+
"attributes": {
|
106
|
+
"id": 2,
|
107
|
+
"title": "Barbecue Ribs",
|
108
|
+
"creator_name": "Mrs. Fern Yundt",
|
109
|
+
"creator_email": "rafael.simonis@metz.com",
|
110
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
111
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
112
|
+
"created_at_epoch": 1665640268,
|
113
|
+
"updated_at_epoch": 1665640268,
|
114
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
115
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
116
|
+
}
|
117
|
+
},
|
118
|
+
{
|
119
|
+
"id": 69,
|
120
|
+
"type": "post",
|
121
|
+
"attributes": {
|
122
|
+
"id": 69,
|
123
|
+
"title": "French Fries with Sausages",
|
124
|
+
"creator_name": "Vance Connelly",
|
125
|
+
"creator_email": "leslee_langosh@mann.biz",
|
126
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
127
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
128
|
+
"created_at_epoch": 1665640268,
|
129
|
+
"updated_at_epoch": 1665640268,
|
130
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
131
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
132
|
+
}
|
133
|
+
},
|
134
|
+
{
|
135
|
+
"id": 3,
|
136
|
+
"type": "post",
|
137
|
+
"attributes": {
|
138
|
+
"id": 3,
|
139
|
+
"title": "Pork Belly Buns",
|
140
|
+
"creator_name": "Mrs. Willie Green",
|
141
|
+
"creator_email": "aracely_tromp@torphy-jenkins.info",
|
142
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
143
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
144
|
+
"created_at_epoch": 1665640268,
|
145
|
+
"updated_at_epoch": 1665640268,
|
146
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
147
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
148
|
+
}
|
149
|
+
},
|
150
|
+
{
|
151
|
+
"id": 4,
|
152
|
+
"type": "post",
|
153
|
+
"attributes": {
|
154
|
+
"id": 4,
|
155
|
+
"title": "Peking Duck",
|
156
|
+
"creator_name": "Joselyn VonRueden",
|
157
|
+
"creator_email": "marianna_damore@lehner-metz.org",
|
158
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
159
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
160
|
+
"created_at_epoch": 1665640268,
|
161
|
+
"updated_at_epoch": 1665640268,
|
162
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
163
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
164
|
+
}
|
165
|
+
},
|
166
|
+
{
|
167
|
+
"id": 5,
|
168
|
+
"type": "post",
|
169
|
+
"attributes": {
|
170
|
+
"id": 5,
|
171
|
+
"title": "Hummus",
|
172
|
+
"creator_name": "Stacee Pfannerstill",
|
173
|
+
"creator_email": "barrett_johnston@weimann-gottlieb.name",
|
174
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
175
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
176
|
+
"created_at_epoch": 1665640268,
|
177
|
+
"updated_at_epoch": 1665640268,
|
178
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
179
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
180
|
+
}
|
181
|
+
},
|
182
|
+
{
|
183
|
+
"id": 6,
|
184
|
+
"type": "post",
|
185
|
+
"attributes": {
|
186
|
+
"id": 6,
|
187
|
+
"title": "California Maki",
|
188
|
+
"creator_name": "Fidel Schroeder",
|
189
|
+
"creator_email": "marquitta@kuvalis.info",
|
190
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
191
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
192
|
+
"created_at_epoch": 1665640268,
|
193
|
+
"updated_at_epoch": 1665640268,
|
194
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
195
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
196
|
+
}
|
197
|
+
},
|
198
|
+
{
|
199
|
+
"id": 7,
|
200
|
+
"type": "post",
|
201
|
+
"attributes": {
|
202
|
+
"id": 7,
|
203
|
+
"title": "French Toast",
|
204
|
+
"creator_name": "Alvaro Berge",
|
205
|
+
"creator_email": "randal_littel@fadel-fritsch.io",
|
206
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
207
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
208
|
+
"created_at_epoch": 1665640268,
|
209
|
+
"updated_at_epoch": 1665640268,
|
210
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
211
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
212
|
+
}
|
213
|
+
},
|
214
|
+
{
|
215
|
+
"id": 8,
|
216
|
+
"type": "post",
|
217
|
+
"attributes": {
|
218
|
+
"id": 8,
|
219
|
+
"title": "Arepas",
|
220
|
+
"creator_name": "Alva Kris DO",
|
221
|
+
"creator_email": "nathaniel_strosin@wintheiser.net",
|
222
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
223
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
224
|
+
"created_at_epoch": 1665640268,
|
225
|
+
"updated_at_epoch": 1665640268,
|
226
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
227
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
228
|
+
}
|
229
|
+
},
|
230
|
+
{
|
231
|
+
"id": 9,
|
232
|
+
"type": "post",
|
233
|
+
"attributes": {
|
234
|
+
"id": 9,
|
235
|
+
"title": "Meatballs with Sauce",
|
236
|
+
"creator_name": "Willie Kuhic",
|
237
|
+
"creator_email": "paul.rowe@mraz.biz",
|
238
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
239
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
240
|
+
"created_at_epoch": 1665640268,
|
241
|
+
"updated_at_epoch": 1665640268,
|
242
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
243
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
244
|
+
}
|
245
|
+
}
|
246
|
+
]
|
247
|
+
}
|
248
|
+
|
249
|
+
```
|
250
|
+
|
251
|
+
and with `set_type :post` disabled
|
252
|
+
|
253
|
+
```json
|
254
|
+
[
|
255
|
+
{
|
256
|
+
"id": 1,
|
257
|
+
"title": "Tacos",
|
258
|
+
"creator_name": "Ella Erdman",
|
259
|
+
"creator_email": "sean.zieme@smitham.name",
|
260
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
261
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
262
|
+
"created_at_epoch": 1665640268,
|
263
|
+
"updated_at_epoch": 1665640268,
|
264
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
265
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
266
|
+
},
|
267
|
+
{
|
268
|
+
"id": 2,
|
269
|
+
"title": "Barbecue Ribs",
|
270
|
+
"creator_name": "Mrs. Fern Yundt",
|
271
|
+
"creator_email": "rafael.simonis@metz.com",
|
272
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
273
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
274
|
+
"created_at_epoch": 1665640268,
|
275
|
+
"updated_at_epoch": 1665640268,
|
276
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
277
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
278
|
+
},
|
279
|
+
{
|
280
|
+
"id": 69,
|
281
|
+
"title": "French Fries with Sausages",
|
282
|
+
"creator_name": "Vance Connelly",
|
283
|
+
"creator_email": "leslee_langosh@mann.biz",
|
284
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
285
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
286
|
+
"created_at_epoch": 1665640268,
|
287
|
+
"updated_at_epoch": 1665640268,
|
288
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
289
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
290
|
+
},
|
291
|
+
{
|
292
|
+
"id": 3,
|
293
|
+
"title": "Pork Belly Buns",
|
294
|
+
"creator_name": "Mrs. Willie Green",
|
295
|
+
"creator_email": "aracely_tromp@torphy-jenkins.info",
|
296
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
297
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
298
|
+
"created_at_epoch": 1665640268,
|
299
|
+
"updated_at_epoch": 1665640268,
|
300
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
301
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
302
|
+
},
|
303
|
+
{
|
304
|
+
"id": 4,
|
305
|
+
"title": "Peking Duck",
|
306
|
+
"creator_name": "Joselyn VonRueden",
|
307
|
+
"creator_email": "marianna_damore@lehner-metz.org",
|
308
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
309
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
310
|
+
"created_at_epoch": 1665640268,
|
311
|
+
"updated_at_epoch": 1665640268,
|
312
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
313
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
314
|
+
},
|
315
|
+
{
|
316
|
+
"id": 5,
|
317
|
+
"title": "Hummus",
|
318
|
+
"creator_name": "Stacee Pfannerstill",
|
319
|
+
"creator_email": "barrett_johnston@weimann-gottlieb.name",
|
320
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
321
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
322
|
+
"created_at_epoch": 1665640268,
|
323
|
+
"updated_at_epoch": 1665640268,
|
324
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
325
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
326
|
+
},
|
327
|
+
{
|
328
|
+
"id": 6,
|
329
|
+
"title": "California Maki",
|
330
|
+
"creator_name": "Fidel Schroeder",
|
331
|
+
"creator_email": "marquitta@kuvalis.info",
|
332
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
333
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
334
|
+
"created_at_epoch": 1665640268,
|
335
|
+
"updated_at_epoch": 1665640268,
|
336
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
337
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
338
|
+
},
|
339
|
+
{
|
340
|
+
"id": 7,
|
341
|
+
"title": "French Toast",
|
342
|
+
"creator_name": "Alvaro Berge",
|
343
|
+
"creator_email": "randal_littel@fadel-fritsch.io",
|
344
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
345
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
346
|
+
"created_at_epoch": 1665640268,
|
347
|
+
"updated_at_epoch": 1665640268,
|
348
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
349
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
350
|
+
},
|
351
|
+
{
|
352
|
+
"id": 8,
|
353
|
+
"title": "Arepas",
|
354
|
+
"creator_name": "Alva Kris DO",
|
355
|
+
"creator_email": "nathaniel_strosin@wintheiser.net",
|
356
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
357
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
358
|
+
"created_at_epoch": 1665640268,
|
359
|
+
"updated_at_epoch": 1665640268,
|
360
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
361
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
362
|
+
},
|
363
|
+
{
|
364
|
+
"id": 9,
|
365
|
+
"title": "Meatballs with Sauce",
|
366
|
+
"creator_name": "Willie Kuhic",
|
367
|
+
"creator_email": "paul.rowe@mraz.biz",
|
368
|
+
"created_at": "2022-10-13 05:51:08 UTC",
|
369
|
+
"updated_at": "2022-10-13 05:51:08 UTC",
|
370
|
+
"created_at_epoch": 1665640268,
|
371
|
+
"updated_at_epoch": 1665640268,
|
372
|
+
"created_at_ist": "Thursday - Oct 13, 2022",
|
373
|
+
"updated_at_ist": "Thursday - Oct 13, 2022"
|
374
|
+
}
|
375
|
+
]
|
376
|
+
```
|
377
|
+
|
378
|
+
## Benchmark, rails 7.0.2.3, ruby 3.0.3
|
379
|
+
|
380
|
+
```ruby
|
381
|
+
|
382
|
+
# there are 1100 posts in the DB, having fields
|
383
|
+
# :id, :title, :content, :creator_name, :creator_email, :created_at, :updated_at
|
384
|
+
posts = Post.all
|
385
|
+
s = PostSerializer.new(posts)
|
386
|
+
|
387
|
+
n = 1000
|
388
|
+
Benchmark.bm do |x|
|
389
|
+
x.report("ActiveModel Serializer") { n.times { posts.to_json } }
|
390
|
+
x.report("JSON:API Serialization") { n.times { PostJsonapiSerializer.new(posts).serializable_hash.to_json }}
|
391
|
+
x.report("Blueprinter") { n.times { PostBlueprint.render(posts) }}
|
392
|
+
x.report("FastJsonSerializer") { n.times { s.serialized_json } }
|
393
|
+
end
|
394
|
+
|
395
|
+
# result
|
396
|
+
user system total real
|
397
|
+
ActiveModel Serializer 35.373407 0.000000 35.373407 ( 35.380744)
|
398
|
+
JSON:API Serialization 50.476935 0.043584 50.520519 ( 50.546082)
|
399
|
+
Blueprinter 20.458713 0.000000 20.458713 ( 20.484488)
|
400
|
+
FastJsonSerializer 5.239119 0.015989 5.255108 ( 5.256627)
|
401
|
+
|
402
|
+
```
|
403
|
+
|
404
|
+
## Contributing
|
405
|
+
|
406
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/skarmakar/fast-json-serializer.
|
407
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'fast/json/serializer'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'lib/fast/json/serializer/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'fast-json-serializer'
|
5
|
+
spec.version = Fast::Json::Serializer::VERSION
|
6
|
+
spec.authors = ['Santanu Karmakar']
|
7
|
+
spec.email = ['skarmakar.personal@gmail.com']
|
8
|
+
|
9
|
+
spec.summary = 'Ruby JSON Serializer. ActiveRecord dependent'
|
10
|
+
spec.description = 'Generate JSON bypassing ActiveRecord object building. Directly from PG result'
|
11
|
+
spec.homepage = 'https://github.com/skarmakar/fast-json-serializer'
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
13
|
+
|
14
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
15
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
16
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_dependency 'oj', '~> 3.13'
|
28
|
+
spec.add_development_dependency 'activerecord', '>= 5.0', '< 7.1'
|
29
|
+
spec.add_development_dependency 'pg', '>= 1.1.1', '< 2.0'
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Fast
|
2
|
+
module Json
|
3
|
+
module Serializer
|
4
|
+
class Attr
|
5
|
+
attr_accessor :name, :block, :column_name, :format
|
6
|
+
|
7
|
+
# options can be
|
8
|
+
# - column_name, from which column to get value
|
9
|
+
# - format, method to be called on the value
|
10
|
+
def initialize(name, options, &block)
|
11
|
+
@name = name
|
12
|
+
@block = block
|
13
|
+
@column_name = options[:column_name]&.to_s
|
14
|
+
@format = options[:format]
|
15
|
+
end
|
16
|
+
|
17
|
+
def calc_value(serializer, hash)
|
18
|
+
return block.call(serializer, hash) if block
|
19
|
+
|
20
|
+
value = hash[column_name || name]
|
21
|
+
format ? value.send(format) : value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'oj'
|
2
|
+
require 'active_record'
|
3
|
+
require_relative 'attr'
|
4
|
+
|
5
|
+
module Fast
|
6
|
+
module Json
|
7
|
+
module Serializer
|
8
|
+
class Base
|
9
|
+
attr_accessor :query
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def set_id(attr_name)
|
13
|
+
@id_column = attr_name.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
# if set_type is not defined, it will return flat result, ie, not fast json api format
|
17
|
+
def set_type(attr_name)
|
18
|
+
@resource_type = attr_name.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def attributes(*attr_names)
|
22
|
+
attr_names.each { |attr_name| attribute(attr_name) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def attribute(attr_name, options = {}, &block)
|
26
|
+
@serializable_attributes ||= []
|
27
|
+
fast_attr = Fast::Json::Serializer::Attr.new(attr_name.to_s, options, &block)
|
28
|
+
@serializable_attributes.append(fast_attr)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(query)
|
33
|
+
@query = (query.is_a?(String) ? query : query.to_sql).squish
|
34
|
+
end
|
35
|
+
|
36
|
+
# https://www.rubydoc.info/gems/pg/PG/Result
|
37
|
+
def pg_result
|
38
|
+
@pg_result ||= ActiveRecord::Base.connection.execute(query)
|
39
|
+
end
|
40
|
+
|
41
|
+
def serializable_hash(data_only: false)
|
42
|
+
resource_type = self.class.instance_variable_get('@resource_type')
|
43
|
+
serializable_attributes = self.class.instance_variable_get('@serializable_attributes')
|
44
|
+
id_key = self.class.instance_variable_get('@id_column') || 'id'
|
45
|
+
|
46
|
+
array_of_hash = pg_result.collect do |hash|
|
47
|
+
build_formatted_hash(hash, resource_type, serializable_attributes, id_key)
|
48
|
+
end
|
49
|
+
|
50
|
+
return array_of_hash if data_only || !resource_type
|
51
|
+
|
52
|
+
{ 'data' => array_of_hash }
|
53
|
+
end
|
54
|
+
|
55
|
+
def build_formatted_hash(hash, resource_type, serializable_attributes, id_key)
|
56
|
+
attributes = {}.tap do |h|
|
57
|
+
serializable_attributes.collect do |fast_attr|
|
58
|
+
h[fast_attr.name] = fast_attr.calc_value(self, hash)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
return attributes unless resource_type
|
63
|
+
|
64
|
+
{
|
65
|
+
'id' => hash[id_key],
|
66
|
+
'type' => resource_type,
|
67
|
+
'attributes' => attributes
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
def serialized_json
|
72
|
+
Oj.dump(serializable_hash)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fast-json-serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Santanu Karmakar
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-14 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.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '7.1'
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '5.0'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '7.1'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: pg
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.1.1
|
54
|
+
- - "<"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '2.0'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 1.1.1
|
64
|
+
- - "<"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2.0'
|
67
|
+
description: Generate JSON bypassing ActiveRecord object building. Directly from PG
|
68
|
+
result
|
69
|
+
email:
|
70
|
+
- skarmakar.personal@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- ".rspec"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.md
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- fast-json-serializer.gemspec
|
86
|
+
- lib/fast/json/serializer.rb
|
87
|
+
- lib/fast/json/serializer/attr.rb
|
88
|
+
- lib/fast/json/serializer/base.rb
|
89
|
+
- lib/fast/json/serializer/version.rb
|
90
|
+
homepage: https://github.com/skarmakar/fast-json-serializer
|
91
|
+
licenses: []
|
92
|
+
metadata:
|
93
|
+
homepage_uri: https://github.com/skarmakar/fast-json-serializer
|
94
|
+
source_code_uri: https://github.com/skarmakar/fast-json-serializer
|
95
|
+
changelog_uri: https://github.com/skarmakar/fast-json-serializer
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 2.3.0
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubygems_version: 3.1.6
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Ruby JSON Serializer. ActiveRecord dependent
|
115
|
+
test_files: []
|