class Range
def self.json_create(object)
def self.json_create(object) new(*object['a']) end
def as_json(*)
Range.json_create(z) # => "a".."d"
Range.json_create(y) # => 1...4
Range.json_create(x) # => 1..4
\Method +JSON.create+ deserializes such a hash, returning a \Range object:
z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
require 'json/add/range'
returning a 2-element hash representing +self+:
\Method Range#as_json serializes +self+,
see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
to serialize and deserialize a \Range object;
Methods Range#as_json and +Range.json_create+ may be used
def as_json(*) { JSON.create_id => self.class.name, 'a' => [ first, last, exclude_end? ] } end
def to_json(*args)
{"json_class":"Range","a":["a","d",false]}
{"json_class":"Range","a":[1,4,true]}
{"json_class":"Range","a":[1,4,false]}
Output:
puts ('a'..'d').to_json
puts (1...4).to_json
puts (1..4).to_json
require 'json/add/range'
Returns a JSON string representing +self+:
def to_json(*args) as_json.to_json(*args) end