Class: OjSerializers::JsonValue

Inherits:
Object
  • Object
show all
Defined in:
lib/oj_serializers/json_value.rb

Overview

Public: Allows to prevent double encoding an existing JSON string.

NOTE: Oj’s raw_json option means there’s no performance overhead, as it would occur with the previous alternative of parsing the JSON string.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ JsonValue

Public: Expects json to be a JSON-encoded string.



9
10
11
# File 'lib/oj_serializers/json_value.rb', line 9

def initialize(json)
  @json = json
end

Class Method Details

.array(json_rows) ⇒ Object

Public: Expects an Array of JSON-encoded strings and wraps them in a JSON array.

Returns a JsonValue representing a JSON-encoded array.



16
17
18
# File 'lib/oj_serializers/json_value.rb', line 16

def self.array(json_rows)
  new("[#{json_rows.join(',')}]")
end

Instance Method Details

#as_json(_options = nil) ⇒ Object

Internal: Used by Oj::Rails::Encoder when found inside a Hash or Array.



31
32
33
# File 'lib/oj_serializers/json_value.rb', line 31

def as_json(_options = nil)
  self
end

#raw_jsonObject

Internal: Used by Oj::Rails::Encoder because we use the ‘raw_json` option.



26
27
28
# File 'lib/oj_serializers/json_value.rb', line 26

def raw_json(*)
  @json
end

#to_sObject

Public: Return the internal json when using string interpolation.



21
22
23
# File 'lib/oj_serializers/json_value.rb', line 21

def to_s
  @json
end