Skip to content
instance method

Struct#as_json

(*untyped) -> ::Hash[::String, ::String | ::Array[Elem]]
as_json(*)

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>
See also #[]= #deconstruct #[] #deconstruct_keys also in 3.4 · 3.3 · 3.2 · 3.1 · 3.0