oj_serializers 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +364 -195
- data/lib/oj_serializers/compat.rb +4 -19
- data/lib/oj_serializers/controller_serialization.rb +2 -2
- data/lib/oj_serializers/json_string_encoder.rb +12 -28
- data/lib/oj_serializers/serializer.rb +389 -156
- data/lib/oj_serializers/version.rb +1 -1
- metadata +6 -33
@@ -6,28 +6,13 @@ 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
|
-
|
11
|
-
def self.write_one(writer, object, options)
|
12
|
-
writer.push_value(new(object, options))
|
9
|
+
def self.one(object, options = nil)
|
10
|
+
new(object, options)
|
13
11
|
end
|
14
12
|
|
15
13
|
# JsonStringEncoder: Used internally to write an array of objects to JSON.
|
16
|
-
|
17
|
-
|
18
|
-
def self.write_many(writer, array, options)
|
19
|
-
writer.push_array
|
20
|
-
array.each do |object|
|
21
|
-
write_one(writer, object, options)
|
22
|
-
end
|
23
|
-
writer.pop
|
24
|
-
end
|
25
|
-
|
26
|
-
# JsonStringEncoder: Used internally to instantiate an Oj::StringWriter.
|
27
|
-
#
|
28
|
-
# Returns an Oj::StringWriter.
|
29
|
-
def self.new_json_writer
|
30
|
-
OjSerializers::Serializer.send(:new_json_writer)
|
14
|
+
def self.many(array, options = nil)
|
15
|
+
array.map { |object| new(object, options) }
|
31
16
|
end
|
32
17
|
end
|
33
18
|
|
@@ -20,10 +20,10 @@ module OjSerializers::ControllerSerialization
|
|
20
20
|
#
|
21
21
|
# which is more performant.
|
22
22
|
%i[_render_option_json _render_with_renderer_json].each do |renderer_method|
|
23
|
-
define_method renderer_method do |resource,
|
23
|
+
define_method renderer_method do |resource, options = {}|
|
24
24
|
serializer_class = options[:serializer] || options[:each_serializer]
|
25
25
|
if serializer_class && serializer_class < OjSerializers::Serializer
|
26
|
-
super(OjSerializers::JsonStringEncoder.encode_to_json(resource, options), options.except(:root, :serializer, :each_serializer))
|
26
|
+
super(OjSerializers::JsonStringEncoder.encode_to_json(resource, **options), options.except(:root, :serializer, :each_serializer))
|
27
27
|
else
|
28
28
|
super(resource, **options)
|
29
29
|
end
|
@@ -14,43 +14,27 @@ module OjSerializers::JsonStringEncoder
|
|
14
14
|
# regardless of whether a serializer is specified or not.
|
15
15
|
#
|
16
16
|
# Returns a JSON string.
|
17
|
-
def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
if root
|
22
|
-
writer.push_object
|
23
|
-
writer.push_key(root.to_s)
|
24
|
-
end
|
25
|
-
|
26
|
-
if serializer
|
27
|
-
serializer.write_one(writer, object, extras)
|
17
|
+
def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **options)
|
18
|
+
result = if serializer
|
19
|
+
serializer.one(object, options)
|
28
20
|
elsif each_serializer
|
29
|
-
each_serializer.
|
21
|
+
each_serializer.many(object, options)
|
30
22
|
elsif object.is_a?(String)
|
31
|
-
|
32
|
-
|
33
|
-
writer.push_json(object)
|
23
|
+
OjSerializers::JsonValue.new(object)
|
34
24
|
else
|
35
|
-
|
25
|
+
object
|
36
26
|
end
|
37
|
-
|
38
|
-
writer.pop if root
|
39
|
-
|
40
|
-
writer.to_json
|
27
|
+
Oj.dump(root ? { root => result } : result)
|
41
28
|
end
|
42
29
|
|
43
30
|
if OjSerializers::Serializer::DEV_MODE
|
44
31
|
alias actual_encode_to_json encode_to_json
|
45
32
|
# Internal: Allows to detect misusage of the options during development.
|
46
|
-
def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **
|
47
|
-
if serializer && serializer < OjSerializers::Serializer
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
raise ArgumentError, 'You must use `serializer` when serializing a single object' unless object.respond_to?(:each)
|
52
|
-
end
|
53
|
-
actual_encode_to_json(object, root: root, serializer: serializer, each_serializer: each_serializer, **extras)
|
33
|
+
def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **options)
|
34
|
+
raise ArgumentError, 'You must use `each_serializer` when serializing collections' if serializer && serializer < OjSerializers::Serializer && object.respond_to?(:map)
|
35
|
+
raise ArgumentError, 'You must use `serializer` when serializing a single object' if each_serializer && each_serializer < OjSerializers::Serializer && !object.respond_to?(:map)
|
36
|
+
|
37
|
+
actual_encode_to_json(object, root: root, serializer: serializer, each_serializer: each_serializer, **options)
|
54
38
|
end
|
55
39
|
end
|
56
40
|
end
|