oj_serializers 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +5 -4
- data/lib/oj_serializers/compat.rb +6 -0
- data/lib/oj_serializers/json_string_encoder.rb +3 -3
- data/lib/oj_serializers/json_value.rb +8 -5
- data/lib/oj_serializers/memo.rb +4 -0
- data/lib/oj_serializers/serializer.rb +1 -4
- data/lib/oj_serializers/setup.rb +4 -3
- data/lib/oj_serializers/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b12d0af1a11e10d1fd2276700de7e44e67532947ee8edfc56001e74b29ad55f0
|
4
|
+
data.tar.gz: d3e74a9a7e27943417680f469ed995312ec9439908a18652977152a0ba43abd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ee8a29984fd104c0844c0c30228966effaf2f2244996c0ca874d228eff876d8d356890d24f166c73089be472511b882c1f692a627953a1705250476d2b4fae1
|
7
|
+
data.tar.gz: a5d34b5799786d9716d6c6b5939cf89e41ecdd6679fd72cd39ea0dba5b502beafb5f57975c01c364053cb98ee3787472d992ad3e9a7c2bbe7cf45f3c0c1d4c08
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## Oj Serializers 1.0.1 (2023-03-01) ##
|
2
|
+
|
3
|
+
* [Avoid caching instances of reloaded classes in development](https://github.com/ElMassimo/oj_serializers/commit/0bd928d64d159926acf6b4d57e3f08b12f6931ce)
|
4
|
+
|
5
|
+
|
1
6
|
## Oj Serializers 1.0.0 (2020-11-05) ##
|
2
7
|
|
3
8
|
* Initial Release.
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
<h1 align="center">
|
2
2
|
Oj Serializers
|
3
3
|
<p align="center">
|
4
|
-
|
5
|
-
<a href="
|
4
|
+
<a href="https://github.com/ElMassimo/oj_serializers/actions"><img alt="Build Status" src="https://github.com/ElMassimo/oj_serializers/workflows/build/badge.svg"/></a>
|
5
|
+
<a href="https://inch-ci.org/github/ElMassimo/oj_serializers"><img alt="Inline docs" src="https://inch-ci.org/github/ElMassimo/oj_serializers.svg"/></a>
|
6
6
|
<a href="https://codeclimate.com/github/ElMassimo/oj_serializers"><img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/oj_serializers/badges/gpa.svg"/></a>
|
7
|
-
<a href="https://codeclimate.com/github/ElMassimo/oj_serializers"><img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/oj_serializers/badges/coverage.svg"/></a>
|
7
|
+
<a href="https://codeclimate.com/github/ElMassimo/oj_serializers"><img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/oj_serializers/badges/coverage.svg"/></a>
|
8
8
|
<a href="https://rubygems.org/gems/oj_serializers"><img alt="Gem Version" src="https://img.shields.io/gem/v/oj_serializers.svg?colorB=e9573f"/></a>
|
9
9
|
<a href="https://github.com/ElMassimo/oj_serializers/blob/master/LICENSE.txt"><img alt="License" src="https://img.shields.io/badge/license-MIT-428F7E.svg"/></a>
|
10
10
|
</p>
|
@@ -19,6 +19,7 @@ JSON serializers for Ruby, built on top of the powerful [`oj`][oj] library.
|
|
19
19
|
[panko]: https://github.com/panko-serializer/panko_serializer
|
20
20
|
[benchmarks]: https://github.com/ElMassimo/oj_serializers/tree/master/benchmarks
|
21
21
|
[raw_benchmarks]: https://github.com/ElMassimo/oj_serializers/blob/master/benchmarks/document_benchmark.rb
|
22
|
+
[sugar]: https://github.com/ElMassimo/oj_serializers/blob/master/lib/oj_serializers/sugar.rb#L14
|
22
23
|
[migration guide]: https://github.com/ElMassimo/oj_serializers/blob/master/MIGRATION_GUIDE.md
|
23
24
|
[design]: https://github.com/ElMassimo/oj_serializers#design-
|
24
25
|
[raw_json]: https://github.com/ohler55/oj/issues/542
|
@@ -174,7 +175,7 @@ class AlbumsController < ApplicationController
|
|
174
175
|
end
|
175
176
|
```
|
176
177
|
|
177
|
-
If you are using Rails you can also use something closer to Active Model Serializers by adding `sugar
|
178
|
+
If you are using Rails you can also use something closer to Active Model Serializers by adding [`sugar`][sugar]:
|
178
179
|
|
179
180
|
```ruby
|
180
181
|
require 'oj_serializers/sugar'
|
@@ -6,11 +6,15 @@ require 'active_model_serializers'
|
|
6
6
|
# as well.
|
7
7
|
class ActiveModel::Serializer
|
8
8
|
# JsonStringEncoder: Used internally to write a single object to JSON.
|
9
|
+
#
|
10
|
+
# Returns nothing.
|
9
11
|
def self.write_one(writer, object, options)
|
10
12
|
writer.push_value(new(object, options))
|
11
13
|
end
|
12
14
|
|
13
15
|
# JsonStringEncoder: Used internally to write an array of objects to JSON.
|
16
|
+
#
|
17
|
+
# Returns nothing.
|
14
18
|
def self.write_many(writer, array, options)
|
15
19
|
writer.push_array
|
16
20
|
array.each do |object|
|
@@ -20,6 +24,8 @@ class ActiveModel::Serializer
|
|
20
24
|
end
|
21
25
|
|
22
26
|
# JsonStringEncoder: Used internally to instantiate an Oj::StringWriter.
|
27
|
+
#
|
28
|
+
# Returns an Oj::StringWriter.
|
23
29
|
def self.new_json_writer
|
24
30
|
OjSerializers::Serializer.send(:new_json_writer)
|
25
31
|
end
|
@@ -10,10 +10,10 @@ module OjSerializers::JsonStringEncoder
|
|
10
10
|
# render json: items, each_serializer: ItemSerializer
|
11
11
|
# render json: item, serializer: ItemSerializer
|
12
12
|
#
|
13
|
-
# Returns a JSON string.
|
14
|
-
#
|
15
13
|
# NOTE: Unlike the default encoder, this one will use the `root` option
|
16
14
|
# regardless of whether a serializer is specified or not.
|
15
|
+
#
|
16
|
+
# Returns a JSON string.
|
17
17
|
def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **extras)
|
18
18
|
# NOTE: Serializers may override `new_json_writer` to modify the behavior.
|
19
19
|
writer = (serializer || each_serializer || OjSerializers::Serializer).send(:new_json_writer)
|
@@ -40,9 +40,9 @@ module OjSerializers::JsonStringEncoder
|
|
40
40
|
writer.to_json
|
41
41
|
end
|
42
42
|
|
43
|
-
# Allows to detect misusage of the options during development.
|
44
43
|
if OjSerializers::Serializer::DEV_MODE
|
45
44
|
alias actual_encode_to_json encode_to_json
|
45
|
+
# Internal: Allows to detect misusage of the options during development.
|
46
46
|
def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **extras)
|
47
47
|
if serializer && serializer < OjSerializers::Serializer
|
48
48
|
raise ArgumentError, 'You must use `each_serializer` when serializing collections' if object.respond_to?(:each)
|
@@ -5,15 +5,18 @@
|
|
5
5
|
# NOTE: Oj's raw_json option means there's no performance overhead, as it would
|
6
6
|
# occur with the previous alternative of parsing the JSON string.
|
7
7
|
class OjSerializers::JsonValue
|
8
|
-
#
|
9
|
-
def self.array(json_rows)
|
10
|
-
new("[#{json_rows.join(',')}]")
|
11
|
-
end
|
12
|
-
|
8
|
+
# Public: Expects json to be a JSON-encoded string.
|
13
9
|
def initialize(json)
|
14
10
|
@json = json
|
15
11
|
end
|
16
12
|
|
13
|
+
# Public: Expects an Array of JSON-encoded strings and wraps them in a JSON array.
|
14
|
+
#
|
15
|
+
# Returns a JsonValue representing a JSON-encoded array.
|
16
|
+
def self.array(json_rows)
|
17
|
+
new("[#{json_rows.join(',')}]")
|
18
|
+
end
|
19
|
+
|
17
20
|
# Public: Return the internal json when using string interpolation.
|
18
21
|
def to_s
|
19
22
|
@json
|
data/lib/oj_serializers/memo.rb
CHANGED
@@ -6,10 +6,14 @@ class OjSerializers::Memo
|
|
6
6
|
@cache = {}
|
7
7
|
end
|
8
8
|
|
9
|
+
# Internal: Allows to clear the cache when binding the serializer to a
|
10
|
+
# different object.
|
9
11
|
def clear
|
10
12
|
@cache.clear
|
11
13
|
end
|
12
14
|
|
15
|
+
# Public: Allows to use a simple memoization pattern that also works for
|
16
|
+
# falsey values.
|
13
17
|
def fetch(key)
|
14
18
|
@cache.fetch(key) { @cache[key] = yield }
|
15
19
|
end
|
@@ -222,9 +222,6 @@ private
|
|
222
222
|
@_associations
|
223
223
|
end
|
224
224
|
|
225
|
-
# Internal: Iterating arrays is faster than iterating hashes.
|
226
|
-
attr_reader :_attributes_entries, :_associations_entries
|
227
|
-
|
228
225
|
protected
|
229
226
|
|
230
227
|
# Internal: Calculates the cache_key used to cache one serialized item.
|
@@ -451,7 +448,7 @@ private
|
|
451
448
|
# Internal: Cache key to set a thread-local instance.
|
452
449
|
def instance_key
|
453
450
|
unless defined?(@instance_key)
|
454
|
-
@instance_key = "#{name.underscore}
|
451
|
+
@instance_key = "#{name.underscore}_instance_#{object_id}".to_sym
|
455
452
|
# We take advantage of the fact that this method will always be called
|
456
453
|
# before instantiating a serializer to define the write_to_json method.
|
457
454
|
class_eval(write_to_json_body)
|
data/lib/oj_serializers/setup.rb
CHANGED
@@ -13,14 +13,15 @@ end
|
|
13
13
|
# NOTE: Add an optimization to make it easier to work with a StringWriter
|
14
14
|
# transparently in different scenarios.
|
15
15
|
class Oj::StringWriter
|
16
|
-
|
16
|
+
alias original_as_json as_json
|
17
|
+
|
18
|
+
# Internal: ActiveSupport can pass an options argument to `as_json` when
|
17
19
|
# serializing a Hash or Array.
|
18
|
-
alias_method :original_as_json, :as_json
|
19
20
|
def as_json(_options = nil)
|
20
21
|
original_as_json
|
21
22
|
end
|
22
23
|
|
23
|
-
#
|
24
|
+
# Internal: We can use `to_s` directly, this is not important but gives a
|
24
25
|
# slight boost to a few use cases that use it for caching in Memcached.
|
25
26
|
def to_json(_options = nil)
|
26
27
|
to_s.delete_suffix("\n")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oj_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maximo Mussini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
|
-
rubygems_version: 3.
|
98
|
+
rubygems_version: 3.2.32
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: A lighter JSON serializer for Ruby Objects in Rails. Easily migrate away
|