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.



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

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


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

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"}


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

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