summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/add/symbol.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/lib/json/add/symbol.rb')
-rw-r--r--ext/json/lib/json/add/symbol.rb31
1 files changed, 27 insertions, 4 deletions
diff --git a/ext/json/lib/json/add/symbol.rb b/ext/json/lib/json/add/symbol.rb
index 74b13a423f..b5f3623158 100644
--- a/ext/json/lib/json/add/symbol.rb
+++ b/ext/json/lib/json/add/symbol.rb
@@ -1,11 +1,26 @@
+
#frozen_string_literal: false
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json'
end
class Symbol
- # Returns a hash, that will be turned into a JSON object and represent this
- # object.
+
+ # Methods <tt>Symbol#as_json</tt> and +Symbol.json_create+ may be used
+ # to serialize and deserialize a \Symbol object;
+ # see Marshal[rdoc-ref:Marshal].
+ #
+ # \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,
@@ -13,12 +28,20 @@ class Symbol
}
end
- # Stores class name (Symbol) with String representation of Symbol as a JSON string.
+ # 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
- # Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol
+ # See #as_json.
def self.json_create(o)
o['s'].to_sym
end