class Symbol - json: Ruby Standard Library Documentation

# class Symbol

### Public Class Methods

json_create(o) click to toggle source

See `as_json`.

    # File json/lib/json/add/symbol.rb, line 44
    def self.json_create(o)
      o['s'].to_sym
    end

### Public Instance Methods

as_json(*) click to toggle source

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

    # File json/lib/json/add/symbol.rb, line 23
    def as_json(*)
      {
        JSON.create_id => self.class.name,
        's'            => to_s,
      }
    end

to_json(*a) click to toggle source

Returns a `JSON` string representing `self`:

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

Output:

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

    # File json/lib/json/add/symbol.rb, line 39
    def to_json(*a)
      as_json.to_json(*a)
    end