oj_serializers 3.0.1 → 3.0.2
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +11 -0
- data/lib/oj_serializers/compat.rb +6 -6
- data/lib/oj_serializers/serializer.rb +7 -1
- data/lib/oj_serializers/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a88e76c54a8ec84dbffa0f384167e6f33113160568263268f0db02ac6c1a9dcd
|
|
4
|
+
data.tar.gz: ce378f273475d3b4f26aaec1fc507f7fd0af291b19eec1598659572d7ac03120
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 05d212b7dd306c565146279cf86af2dbda1c756fc695e07a6d1dd415d9b7ba947e324cb1484bd70bf9c0a918e48fb616b0118df673af609868ef847888d5675d
|
|
7
|
+
data.tar.gz: 6aee49934b25a387d185ae24688b1929d1296b6f4985a82ecd224e7023f114e3ccd0b4b17458c4898f816491c245cddbff7f4507b2941180fac13d3753d4d0a9
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -283,6 +283,17 @@ class SongSerializer < Oj::Serializer
|
|
|
283
283
|
end
|
|
284
284
|
```
|
|
285
285
|
|
|
286
|
+
Alternatively, use `from` to write the mapping the other way around: the key on
|
|
287
|
+
the left is the output name, and `from` names the source method or attribute.
|
|
288
|
+
|
|
289
|
+
```ruby
|
|
290
|
+
class SongSerializer < Oj::Serializer
|
|
291
|
+
# These two definitions are equivalent:
|
|
292
|
+
attributes name: {as: :label, type: :string}
|
|
293
|
+
attributes label: {from: :name, type: :string}
|
|
294
|
+
end
|
|
295
|
+
```
|
|
296
|
+
|
|
286
297
|
### Conditional attributes ❔
|
|
287
298
|
|
|
288
299
|
You can render attributes and associations conditionally by using `:if`.
|
|
@@ -7,33 +7,33 @@ require 'active_model_serializers'
|
|
|
7
7
|
class ActiveModel::Serializer
|
|
8
8
|
# JsonStringEncoder: Used internally to write a single object to JSON.
|
|
9
9
|
def self.one(object, options = nil)
|
|
10
|
-
new(object, options)
|
|
10
|
+
new(object, options || {})
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
# JsonStringEncoder: Used internally to write an array of objects to JSON.
|
|
14
14
|
def self.many(array, options = nil)
|
|
15
|
-
array.map { |object| new(object, options) }
|
|
15
|
+
array.map { |object| new(object, options || {}) }
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
# OjSerializer: Used internally to write a single object association in :hash mode.
|
|
19
19
|
#
|
|
20
20
|
# Returns nothing.
|
|
21
21
|
def self.one_as_hash(object, options = nil)
|
|
22
|
-
new(object)
|
|
22
|
+
new(object, options || {})
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
# OjSerializer: Used internally to write an association in :hash mode.
|
|
26
26
|
#
|
|
27
27
|
# Returns nothing.
|
|
28
28
|
def self.many_as_hash(array, options = nil)
|
|
29
|
-
array.map { |object| new(object) }
|
|
29
|
+
array.map { |object| new(object, options || {}) }
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
# OjSerializer: Used internally to write a single object association in :json mode.
|
|
33
33
|
#
|
|
34
34
|
# Returns nothing.
|
|
35
35
|
def self.write_one(writer, object, options = nil)
|
|
36
|
-
writer.push_value(new(object))
|
|
36
|
+
writer.push_value(new(object, options || {}))
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
# OjSerializer: Used internally to write an association in :json mode.
|
|
@@ -42,7 +42,7 @@ class ActiveModel::Serializer
|
|
|
42
42
|
def self.write_many(writer, array, options = nil)
|
|
43
43
|
writer.push_array
|
|
44
44
|
array.each do |object|
|
|
45
|
-
write_one(writer, object)
|
|
45
|
+
write_one(writer, object, options)
|
|
46
46
|
end
|
|
47
47
|
writer.pop
|
|
48
48
|
end
|
|
@@ -444,7 +444,13 @@ protected
|
|
|
444
444
|
|
|
445
445
|
private
|
|
446
446
|
|
|
447
|
-
def add_attribute(value_from, root: nil, as: nil, **options)
|
|
447
|
+
def add_attribute(value_from, root: nil, as: nil, from: nil, **options)
|
|
448
|
+
# `from` is the inverse of `as`: the definition key becomes the output name.
|
|
449
|
+
if from
|
|
450
|
+
as ||= value_from
|
|
451
|
+
value_from = from
|
|
452
|
+
end
|
|
453
|
+
|
|
448
454
|
# Because it's so common, automatically mark id as an identifier.
|
|
449
455
|
options[:identifier] = true if value_from == :id && !options.key?(:identifier)
|
|
450
456
|
|