Class: Symbol

Inherits:
Object
  • Object
show all
Defined in:
lib/json/add/symbol.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.json_create(o) ⇒ Object

See #as_json.



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

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

Instance Method Details

#as_jsonObject

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


23
24
25
26
27
28
# File 'lib/json/add/symbol.rb', line 23

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

#to_json(*a) ⇒ Object

Returns a JSON string representing self:

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

Output:

# {"json_class":"Symbol","s":"foo"}


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

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