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