oj_serializers 3.0.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a055b7489622b5fcc884399410af78d536d8e03750cecdebe5535e2fd46d3d6
4
- data.tar.gz: 61a1c9852a57dd3d367f22015018a9397759bb3c3a313f1f8d32ce7093e7eb34
3
+ metadata.gz: a88e76c54a8ec84dbffa0f384167e6f33113160568263268f0db02ac6c1a9dcd
4
+ data.tar.gz: ce378f273475d3b4f26aaec1fc507f7fd0af291b19eec1598659572d7ac03120
5
5
  SHA512:
6
- metadata.gz: '02887512f4deac20d0a3a6d8d14718539db2baa44fffe3ee273a2b2fdf39312c717f1b62ca75006b1fa32dc16ef38a20e59e05725ba33ddb75c0b0cab498b2f8'
7
- data.tar.gz: c4451e1a5e1f7f4b9d1b1d17696cc2559114946e220103b6e1c0546ab3198483f9e22e85ba3ed78845265ead0fed97a4fb78e2ed663d2ca6b0485efecabf9667
6
+ metadata.gz: 05d212b7dd306c565146279cf86af2dbda1c756fc695e07a6d1dd415d9b7ba947e324cb1484bd70bf9c0a918e48fb616b0118df673af609868ef847888d5675d
7
+ data.tar.gz: 6aee49934b25a387d185ae24688b1929d1296b6f4985a82ecd224e7023f114e3ccd0b4b17458c4898f816491c245cddbff7f4507b2941180fac13d3753d4d0a9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## Oj Serializers 3.0.2 (2026-08-28)
2
+
3
+ ### Features ✨
4
+
5
+ - [feat: add `from` as the inverse of `as` for attribute definitions](https://github.com/ElMassimo/oj_serializers/pull/43)
6
+
7
+ ## Oj Serializers 3.0.1 (2026-07-17)
8
+
9
+ ### Fixes 🐞
10
+
11
+ - Enable `use_raw_json` before `Oj.optimize_rails` so `JsonValue` is not double-encoded on Rails 8.1+.
12
+
1
13
  ## Oj Serializers 3.0.0 (2026-01-02)
2
14
 
3
15
  ### Features ✨
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
 
@@ -4,10 +4,16 @@ require 'oj'
4
4
 
5
5
  # NOTE: We automatically set the necessary configuration unless it had been
6
6
  # explicitly set beforehand.
7
+ #
8
+ # `use_raw_json` must be enabled *before* `Oj.optimize_rails`. Rails 8.1
9
+ # memoizes an option-less encoder when the JSON encoder is assigned (which
10
+ # `Oj.optimize_rails` does), and `Oj::Rails::Encoder` captures `default_options`
11
+ # at construction. Enabling `use_raw_json` afterwards leaves that memoized
12
+ # encoder with `use_raw_json: false`, which double-encodes `JsonValue`.
7
13
  unless Oj.default_options[:use_raw_json]
8
14
  require 'rails'
9
- Oj.optimize_rails
10
15
  Oj.default_options = { mode: :rails, use_raw_json: true }
16
+ Oj.optimize_rails
11
17
  end
12
18
 
13
19
  # NOTE: Add an optimization to make it easier to work with a StringWriter
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OjSerializers
4
- VERSION = '3.0.0'
4
+ VERSION = '3.0.2'
5
5
  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: 3.0.0
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maximo Mussini