oj_serializers 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +62 -12
- data/lib/oj_serializers/serializer.rb +3 -2
- data/lib/oj_serializers/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1a6edc7a526ccc3e6edd655070c28a9c54f2fd38646aaf36bf4db0b58363f36
|
4
|
+
data.tar.gz: 5a2541b9005c35d1f4c68a0dc142691b35199c78e0e134f6cc44ad0cd6a3a8bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ee1d2c5e850cd14548dbf1219056f61b25be85a55a83107bc6cf695b991cb52bb54605c45c88aed18e3573e53c72f22bbedbfa7a2bd066e10a17d71a1e25bd1
|
7
|
+
data.tar.gz: f8454069164b46838efecc9652e5a56fa816a07e47e7ad1983be49147c469f2c58bcdb67ab54ebb59bbcdd381abe0ee7ff7240ca282f310e581fc8dc0a275a0f
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
## Oj Serializers 2.0.2 (2023-04-02)
|
2
|
+
|
3
|
+
### Features ✨
|
4
|
+
|
5
|
+
- [Automatically remove `?` when using `transform_keys :camelize`](https://github.com/ElMassimo/oj_serializers/commit/79758a0)
|
6
|
+
|
7
|
+
### Fixes 🐞
|
8
|
+
|
9
|
+
- [Error when defining attributes with options](https://github.com/ElMassimo/oj_serializers/commit/680ab47)
|
10
|
+
|
1
11
|
## Oj Serializers 2.0.1 (2023-04-02)
|
2
12
|
|
3
13
|
### Features ✨
|
data/README.md
CHANGED
@@ -22,6 +22,8 @@ Faster JSON serializers for Ruby, built on top of the powerful [`oj`][oj] librar
|
|
22
22
|
[sugar]: https://github.com/ElMassimo/oj_serializers/blob/main/lib/oj_serializers/sugar.rb#L14
|
23
23
|
[migration guide]: https://github.com/ElMassimo/oj_serializers/blob/main/MIGRATION_GUIDE.md
|
24
24
|
[design]: https://github.com/ElMassimo/oj_serializers#design-
|
25
|
+
[associations]: https://github.com/ElMassimo/oj_serializers#associations-
|
26
|
+
[compose]: https://github.com/ElMassimo/oj_serializers#composing-serializers-
|
25
27
|
[raw_json]: https://github.com/ohler55/oj/issues/542
|
26
28
|
[trailing_commas]: https://maximomussini.com/posts/trailing-commas/
|
27
29
|
[render dsl]: https://github.com/ElMassimo/oj_serializers#render-dsl-
|
@@ -41,12 +43,12 @@ Learn more about [how this library achieves its performance][design].
|
|
41
43
|
|
42
44
|
## Features ⚡️
|
43
45
|
|
44
|
-
-
|
46
|
+
- Intuitive declaration syntax, supporting mixins and inheritance
|
45
47
|
- Reduced [memory allocation][benchmarks] and [improved performance][benchmarks]
|
46
|
-
-
|
48
|
+
- Generate [TypeScript interfaces automatically][types_from_serializers]
|
49
|
+
- Support for [`has_one`][associations] and [`has_many`][associations], compose with [`flat_one`][compose]
|
47
50
|
- Useful development checks to avoid typos and mistakes
|
48
|
-
-
|
49
|
-
- [Generate TypeScript interfaces automatically][types_from_serializers]
|
51
|
+
- [Migrate easily from Active Model Serializers][migration guide]
|
50
52
|
|
51
53
|
## Installation 💿
|
52
54
|
|
@@ -155,6 +157,24 @@ class AlbumsController < ApplicationController
|
|
155
157
|
end
|
156
158
|
```
|
157
159
|
|
160
|
+
<details>
|
161
|
+
<summary>Active Model Serializers style</summary>
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
require "oj_serializers/sugar" # In an initializer
|
165
|
+
|
166
|
+
class AlbumsController < ApplicationController
|
167
|
+
def show
|
168
|
+
render json: album, serializer: AlbumSerializer
|
169
|
+
end
|
170
|
+
|
171
|
+
def index
|
172
|
+
render json: albums, root: :albums, each_serializer: AlbumSerializer
|
173
|
+
end
|
174
|
+
end
|
175
|
+
```
|
176
|
+
</details>
|
177
|
+
|
158
178
|
## Rendering 🖨
|
159
179
|
|
160
180
|
Use `one` to serialize objects, and `many` to serialize enumerables:
|
@@ -170,7 +190,7 @@ Serializers can be rendered arrays, hashes, or even inside `ActiveModel::Seriali
|
|
170
190
|
by using a method in the serializer, making it very easy to combine with other
|
171
191
|
libraries and migrate incrementally.
|
172
192
|
|
173
|
-
|
193
|
+
`render` is a shortcut for `one` and `many`:
|
174
194
|
|
175
195
|
```ruby
|
176
196
|
render json: {
|
@@ -378,6 +398,41 @@ It's easy for the backend and the frontend to become out of sync. Traditionally,
|
|
378
398
|
|
379
399
|
As a result, it's posible to easily detect mismatches between the backend and the frontend, as well as make the fields more discoverable and provide great autocompletion in the frontend, without having to manually write the types.
|
380
400
|
|
401
|
+
### Composing serializers 🧱
|
402
|
+
|
403
|
+
There are three options to compose serializers: inheritance, mixins, and `flat_one`.
|
404
|
+
|
405
|
+
Use `flat_one` to include all attributes from a different serializer:
|
406
|
+
|
407
|
+
```ruby
|
408
|
+
class AttachmentSerializer < BaseSerializer
|
409
|
+
identifier
|
410
|
+
|
411
|
+
class BlobSerializer < BaseSerializer
|
412
|
+
attributes :filename, :byte_size, :content_type, :created_at
|
413
|
+
end
|
414
|
+
|
415
|
+
flat_one :blob, serializer: BlobSerializer
|
416
|
+
end
|
417
|
+
```
|
418
|
+
|
419
|
+
<details>
|
420
|
+
<summary>Example Output</summary>
|
421
|
+
|
422
|
+
```ruby
|
423
|
+
{
|
424
|
+
id: 5,
|
425
|
+
filename: "image.jpg,
|
426
|
+
byte_size: 256074,
|
427
|
+
content_type: "image/jpeg",
|
428
|
+
created_at: "2022-08-04T17:25:12.637-07:00",
|
429
|
+
}
|
430
|
+
```
|
431
|
+
</details>
|
432
|
+
|
433
|
+
This is especially convenient when using [`types_from_serializers`][types_from_serializers],
|
434
|
+
as it enables automatic type inference for the included attributes.
|
435
|
+
|
381
436
|
### Memoization & local state
|
382
437
|
|
383
438
|
Serializers are designed to be stateless so that an instanced can be reused, but
|
@@ -480,10 +535,8 @@ end
|
|
480
535
|
This will change the default shortcuts (`render`, `one`, `one_if`, and `many`),
|
481
536
|
so that the serializer writes directly to JSON instead of returning a Hash.
|
482
537
|
|
483
|
-
|
484
|
-
|
485
|
-
> This was the default behavior in `oj_serializers` v1, but was replaced with
|
486
|
-
`default_format :hash` in v2.
|
538
|
+
Even when using this mode, you can still use rendered values inside arrays,
|
539
|
+
hashes, and other serializers, thanks to [the `raw_json` extensions][raw_json].
|
487
540
|
|
488
541
|
<details>
|
489
542
|
<summary>Example Output</summary>
|
@@ -569,9 +622,6 @@ so that the serializer writes directly to JSON instead of returning a Hash.
|
|
569
622
|
```
|
570
623
|
</details>
|
571
624
|
|
572
|
-
Even when using this mode, you can still use rendered values inside arrays,
|
573
|
-
hashes, and other serializers, thanks to [the `raw_json` extensions][raw_json].
|
574
|
-
|
575
625
|
## Design 📐
|
576
626
|
|
577
627
|
Unlike `ActiveModel::Serializer`, which builds a Hash that then gets encoded to
|
@@ -126,7 +126,7 @@ protected
|
|
126
126
|
# This setting is inherited from parent classes.
|
127
127
|
def transform_keys(strategy = nil, &block)
|
128
128
|
transformer = case (strategy ||= block)
|
129
|
-
when :camelize, :camel_case then ->(key) { key.camelize(:lower) }
|
129
|
+
when :camelize, :camel_case then ->(key) { key.camelize(:lower).chomp('?') }
|
130
130
|
when :none then nil
|
131
131
|
when Symbol then strategy.to_proc
|
132
132
|
when Proc then strategy
|
@@ -376,7 +376,7 @@ protected
|
|
376
376
|
#
|
377
377
|
# See ./benchmarks/document_benchmark.rb
|
378
378
|
def mongo_attributes(*attr_names, **options)
|
379
|
-
identifier(:_id, as: :id, attribute: :mongoid, **options) if attr_names.delete(:id)
|
379
|
+
identifier(:_id, as: :id, attribute: :mongoid, **options.slice(:if)) if attr_names.delete(:id)
|
380
380
|
attributes(*attr_names, **options, attribute: :mongoid)
|
381
381
|
end
|
382
382
|
|
@@ -392,6 +392,7 @@ protected
|
|
392
392
|
|
393
393
|
methods_with_options.each do |attr_name, options|
|
394
394
|
options = { as: options } if options.is_a?(Symbol)
|
395
|
+
options[:attribute] ||= attr_options[:attribute]
|
395
396
|
add_attribute(attr_name, **options)
|
396
397
|
end
|
397
398
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oj_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maximo Mussini
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
|
-
rubygems_version: 3.
|
71
|
+
rubygems_version: 3.3.7
|
72
72
|
signing_key:
|
73
73
|
specification_version: 4
|
74
74
|
summary: A lighter JSON serializer for Ruby Objects in Rails. Easily migrate away
|