1unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2  require 'json'
3end
4defined?(::Complex) or require 'complex'
5
6class Complex
7  def self.json_create(object)
8    Complex(object['r'], object['i'])
9  end
10
11  def as_json(*)
12    {
13      JSON.create_id => self.class.name,
14      'r'            => real,
15      'i'            => imag,
16    }
17  end
18
19  def to_json(*)
20    as_json.to_json
21  end
22end
23