Module: OjSerializers::JsonStringEncoder

Defined in:
lib/oj_serializers/json_string_encoder.rb

Overview

Public: Contains utility functions to render objects to JSON.

Useful to instantiate a single ‘JsonWriter` when rendering new serializers.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **options) ⇒ Object

Public: Allows to use Oj::Serializer in ‘serializer` and `each_serializer` as with ActiveModelSerializers.

render json: items, each_serializer: ItemSerializer
render json: item, serializer: ItemSerializer

NOTE: Unlike the default encoder, this one will use the ‘root` option regardless of whether a serializer is specified or not.

Returns a JSON string.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oj_serializers/json_string_encoder.rb', line 17

def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **options)
  result = if serializer
    serializer.one(object, options)
  elsif each_serializer
    each_serializer.many(object, options)
  elsif object.is_a?(String)
    OjSerializers::JsonValue.new(object)
  else
    object
  end
  Oj.dump(root ? { root => result } : result)
end

Instance Method Details

#actual_encode_to_jsonObject



31
# File 'lib/oj_serializers/json_string_encoder.rb', line 31

alias actual_encode_to_json encode_to_json

#encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **options) ⇒ Object

Internal: Allows to detect misusage of the options during development.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
# File 'lib/oj_serializers/json_string_encoder.rb', line 33

def encode_to_json(object, root: nil, serializer: nil, each_serializer: nil, **options)
  raise ArgumentError, 'You must use `each_serializer` when serializing collections' if serializer && serializer < OjSerializers::Serializer && object.respond_to?(:map)
  raise ArgumentError, 'You must use `serializer` when serializing a single object' if each_serializer && each_serializer < OjSerializers::Serializer && !object.respond_to?(:map)

  actual_encode_to_json(object, root: root, serializer: serializer, each_serializer: each_serializer, **options)
end