class Struct
def self.json_create(object)
def self.json_create(object) new(*object['v']) end
def as_json(*)
# => #
Struct::Customer.json_create(x)
\Method +JSON.create+ deserializes such a hash, returning a \Struct object:
# => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]}
x = Struct::Customer.new.as_json
Customer = Struct.new('Customer', :name, :address, :zip)
require 'json/add/struct'
returning a 2-element hash representing +self+:
\Method Struct#as_json serializes +self+,
see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
to serialize and deserialize a \Struct object;
Methods Struct#as_json and +Struct.json_create+ may be used
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
def to_json(*args)
{"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
Output:
puts Struct::Customer.new.to_json
Customer = Struct.new('Customer', :name, :address, :zip)
require 'json/add/struct'
Returns a JSON string representing +self+:
def to_json(*args) as_json.to_json(*args) end