1unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2  require 'json'
3end
4require 'date'
5
6# Date serialization/deserialization
7class Date
8
9  # Deserializes JSON string by converting Julian year <tt>y</tt>, month
10  # <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
11  def self.json_create(object)
12    civil(*object.values_at('y', 'm', 'd', 'sg'))
13  end
14
15  alias start sg unless method_defined?(:start)
16
17  # Returns a hash, that will be turned into a JSON object and represent this
18  # object.
19  def as_json(*)
20    {
21      JSON.create_id => self.class.name,
22      'y' => year,
23      'm' => month,
24      'd' => day,
25      'sg' => start,
26    }
27  end
28
29  # Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
30  # <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
31  def to_json(*args)
32    as_json.to_json(*args)
33  end
34end
35