1unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED 2 require 'json' 3end 4 5# Range serialization/deserialization 6class Range 7 8 # Deserializes JSON string by constructing new Range object with arguments 9 # <tt>a</tt> serialized by <tt>to_json</tt>. 10 def self.json_create(object) 11 new(*object['a']) 12 end 13 14 # Returns a hash, that will be turned into a JSON object and represent this 15 # object. 16 def as_json(*) 17 { 18 JSON.create_id => self.class.name, 19 'a' => [ first, last, exclude_end? ] 20 } 21 end 22 23 # Stores class name (Range) with JSON array of arguments <tt>a</tt> which 24 # include <tt>first</tt> (integer), <tt>last</tt> (integer), and 25 # <tt>exclude_end?</tt> (boolean) as JSON string. 26 def to_json(*args) 27 as_json.to_json(*args) 28 end 29end 30