summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/add/core.rb
blob: 630ed6e4b70819704713eca33e474b99488f590e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# This file contains implementations of ruby core's custom objects for
# serialisation/deserialisation.

unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
  ::JSON::JSON_LOADED
  require 'json'
end
require 'date'

class Time
  def self.json_create(object)
    at(*object.values_at('s', 'u'))
  end

  def to_json(*args)
    {
      'json_class' => self.class.name.to_s,
      's' => tv_sec,
      'u' => tv_usec,
    }.to_json(*args)
  end
end

class Date
  def self.json_create(object)
    civil(*object.values_at('y', 'm', 'd', 'sg'))
  end

  def to_json(*args)
    {
      'json_class' => self.class.name.to_s,
      'y' => year,
      'm' => month,
      'd' => day,
      'sg' => @sg,
    }.to_json(*args)
  end
end

class DateTime
  def self.json_create(object)
    args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
    of_a, of_b = object['of'].split('/')
    args << Rational(of_a.to_i, of_b.to_i)
    args << object['sg']
    civil(*args)
  end

  def to_json(*args)
    {
      'json_class' => self.class.name.to_s,
      'y' => year,
      'm' => month,
      'd' => day,
      'H' => hour,
      'M' => min,
      'S' => sec,
      'of' => offset.to_s,
      'sg' => @sg,
    }.to_json(*args)
  end
end

class Range
  def self.json_create(object)
    new(*object['a'])
  end

  def to_json(*args)
    {
      'json_class'   => self.class.name.to_s,
      'a'         => [ first, last, exclude_end? ]
    }.to_json(*args)
  end
end

class Struct
  def self.json_create(object)
    new(*object['v'])
  end

  def to_json(*args)
    klass = self.class.name.to_s
    klass.empty? and raise JSON::JSONError, "Only named structs are supported!"
    {
      'json_class' => klass,
      'v'     => values,
    }.to_json(*args)
  end
end

class Exception
  def self.json_create(object)
    result = new(object['m'])
    result.set_backtrace object['b']
    result
  end

  def to_json(*args)
    {
      'json_class' => self.class.name.to_s,
      'm'   => message,
      'b' => backtrace,
    }.to_json(*args)
  end
end

class Regexp
  def self.json_create(object)
    new(object['s'], object['o'])
  end

  def to_json(*)
    {
      'json_class' => self.class.name.to_s,
      'o' => options,
      's' => source,
    }.to_json
  end
end