instance method
Struct#as_json
(*untyped) -> ::Hash[::String, ::String | ::Array[Elem]]
as_json(*)
ext/json/lib/json/add/struct.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ext/json/lib/json/add/struct.rb, line 30
def as_json(*)
klass = self.class.name
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
{
JSON.create_id => klass,
'v' => values,
}
end
Methods Struct#as_json and Struct.json_create may be used to serialize and deserialize a Struct object; see Marshal.
Method Struct#as_json serializes self, returning a 2-element hash representing self:
Example
require 'json/add/struct' Customer = Struct.new('Customer', :name, :address, :zip) x = Struct::Customer.new.as_json # => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]}
Method JSON.create deserializes such a hash, returning a Struct object:
Example
Struct::Customer.json_create(x) # => #<struct Struct::Customer name=nil, address=nil, zip=nil>