123456789_123456789_123456789_123456789_123456789_

Class: Symbol

Relationships & Source Files
Inherits: Object
Defined in: ext/json/lib/json/add/symbol.rb

Class Method Summary

Instance Method Summary

Class Method Details

.json_create(o)

See #as_json.

[ GitHub ]

  
# File 'ext/json/lib/json/add/symbol.rb', line 44

def self.json_create(o)
  o['s'].to_sym
end

Instance Method Details

#as_json

Methods as_json and .json_create may be used to serialize and deserialize a Symbol object; see Marshal.

Method 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
[ GitHub ]

  
# File 'ext/json/lib/json/add/symbol.rb', line 23

def as_json(*)
  {
    JSON.create_id => self.class.name,
    's'            => to_s,
  }
end

#to_json(*a)

Returns a ::JSON string representing self:

require 'json/add/symbol'
puts :foo.to_json

Output:

# {"json_class":"Symbol","s":"foo"}
[ GitHub ]

  
# File 'ext/json/lib/json/add/symbol.rb', line 39

def to_json(*a)
  as_json.to_json(*a)
end