Sha256: bcaed4af2f2689379bf5857b30f39c91867753d397b33834e45fde50f62ee7f6

Contents?: true

Size: 1.04 KB

Versions: 30

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
  require 'json'
end

class Symbol

  # Methods <tt>Symbol#as_json</tt> and +Symbol.json_create+ may be used
  # to serialize and deserialize a \Symbol object;
  # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
  #
  # \Method <tt>Symbol#as_json</tt> 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
  #
  def as_json(*)
    {
      JSON.create_id => self.class.name,
      's'            => to_s,
    }
  end

  # Returns a JSON string representing +self+:
  #
  #   require 'json/add/symbol'
  #   puts :foo.to_json
  #
  # Output:
  #
  #   # {"json_class":"Symbol","s":"foo"}
  #
  def to_json(*a)
    as_json.to_json(*a)
  end

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

Version data entries

30 entries across 30 versions & 5 rubygems

Version Path
es_cli-0.1.0 vendor/bundle/ruby/3.1.0/gems/json-2.9.1/lib/json/add/symbol.rb
talon_one-7.0.0 .github/.example/vendor/bundle/ruby/3.3.0/gems/json-2.8.2/lib/json/add/symbol.rb
json-2.9.1 lib/json/add/symbol.rb
json-2.9.1-java lib/json/add/symbol.rb
json-2.9.0 lib/json/add/symbol.rb
json-2.9.0-java lib/json/add/symbol.rb
cloudsmith-api-2.0.16 vendor/bundle/ruby/2.6.0/gems/json-2.7.6/lib/json/add/symbol.rb
json-2.8.2-java lib/json/add/symbol.rb
json-2.8.2 lib/json/add/symbol.rb
json-2.8.1 lib/json/add/symbol.rb
json-2.8.1-java lib/json/add/symbol.rb
json-2.8.0 lib/json/add/symbol.rb
json-2.8.0-java lib/json/add/symbol.rb
json-2.8.0.alpha1 lib/json/add/symbol.rb
json-2.7.6-java lib/json/add/symbol.rb
json_pure-2.7.6 lib/json/add/symbol.rb
json-2.7.6 lib/json/add/symbol.rb
json_pure-2.7.5 lib/json/add/symbol.rb
json-2.7.5 lib/json/add/symbol.rb
json-2.7.5-java lib/json/add/symbol.rb