dulead-jsonapi-serializer 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +201 -0
- data/README.md +762 -0
- data/lib/extensions/has_one.rb +18 -0
- data/lib/fast_jsonapi/attribute.rb +5 -0
- data/lib/fast_jsonapi/helpers.rb +21 -0
- data/lib/fast_jsonapi/instrumentation/skylight.rb +3 -0
- data/lib/fast_jsonapi/instrumentation.rb +7 -0
- data/lib/fast_jsonapi/link.rb +5 -0
- data/lib/fast_jsonapi/object_serializer.rb +357 -0
- data/lib/fast_jsonapi/railtie.rb +11 -0
- data/lib/fast_jsonapi/relationship.rb +236 -0
- data/lib/fast_jsonapi/scalar.rb +29 -0
- data/lib/fast_jsonapi/serialization_core.rb +208 -0
- data/lib/fast_jsonapi/version.rb +3 -0
- data/lib/fast_jsonapi.rb +12 -0
- data/lib/generators/serializer/USAGE +8 -0
- data/lib/generators/serializer/serializer_generator.rb +19 -0
- data/lib/generators/serializer/templates/serializer.rb.tt +6 -0
- data/lib/jsonapi/serializer/errors.rb +21 -0
- data/lib/jsonapi/serializer/instrumentation.rb +27 -0
- data/lib/jsonapi/serializer/version.rb +5 -0
- data/lib/jsonapi/serializer.rb +12 -0
- metadata +249 -0
data/README.md
ADDED
@@ -0,0 +1,762 @@
|
|
1
|
+
# JSON:API Serialization Library
|
2
|
+
|
3
|
+
## :warning: :construction: [At the moment, contributions are welcome only for v3](https://github.com/jsonapi-serializer/jsonapi-serializer/pull/141)! :construction: :warning:
|
4
|
+
|
5
|
+
A fast [JSON:API](https://jsonapi.org/) serializer for Ruby Objects.
|
6
|
+
|
7
|
+
Previously this project was called **fast_jsonapi**, we forked the project
|
8
|
+
and renamed it to **jsonapi/serializer** in order to keep it alive.
|
9
|
+
|
10
|
+
We would like to thank the Netflix team for the initial work and to all our
|
11
|
+
contributors and users for the continuous support!
|
12
|
+
|
13
|
+
# Performance Comparison
|
14
|
+
|
15
|
+
We compare serialization times with `ActiveModelSerializer` and alternative
|
16
|
+
implementations as part of performance tests available at
|
17
|
+
[jsonapi-serializer/comparisons](https://github.com/jsonapi-serializer/comparisons).
|
18
|
+
|
19
|
+
We want to ensure that with every
|
20
|
+
change on this library, serialization time stays significantly faster than
|
21
|
+
the performance provided by the alternatives. Please read the performance
|
22
|
+
article in the `docs` folder for any questions related to methodology.
|
23
|
+
|
24
|
+
# Table of Contents
|
25
|
+
|
26
|
+
* [Features](#features)
|
27
|
+
* [Installation](#installation)
|
28
|
+
* [Usage](#usage)
|
29
|
+
* [Rails Generator](#rails-generator)
|
30
|
+
* [Model Definition](#model-definition)
|
31
|
+
* [Serializer Definition](#serializer-definition)
|
32
|
+
* [Object Serialization](#object-serialization)
|
33
|
+
* [Compound Document](#compound-document)
|
34
|
+
* [Key Transforms](#key-transforms)
|
35
|
+
* [Collection Serialization](#collection-serialization)
|
36
|
+
* [Caching](#caching)
|
37
|
+
* [Params](#params)
|
38
|
+
* [Conditional Attributes](#conditional-attributes)
|
39
|
+
* [Conditional Relationships](#conditional-relationships)
|
40
|
+
* [Specifying a Relationship Serializer](#specifying-a-relationship-serializer)
|
41
|
+
* [Sparse Fieldsets](#sparse-fieldsets)
|
42
|
+
* [Using helper methods](#using-helper-methods)
|
43
|
+
* [Performance Instrumentation](#performance-instrumentation)
|
44
|
+
* [Deserialization](#deserialization)
|
45
|
+
* [Migrating from Netflix/fast_jsonapi](#migrating-from-netflixfast_jsonapi)
|
46
|
+
* [Contributing](#contributing)
|
47
|
+
|
48
|
+
|
49
|
+
## Features
|
50
|
+
|
51
|
+
* Declaration syntax similar to Active Model Serializer
|
52
|
+
* Support for `belongs_to`, `has_many` and `has_one`
|
53
|
+
* Support for compound documents (included)
|
54
|
+
* Optimized serialization of compound documents
|
55
|
+
* Caching
|
56
|
+
|
57
|
+
## Installation
|
58
|
+
|
59
|
+
Add this line to your application's Gemfile:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
gem 'jsonapi-serializer'
|
63
|
+
```
|
64
|
+
|
65
|
+
Execute:
|
66
|
+
|
67
|
+
```bash
|
68
|
+
$ bundle install
|
69
|
+
```
|
70
|
+
|
71
|
+
## Usage
|
72
|
+
|
73
|
+
### Rails Generator
|
74
|
+
You can use the bundled generator if you are using the library inside of
|
75
|
+
a Rails project:
|
76
|
+
|
77
|
+
rails g serializer Movie name year
|
78
|
+
|
79
|
+
This will create a new serializer in `app/serializers/movie_serializer.rb`
|
80
|
+
|
81
|
+
### Model Definition
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class Movie
|
85
|
+
attr_accessor :id, :name, :year, :actor_ids, :owner_id, :movie_type_id
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
### Serializer Definition
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class MovieSerializer
|
93
|
+
include JSONAPI::Serializer
|
94
|
+
|
95
|
+
set_type :movie # optional
|
96
|
+
set_id :owner_id # optional
|
97
|
+
attributes :name, :year
|
98
|
+
has_many :actors
|
99
|
+
belongs_to :owner, record_type: :user
|
100
|
+
belongs_to :movie_type
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
### Sample Object
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
movie = Movie.new
|
108
|
+
movie.id = 232
|
109
|
+
movie.name = 'test movie'
|
110
|
+
movie.actor_ids = [1, 2, 3]
|
111
|
+
movie.owner_id = 3
|
112
|
+
movie.movie_type_id = 1
|
113
|
+
movie
|
114
|
+
|
115
|
+
movies =
|
116
|
+
2.times.map do |i|
|
117
|
+
m = Movie.new
|
118
|
+
m.id = i + 1
|
119
|
+
m.name = "test movie #{i}"
|
120
|
+
m.actor_ids = [1, 2, 3]
|
121
|
+
m.owner_id = 3
|
122
|
+
m.movie_type_id = 1
|
123
|
+
m
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
### Object Serialization
|
128
|
+
|
129
|
+
#### Return a hash
|
130
|
+
```ruby
|
131
|
+
hash = MovieSerializer.new(movie).serializable_hash
|
132
|
+
```
|
133
|
+
|
134
|
+
#### Return Serialized JSON
|
135
|
+
```ruby
|
136
|
+
json_string = MovieSerializer.new(movie).serializable_hash.to_json
|
137
|
+
```
|
138
|
+
|
139
|
+
#### Serialized Output
|
140
|
+
|
141
|
+
```json
|
142
|
+
{
|
143
|
+
"data": {
|
144
|
+
"id": "3",
|
145
|
+
"type": "movie",
|
146
|
+
"attributes": {
|
147
|
+
"name": "test movie",
|
148
|
+
"year": null
|
149
|
+
},
|
150
|
+
"relationships": {
|
151
|
+
"actors": {
|
152
|
+
"data": [
|
153
|
+
{
|
154
|
+
"id": "1",
|
155
|
+
"type": "actor"
|
156
|
+
},
|
157
|
+
{
|
158
|
+
"id": "2",
|
159
|
+
"type": "actor"
|
160
|
+
}
|
161
|
+
]
|
162
|
+
},
|
163
|
+
"owner": {
|
164
|
+
"data": {
|
165
|
+
"id": "3",
|
166
|
+
"type": "user"
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
```
|
174
|
+
|
175
|
+
#### The Optionality of `set_type`
|
176
|
+
By default fast_jsonapi will try to figure the type based on the name of the serializer class. For example `class MovieSerializer` will automatically have a type of `:movie`. If your serializer class name does not follow this format, you have to manually state the `set_type` at the serializer.
|
177
|
+
|
178
|
+
### Key Transforms
|
179
|
+
By default fast_jsonapi underscores the key names. It supports the same key transforms that are supported by AMS. Here is the syntax of specifying a key transform
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
class MovieSerializer
|
183
|
+
include JSONAPI::Serializer
|
184
|
+
|
185
|
+
# Available options :camel, :camel_lower, :dash, :underscore(default)
|
186
|
+
set_key_transform :camel
|
187
|
+
end
|
188
|
+
```
|
189
|
+
Here are examples of how these options transform the keys
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
set_key_transform :camel # "some_key" => "SomeKey"
|
193
|
+
set_key_transform :camel_lower # "some_key" => "someKey"
|
194
|
+
set_key_transform :dash # "some_key" => "some-key"
|
195
|
+
set_key_transform :underscore # "some_key" => "some_key"
|
196
|
+
```
|
197
|
+
|
198
|
+
### Attributes
|
199
|
+
Attributes are defined using the `attributes` method. This method is also aliased as `attribute`, which is useful when defining a single attribute.
|
200
|
+
|
201
|
+
By default, attributes are read directly from the model property of the same name. In this example, `name` is expected to be a property of the object being serialized:
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
class MovieSerializer
|
205
|
+
include JSONAPI::Serializer
|
206
|
+
|
207
|
+
attribute :name
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
Custom attributes that must be serialized but do not exist on the model can be declared using Ruby block syntax:
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
class MovieSerializer
|
215
|
+
include JSONAPI::Serializer
|
216
|
+
|
217
|
+
attributes :name, :year
|
218
|
+
|
219
|
+
attribute :name_with_year do |object|
|
220
|
+
"#{object.name} (#{object.year})"
|
221
|
+
end
|
222
|
+
end
|
223
|
+
```
|
224
|
+
|
225
|
+
The block syntax can also be used to override the property on the object:
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
class MovieSerializer
|
229
|
+
include JSONAPI::Serializer
|
230
|
+
|
231
|
+
attribute :name do |object|
|
232
|
+
"#{object.name} Part 2"
|
233
|
+
end
|
234
|
+
end
|
235
|
+
```
|
236
|
+
|
237
|
+
Attributes can also use a different name by passing the original method or accessor with a proc shortcut:
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
class MovieSerializer
|
241
|
+
include JSONAPI::Serializer
|
242
|
+
|
243
|
+
attributes :name
|
244
|
+
|
245
|
+
attribute :released_in_year, &:year
|
246
|
+
end
|
247
|
+
```
|
248
|
+
|
249
|
+
### Links Per Object
|
250
|
+
Links are defined using the `link` method. By default, links are read directly from the model property of the same name. In this example, `public_url` is expected to be a property of the object being serialized.
|
251
|
+
|
252
|
+
You can configure the method to use on the object for example a link with key `self` will get set to the value returned by a method called `url` on the movie object.
|
253
|
+
|
254
|
+
You can also use a block to define a url as shown in `custom_url`. You can access params in these blocks as well as shown in `personalized_url`
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
class MovieSerializer
|
258
|
+
include JSONAPI::Serializer
|
259
|
+
|
260
|
+
link :public_url
|
261
|
+
|
262
|
+
link :self, :url
|
263
|
+
|
264
|
+
link :custom_url do |object|
|
265
|
+
"https://movies.com/#{object.name}-(#{object.year})"
|
266
|
+
end
|
267
|
+
|
268
|
+
link :personalized_url do |object, params|
|
269
|
+
"https://movies.com/#{object.name}-#{params[:user].reference_code}"
|
270
|
+
end
|
271
|
+
end
|
272
|
+
```
|
273
|
+
|
274
|
+
#### Links on a Relationship
|
275
|
+
|
276
|
+
You can specify [relationship links](https://jsonapi.org/format/#document-resource-object-relationships) by using the `links:` option on the serializer. Relationship links in JSON API are useful if you want to load a parent document and then load associated documents later due to size constraints (see [related resource links](https://jsonapi.org/format/#document-resource-object-related-resource-links))
|
277
|
+
|
278
|
+
```ruby
|
279
|
+
class MovieSerializer
|
280
|
+
include JSONAPI::Serializer
|
281
|
+
|
282
|
+
has_many :actors, links: {
|
283
|
+
self: :url,
|
284
|
+
related: -> (object) {
|
285
|
+
"https://movies.com/#{object.id}/actors"
|
286
|
+
}
|
287
|
+
}
|
288
|
+
end
|
289
|
+
```
|
290
|
+
|
291
|
+
Relationship links can also be configured to be defined as a method on the object.
|
292
|
+
|
293
|
+
```ruby
|
294
|
+
has_many :actors, links: :actor_relationship_links
|
295
|
+
```
|
296
|
+
|
297
|
+
This will create a `self` reference for the relationship, and a `related` link for loading the actors relationship later. NB: This will not automatically disable loading the data in the relationship, you'll need to do that using the `lazy_load_data` option:
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
has_many :actors, lazy_load_data: true, links: {
|
301
|
+
self: :url,
|
302
|
+
related: -> (object) {
|
303
|
+
"https://movies.com/#{object.id}/actors"
|
304
|
+
}
|
305
|
+
}
|
306
|
+
```
|
307
|
+
|
308
|
+
### Meta Per Resource
|
309
|
+
|
310
|
+
For every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.
|
311
|
+
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
class MovieSerializer
|
315
|
+
include JSONAPI::Serializer
|
316
|
+
|
317
|
+
meta do |movie|
|
318
|
+
{
|
319
|
+
years_since_release: Date.current.year - movie.year
|
320
|
+
}
|
321
|
+
end
|
322
|
+
end
|
323
|
+
```
|
324
|
+
|
325
|
+
#### Meta on a Relationship
|
326
|
+
|
327
|
+
You can specify [relationship meta](https://jsonapi.org/format/#document-resource-object-relationships) by using the `meta:` option on the serializer. Relationship meta in JSON API is useful if you wish to provide non-standard meta-information about the relationship.
|
328
|
+
|
329
|
+
Meta can be defined either by passing a static hash or by using Proc to the `meta` key. In the latter case, the record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.
|
330
|
+
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
class MovieSerializer
|
334
|
+
include JSONAPI::Serializer
|
335
|
+
|
336
|
+
has_many :actors, meta: Proc.new do |movie_record, params|
|
337
|
+
{ count: movie_record.actors.length }
|
338
|
+
end
|
339
|
+
end
|
340
|
+
```
|
341
|
+
|
342
|
+
### Compound Document
|
343
|
+
|
344
|
+
Support for top-level and nested included associations through `options[:include]`.
|
345
|
+
|
346
|
+
```ruby
|
347
|
+
options = {}
|
348
|
+
options[:meta] = { total: 2 }
|
349
|
+
options[:links] = {
|
350
|
+
self: '...',
|
351
|
+
next: '...',
|
352
|
+
prev: '...'
|
353
|
+
}
|
354
|
+
options[:include] = [:actors, :'actors.agency', :'actors.agency.state']
|
355
|
+
MovieSerializer.new(movies, options).serializable_hash.to_json
|
356
|
+
```
|
357
|
+
|
358
|
+
### Collection Serialization
|
359
|
+
|
360
|
+
```ruby
|
361
|
+
options[:meta] = { total: 2 }
|
362
|
+
options[:links] = {
|
363
|
+
self: '...',
|
364
|
+
next: '...',
|
365
|
+
prev: '...'
|
366
|
+
}
|
367
|
+
hash = MovieSerializer.new(movies, options).serializable_hash
|
368
|
+
json_string = MovieSerializer.new(movies, options).serializable_hash.to_json
|
369
|
+
```
|
370
|
+
|
371
|
+
#### Control Over Collection Serialization
|
372
|
+
|
373
|
+
You can use `is_collection` option to have better control over collection serialization.
|
374
|
+
|
375
|
+
If this option is not provided or `nil` autodetect logic is used to try understand
|
376
|
+
if provided resource is a single object or collection.
|
377
|
+
|
378
|
+
Autodetect logic is compatible with most DB toolkits (ActiveRecord, Sequel, etc.) but
|
379
|
+
**cannot** guarantee that single vs collection will be always detected properly.
|
380
|
+
|
381
|
+
```ruby
|
382
|
+
options[:is_collection]
|
383
|
+
```
|
384
|
+
|
385
|
+
was introduced to be able to have precise control this behavior
|
386
|
+
|
387
|
+
- `nil` or not provided: will try to autodetect single vs collection (please, see notes above)
|
388
|
+
- `true` will always treat input resource as *collection*
|
389
|
+
- `false` will always treat input resource as *single object*
|
390
|
+
|
391
|
+
### Caching
|
392
|
+
|
393
|
+
To enable caching, use `cache_options store: <cache_store>`:
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
class MovieSerializer
|
397
|
+
include JSONAPI::Serializer
|
398
|
+
|
399
|
+
# use rails cache with a separate namespace and fixed expiry
|
400
|
+
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
|
401
|
+
end
|
402
|
+
```
|
403
|
+
|
404
|
+
`store` is required can be anything that implements a
|
405
|
+
`#fetch(record, **options, &block)` method:
|
406
|
+
|
407
|
+
- `record` is the record that is currently serialized
|
408
|
+
- `options` is everything that was passed to `cache_options` except `store`, so it can be everyhing the cache store supports
|
409
|
+
- `&block` should be executed to fetch new data if cache is empty
|
410
|
+
|
411
|
+
So for the example above it will call the cache instance like this:
|
412
|
+
|
413
|
+
```ruby
|
414
|
+
Rails.cache.fetch(record, namespace: 'jsonapi-serializer', expires_in: 1.hour) { ... }
|
415
|
+
```
|
416
|
+
|
417
|
+
#### Caching and Sparse Fieldsets
|
418
|
+
|
419
|
+
If caching is enabled and fields are provided to the serializer, the fieldset will be appended to the cache key's namespace.
|
420
|
+
|
421
|
+
For example, given the following serializer definition and instance:
|
422
|
+
```ruby
|
423
|
+
class ActorSerializer
|
424
|
+
include JSONAPI::Serializer
|
425
|
+
|
426
|
+
attributes :first_name, :last_name
|
427
|
+
|
428
|
+
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
|
429
|
+
end
|
430
|
+
|
431
|
+
serializer = ActorSerializer.new(actor, { fields: { actor: [:first_name] } })
|
432
|
+
```
|
433
|
+
|
434
|
+
The following cache namespace will be generated: `'jsonapi-serializer-fieldset:first_name'`.
|
435
|
+
|
436
|
+
### Params
|
437
|
+
|
438
|
+
In some cases, attribute values might require more information than what is
|
439
|
+
available on the record, for example, access privileges or other information
|
440
|
+
related to a current authenticated user. The `options[:params]` value covers these
|
441
|
+
cases by allowing you to pass in a hash of additional parameters necessary for
|
442
|
+
your use case.
|
443
|
+
|
444
|
+
Leveraging the new params is easy, when you define a custom id, attribute or
|
445
|
+
relationship with a block you opt-in to using params by adding it as a block
|
446
|
+
parameter.
|
447
|
+
|
448
|
+
```ruby
|
449
|
+
class MovieSerializer
|
450
|
+
include JSONAPI::Serializer
|
451
|
+
|
452
|
+
set_id do |movie, params|
|
453
|
+
# in here, params is a hash containing the `:admin` key
|
454
|
+
params[:admin] ? movie.owner_id : "movie-#{movie.id}"
|
455
|
+
end
|
456
|
+
|
457
|
+
attributes :name, :year
|
458
|
+
attribute :can_view_early do |movie, params|
|
459
|
+
# in here, params is a hash containing the `:current_user` key
|
460
|
+
params[:current_user].is_employee? ? true : false
|
461
|
+
end
|
462
|
+
|
463
|
+
belongs_to :primary_agent do |movie, params|
|
464
|
+
# in here, params is a hash containing the `:current_user` key
|
465
|
+
params[:current_user]
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
# ...
|
470
|
+
current_user = User.find(cookies[:current_user_id])
|
471
|
+
serializer = MovieSerializer.new(movie, {params: {current_user: current_user}})
|
472
|
+
serializer.serializable_hash
|
473
|
+
```
|
474
|
+
|
475
|
+
Custom attributes and relationships that only receive the resource are still possible by defining
|
476
|
+
the block to only receive one argument.
|
477
|
+
|
478
|
+
### Conditional Attributes
|
479
|
+
|
480
|
+
Conditional attributes can be defined by passing a Proc to the `if` key on the `attribute` method. Return `true` if the attribute should be serialized, and `false` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.
|
481
|
+
|
482
|
+
```ruby
|
483
|
+
class MovieSerializer
|
484
|
+
include JSONAPI::Serializer
|
485
|
+
|
486
|
+
attributes :name, :year
|
487
|
+
attribute :release_year, if: Proc.new { |record|
|
488
|
+
# Release year will only be serialized if it's greater than 1990
|
489
|
+
record.release_year > 1990
|
490
|
+
}
|
491
|
+
|
492
|
+
attribute :director, if: Proc.new { |record, params|
|
493
|
+
# The director will be serialized only if the :admin key of params is true
|
494
|
+
params && params[:admin] == true
|
495
|
+
}
|
496
|
+
|
497
|
+
# Custom attribute `name_year` will only be serialized if both `name` and `year` fields are present
|
498
|
+
attribute :name_year, if: Proc.new { |record|
|
499
|
+
record.name.present? && record.year.present?
|
500
|
+
} do |object|
|
501
|
+
"#{object.name} - #{object.year}"
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
# ...
|
506
|
+
current_user = User.find(cookies[:current_user_id])
|
507
|
+
serializer = MovieSerializer.new(movie, { params: { admin: current_user.admin? }})
|
508
|
+
serializer.serializable_hash
|
509
|
+
```
|
510
|
+
|
511
|
+
### Conditional Relationships
|
512
|
+
|
513
|
+
Conditional relationships can be defined by passing a Proc to the `if` key. Return `true` if the relationship should be serialized, and `false` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.
|
514
|
+
|
515
|
+
```ruby
|
516
|
+
class MovieSerializer
|
517
|
+
include JSONAPI::Serializer
|
518
|
+
|
519
|
+
# Actors will only be serialized if the record has any associated actors
|
520
|
+
has_many :actors, if: Proc.new { |record| record.actors.any? }
|
521
|
+
|
522
|
+
# Owner will only be serialized if the :admin key of params is true
|
523
|
+
belongs_to :owner, if: Proc.new { |record, params| params && params[:admin] == true }
|
524
|
+
end
|
525
|
+
|
526
|
+
# ...
|
527
|
+
current_user = User.find(cookies[:current_user_id])
|
528
|
+
serializer = MovieSerializer.new(movie, { params: { admin: current_user.admin? }})
|
529
|
+
serializer.serializable_hash
|
530
|
+
```
|
531
|
+
|
532
|
+
### Specifying a Relationship Serializer
|
533
|
+
|
534
|
+
In many cases, the relationship can automatically detect the serializer to use.
|
535
|
+
|
536
|
+
```ruby
|
537
|
+
class MovieSerializer
|
538
|
+
include JSONAPI::Serializer
|
539
|
+
|
540
|
+
# resolves to StudioSerializer
|
541
|
+
belongs_to :studio
|
542
|
+
# resolves to ActorSerializer
|
543
|
+
has_many :actors
|
544
|
+
end
|
545
|
+
```
|
546
|
+
|
547
|
+
At other times, such as when a property name differs from the class name, you may need to explicitly state the serializer to use. You can do so by specifying a different symbol or the serializer class itself (which is the recommended usage):
|
548
|
+
|
549
|
+
```ruby
|
550
|
+
class MovieSerializer
|
551
|
+
include JSONAPI::Serializer
|
552
|
+
|
553
|
+
# resolves to MovieStudioSerializer
|
554
|
+
belongs_to :studio, serializer: :movie_studio
|
555
|
+
# resolves to PerformerSerializer
|
556
|
+
has_many :actors, serializer: PerformerSerializer
|
557
|
+
end
|
558
|
+
```
|
559
|
+
|
560
|
+
For more advanced cases, such as polymorphic relationships and Single Table Inheritance, you may need even greater control to select the serializer based on the specific object or some specified serialization parameters. You can do by defining the serializer as a `Proc`:
|
561
|
+
|
562
|
+
```ruby
|
563
|
+
class MovieSerializer
|
564
|
+
include JSONAPI::Serializer
|
565
|
+
|
566
|
+
has_many :actors, serializer: Proc.new do |record, params|
|
567
|
+
if record.comedian?
|
568
|
+
ComedianSerializer
|
569
|
+
elsif params[:use_drama_serializer]
|
570
|
+
DramaSerializer
|
571
|
+
else
|
572
|
+
ActorSerializer
|
573
|
+
end
|
574
|
+
end
|
575
|
+
end
|
576
|
+
```
|
577
|
+
|
578
|
+
### Sparse Fieldsets
|
579
|
+
|
580
|
+
Attributes and relationships can be selectively returned per record type by using the `fields` option.
|
581
|
+
|
582
|
+
```ruby
|
583
|
+
class MovieSerializer
|
584
|
+
include JSONAPI::Serializer
|
585
|
+
|
586
|
+
attributes :name, :year
|
587
|
+
end
|
588
|
+
|
589
|
+
serializer = MovieSerializer.new(movie, { fields: { movie: [:name] } })
|
590
|
+
serializer.serializable_hash
|
591
|
+
```
|
592
|
+
|
593
|
+
### Using helper methods
|
594
|
+
|
595
|
+
You can mix-in code from another ruby module into your serializer class to reuse functions across your app.
|
596
|
+
|
597
|
+
Since a serializer is evaluated in a the context of a `class` rather than an `instance` of a class, you need to make sure that your methods act as `class` methods when mixed in.
|
598
|
+
|
599
|
+
|
600
|
+
##### Using ActiveSupport::Concern
|
601
|
+
|
602
|
+
``` ruby
|
603
|
+
|
604
|
+
module AvatarHelper
|
605
|
+
extend ActiveSupport::Concern
|
606
|
+
|
607
|
+
class_methods do
|
608
|
+
def avatar_url(user)
|
609
|
+
user.image.url
|
610
|
+
end
|
611
|
+
end
|
612
|
+
end
|
613
|
+
|
614
|
+
class UserSerializer
|
615
|
+
include JSONAPI::Serializer
|
616
|
+
|
617
|
+
include AvatarHelper # mixes in your helper method as class method
|
618
|
+
|
619
|
+
set_type :user
|
620
|
+
|
621
|
+
attributes :name, :email
|
622
|
+
|
623
|
+
attribute :avatar do |user|
|
624
|
+
avatar_url(user)
|
625
|
+
end
|
626
|
+
end
|
627
|
+
|
628
|
+
```
|
629
|
+
|
630
|
+
##### Using Plain Old Ruby
|
631
|
+
|
632
|
+
``` ruby
|
633
|
+
module AvatarHelper
|
634
|
+
def avatar_url(user)
|
635
|
+
user.image.url
|
636
|
+
end
|
637
|
+
end
|
638
|
+
|
639
|
+
class UserSerializer
|
640
|
+
include JSONAPI::Serializer
|
641
|
+
|
642
|
+
extend AvatarHelper # mixes in your helper method as class method
|
643
|
+
|
644
|
+
set_type :user
|
645
|
+
|
646
|
+
attributes :name, :email
|
647
|
+
|
648
|
+
attribute :avatar do |user|
|
649
|
+
avatar_url(user)
|
650
|
+
end
|
651
|
+
end
|
652
|
+
|
653
|
+
```
|
654
|
+
|
655
|
+
### Customizable Options
|
656
|
+
|
657
|
+
Option | Purpose | Example
|
658
|
+
------------ | ------------- | -------------
|
659
|
+
set_type | Type name of Object | `set_type :movie`
|
660
|
+
key | Key of Object | `belongs_to :owner, key: :user`
|
661
|
+
set_id | ID of Object | `set_id :owner_id` or `set_id { \|record, params\| params[:admin] ? record.id : "#{record.name.downcase}-#{record.id}" }`
|
662
|
+
cache_options | Hash with store to enable caching and optional further cache options | `cache_options store: ActiveSupport::Cache::MemoryStore.new, expires_in: 5.minutes`
|
663
|
+
id_method_name | Set custom method name to get ID of an object (If block is provided for the relationship, `id_method_name` is invoked on the return value of the block instead of the resource object) | `has_many :locations, id_method_name: :place_ids`
|
664
|
+
object_method_name | Set custom method name to get related objects | `has_many :locations, object_method_name: :places`
|
665
|
+
record_type | Set custom Object Type for a relationship | `belongs_to :owner, record_type: :user`
|
666
|
+
serializer | Set custom Serializer for a relationship | `has_many :actors, serializer: :custom_actor`, `has_many :actors, serializer: MyApp::Api::V1::ActorSerializer`, or `has_many :actors, serializer -> (object, params) { (return a serializer class) }`
|
667
|
+
polymorphic | Allows different record types for a polymorphic association | `has_many :targets, polymorphic: true`
|
668
|
+
polymorphic | Sets custom record types for each object class in a polymorphic association | `has_many :targets, polymorphic: { Person => :person, Group => :group }`
|
669
|
+
|
670
|
+
### Performance Instrumentation
|
671
|
+
|
672
|
+
Performance instrumentation is available by using the
|
673
|
+
`active_support/notifications`.
|
674
|
+
|
675
|
+
To enable it, include the module in your serializer class:
|
676
|
+
|
677
|
+
```ruby
|
678
|
+
require 'jsonapi/serializer'
|
679
|
+
require 'jsonapi/serializer/instrumentation'
|
680
|
+
|
681
|
+
class MovieSerializer
|
682
|
+
include JSONAPI::Serializer
|
683
|
+
include JSONAPI::Serializer::Instrumentation
|
684
|
+
|
685
|
+
# ...
|
686
|
+
end
|
687
|
+
```
|
688
|
+
|
689
|
+
[Skylight](https://www.skylight.io/) integration is also available and
|
690
|
+
supported by us, follow the Skylight documentation to enable it.
|
691
|
+
|
692
|
+
### Running Tests
|
693
|
+
The project has and requires unit tests, functional tests and performance
|
694
|
+
tests. To run tests use the following command:
|
695
|
+
|
696
|
+
```bash
|
697
|
+
rspec
|
698
|
+
```
|
699
|
+
|
700
|
+
## Deserialization
|
701
|
+
We currently do not support deserialization, but we recommend to use any of the next gems:
|
702
|
+
|
703
|
+
### [JSONAPI.rb](https://github.com/stas/jsonapi.rb)
|
704
|
+
|
705
|
+
This gem provides the next features alongside deserialization:
|
706
|
+
- Collection meta
|
707
|
+
- Error handling
|
708
|
+
- Includes and sparse fields
|
709
|
+
- Filtering and sorting
|
710
|
+
- Pagination
|
711
|
+
|
712
|
+
## Migrating from Netflix/fast_jsonapi
|
713
|
+
|
714
|
+
If you come from [Netflix/fast_jsonapi](https://github.com/Netflix/fast_jsonapi), here is the instructions to switch.
|
715
|
+
|
716
|
+
### Modify your Gemfile
|
717
|
+
|
718
|
+
```diff
|
719
|
+
- gem 'fast_jsonapi'
|
720
|
+
+ gem 'jsonapi-serializer'
|
721
|
+
```
|
722
|
+
|
723
|
+
### Replace all constant references
|
724
|
+
|
725
|
+
```diff
|
726
|
+
class MovieSerializer
|
727
|
+
- include FastJsonapi::ObjectSerializer
|
728
|
+
+ include JSONAPI::Serializer
|
729
|
+
end
|
730
|
+
```
|
731
|
+
|
732
|
+
### Replace removed methods
|
733
|
+
|
734
|
+
```diff
|
735
|
+
- json_string = MovieSerializer.new(movie).serialized_json
|
736
|
+
+ json_string = MovieSerializer.new(movie).serializable_hash.to_json
|
737
|
+
```
|
738
|
+
|
739
|
+
### Replace require references
|
740
|
+
|
741
|
+
```diff
|
742
|
+
- require 'fast_jsonapi'
|
743
|
+
+ require 'jsonapi/serializer'
|
744
|
+
```
|
745
|
+
|
746
|
+
### Update your cache options
|
747
|
+
|
748
|
+
See [docs](https://github.com/jsonapi-serializer/jsonapi-serializer#caching).
|
749
|
+
|
750
|
+
```diff
|
751
|
+
- cache_options enabled: true, cache_length: 12.hours
|
752
|
+
+ cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
|
753
|
+
```
|
754
|
+
|
755
|
+
## Contributing
|
756
|
+
|
757
|
+
Please follow the instructions we provide as part of the issue and
|
758
|
+
pull request creation processes.
|
759
|
+
|
760
|
+
This project is intended to be a safe, welcoming space for collaboration, and
|
761
|
+
contributors are expected to adhere to the
|
762
|
+
[Contributor Covenant](https://contributor-covenant.org) code of conduct.
|