/
from_json.cr
256 lines (229 loc) · 5.52 KB
/
from_json.cr
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Deserializes the given JSON in *string_or_io* into
# an instance of `self`. This simply creates a `parser = JSON::PullParser`
# and invokes `new(parser)`: classes that want to provide JSON
# deserialization must provide an `def initialize(parser : JSON::PullParser`
# method.
#
# ```
# Int32.from_json("1") # => 1
# Array(Int32).from_json("[1, 2, 3]") # => [1, 2, 3]
# ```
def Object.from_json(string_or_io) : self
parser = JSON::PullParser.new(string_or_io)
new parser
end
# Deserializes the given JSON in *string_or_io* into
# an instance of `self`, assuming the JSON consists
# of an JSON object with key *root*, and whose value is
# the value to deserialize.
#
# ```
# Int32.from_json(%({"main": 1}), root: "main") # => 1
# ```
def Object.from_json(string_or_io, root : String) : self
parser = JSON::PullParser.new(string_or_io)
parser.on_key!(root) do
new parser
end
end
# Parses a String or IO denoting a JSON array, yielding
# each of its elements to the given block. This is useful
# for decoding an array and processing its elements without
# creating an Array in memory, which might be expensive.
#
# ```
# require "json"
#
# Array(Int32).from_json("[1, 2, 3]") do |element|
# puts element
# end
# ```
#
# Output:
#
# ```text
# 1
# 2
# 3
# ```
#
# To parse and get an Array, use the block-less overload.
def Array.from_json(string_or_io) : Nil
parser = JSON::PullParser.new(string_or_io)
new(parser) do |element|
yield element
end
nil
end
def Nil.new(pull : JSON::PullParser)
pull.read_null
end
def Bool.new(pull : JSON::PullParser)
pull.read_bool
end
{% for type in %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64) %}
def {{type.id}}.new(pull : JSON::PullParser)
{{type.id}}.new(pull.read_int)
end
{% end %}
def Float32.new(pull : JSON::PullParser)
case pull.kind
when :int
value = pull.int_value.to_f32
pull.read_next
value
else
pull.read_float.to_f32
end
end
def Float64.new(pull : JSON::PullParser)
case pull.kind
when :int
value = pull.int_value.to_f
pull.read_next
value
else
pull.read_float.to_f
end
end
def String.new(pull : JSON::PullParser)
pull.read_string
end
def Array.new(pull : JSON::PullParser)
ary = new
new(pull) do |element|
ary << element
end
ary
end
def Array.new(pull : JSON::PullParser)
pull.read_array do
yield T.new(pull)
end
end
def Set.new(pull : JSON::PullParser)
set = new
pull.read_array do
set << T.new(pull)
end
set
end
def Hash.new(pull : JSON::PullParser)
hash = new
pull.read_object do |key|
if pull.kind == :null
pull.read_next
else
hash[key] = V.new(pull)
end
end
hash
end
def Tuple.new(pull : JSON::PullParser)
{% if true %}
pull.read_begin_array
value = Tuple.new(
{% for i in 0...T.size %}
(self[{{i}}].new(pull)),
{% end %}
)
pull.read_end_array
value
{% end %}
end
def NamedTuple.new(pull : JSON::PullParser)
{% begin %}
{% for key in T.keys %}
%var{key.id} = nil
{% end %}
pull.read_object do |key|
case key
{% for key, type in T %}
when {{key.stringify}}
%var{key.id} = {{type}}.new(pull)
{% end %}
else
pull.skip
end
end
{% for key in T.keys %}
if %var{key.id}.nil?
raise JSON::ParseException.new("missing json attribute: {{key}}", 0, 0)
end
{% end %}
{
{% for key in T.keys %}
{{key}}: %var{key.id},
{% end %}
}
{% end %}
end
def Enum.new(pull : JSON::PullParser)
case pull.kind
when :int
from_value(pull.read_int)
when :string
parse(pull.read_string)
else
raise "expecting int or string in JSON for #{self.class}, not #{pull.kind}"
end
end
def Union.new(pull : JSON::PullParser)
# Optimization: use fast path for primitive types
{% begin %}
# Here we store types that are not primitive types
{% non_primitives = [] of Nil %}
{% for type, index in T %}
{% if type == Nil %}
return pull.read_null if pull.kind == :null
{% elsif type == Bool ||
type == Int8 || type == Int16 || type == Int32 || type == Int64 ||
type == UInt8 || type == UInt16 || type == UInt32 || type == UInt64 ||
type == Float32 || type == Float64 ||
type == String %}
value = pull.read?({{type}})
return value unless value.nil?
{% else %}
{% non_primitives << type %}
{% end %}
{% end %}
# If after traversing all the types we are left with just one
# non-primitive type, we can parse it directly (no need to use `read_raw`)
{% if non_primitives.size == 1 %}
return {{non_primitives[0]}}.new(pull)
{% end %}
{% end %}
string = pull.read_raw
{% for type in T %}
begin
return {{type}}.from_json(string)
rescue JSON::ParseException
# Ignore
end
{% end %}
raise JSON::ParseException.new("couldn't parse #{self} from #{string}", 0, 0)
end
def Time.new(pull : JSON::PullParser)
Time::Format::ISO_8601_DATE_TIME.parse(pull.read_string)
end
struct Time::Format
def from_json(pull : JSON::PullParser)
string = pull.read_string
parse(string)
end
end
module Time::EpochConverter
def self.from_json(value : JSON::PullParser) : Time
Time.epoch(value.read_int)
end
end
module Time::EpochMillisConverter
def self.from_json(value : JSON::PullParser) : Time
Time.epoch_ms(value.read_int)
end
end
module String::RawConverter
def self.from_json(value : JSON::PullParser)
value.read_raw
end
end