Returns a JSON
string representing self
:
require 'json/add/ostruct' puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
Output:
{"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
Yields all attributes (as symbols) along with the corresponding values or returns an enumerator if no block is given.
require "ostruct" data = OpenStruct.new("country" => "Australia", :capital => "Canberra") data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
Methods Range#as_json
and Range.json_create
may be used to serialize and deserialize a Range object; see Marshal
.
Method Range#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/range' x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]} y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]} z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
Method JSON.create
deserializes such a hash, returning a Range object:
Range.json_create(x) # => 1..4 Range.json_create(y) # => 1...4 Range.json_create(z) # => "a".."d"
Returns a JSON
string representing self
:
require 'json/add/range' puts (1..4).to_json puts (1...4).to_json puts ('a'..'d').to_json
Output:
{"json_class":"Range","a":[1,4,false]} {"json_class":"Range","a":[1,4,true]} {"json_class":"Range","a":["a","d",false]}
Methods Regexp#as_json
and Regexp.json_create
may be used to serialize and deserialize a Regexp object; see Marshal
.
Method Regexp#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/regexp' x = /foo/.as_json # => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}
Method JSON.create
deserializes such a hash, returning a Regexp object:
Regexp.json_create(x) # => /foo/
Returns a JSON
string representing self
:
require 'json/add/regexp' puts /foo/.to_json
Output:
{"json_class":"Regexp","o":0,"s":"foo"}
See as_json
.
Methods Set#as_json
and Set.json_create
may be used to serialize and deserialize a Set object; see Marshal
.
Method Set#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/set' x = Set.new(%w/foo bar baz/).as_json # => {"json_class"=>"Set", "a"=>["foo", "bar", "baz"]}
Method JSON.create
deserializes such a hash, returning a Set object:
Set.json_create(x) # => #<Set: {"foo", "bar", "baz"}>
Returns a JSON
string representing self
:
require 'json/add/set' puts Set.new(%w/foo bar baz/).to_json
Output:
{"json_class":"Set","a":["foo","bar","baz"]}
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
:
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:
Struct::Customer.json_create(x) # => #<struct Struct::Customer name=nil, address=nil, zip=nil>
Returns a JSON
string representing self
:
require 'json/add/struct' Customer = Struct.new('Customer', :name, :address, :zip) puts Struct::Customer.new.to_json
Output:
{"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
Returns true
if the class was initialized with keyword_init: true
. Otherwise returns nil
or false
.
Examples:
Foo = Struct.new(:a) Foo.keyword_init? # => nil Bar = Struct.new(:a, keyword_init: true) Bar.keyword_init? # => true Baz = Struct.new(:a, keyword_init: false) Baz.keyword_init? # => false
Calls the given block with each member name/value pair; returns self
:
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each_pair {|(name, value)| p "#{name} => #{value}" }
Output:
"name => Joe Smith" "address => 123 Maple, Anytown NC" "zip => 12345"
Returns an Enumerator
if no block is given.
Related: each
.
Returns a hash of the name/value pairs for the given member names.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) h = joe.deconstruct_keys([:zip, :address]) h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
Returns all names and values if array_of_names
is nil
:
h = joe.deconstruct_keys(nil) h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
Methods Symbol#as_json
and Symbol.json_create
may be used to serialize and deserialize a Symbol object; see Marshal
.
Method Symbol#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/symbol' x = :foo.as_json # => {"json_class"=>"Symbol", "s"=>"foo"}
Method JSON.create
deserializes such a hash, returning a Symbol object:
Symbol.json_create(x) # => :foo
Returns a JSON
string representing self
:
require 'json/add/symbol' puts :foo.to_json
Output:
# {"json_class":"Symbol","s":"foo"}
Equivalent to self.to_s.end_with?
; see String#end_with?
.
Returns true if this class can be used to create an instance from a serialised JSON
string. The class has to implement a class method json_create that expects a hash as first parameter. The hash should include the required data.