class Regexp
def self.json_create(object)
def self.json_create(object) new(object['s'], object['o']) end
def as_json(*)
Regexp.json_create(x) # => /foo/
\Method +JSON.create+ deserializes such a hash, returning a \Regexp object:
# => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}
x = /foo/.as_json
require 'json/add/regexp'
returning a 2-element hash representing +self+:
\Method Regexp#as_json serializes +self+,
see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
to serialize and deserialize a \Regexp object;
Methods Regexp#as_json and +Regexp.json_create+ may be used
def as_json(*) { JSON.create_id => self.class.name, 'o' => options, 's' => source, } end
def to_json(*args)
{"json_class":"Regexp","o":0,"s":"foo"}
Output:
puts /foo/.to_json
require 'json/add/regexp'
Returns a JSON string representing +self+:
def to_json(*args) as_json.to_json(*args) end