Class: JSON::Fragment
Overview
Fragment of JSON document that is to be included as is:
fragment = JSON::Fragment.new("[1, 2, 3]")
JSON.generate({ count: 3, items: fragments })
This allows to easily assemble multiple JSON fragments that have been persisted somewhere without having to parse them nor resorting to string interpolation.
Note: no validation is performed on the provided string. It is the responsability of the caller to ensure the string contains valid JSON.
Instance Attribute Summary collapse
-
#json ⇒ Object
Returns the value of attribute json.
Instance Method Summary collapse
-
#initialize(json) ⇒ Fragment
constructor
A new instance of Fragment.
- #to_json(state = nil) ⇒ Object
Methods inherited from Struct
Constructor Details
#initialize(json) ⇒ Fragment
Returns a new instance of Fragment.
184 185 186 187 188 189 190 |
# File 'lib/json/common.rb', line 184 def initialize(json) unless string = String.try_convert(json) raise TypeError, " no implicit conversion of #{json.class} into String" end super(string) end |
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json
183 184 185 |
# File 'lib/json/common.rb', line 183 def json @json end |
Instance Method Details
#to_json(state = nil) ⇒ Object
192 193 194 |
# File 'lib/json/common.rb', line 192 def to_json(state = nil, *) json end |