/
to_json.cr
209 lines (189 loc) · 4.27 KB
/
to_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
class Object
def to_json
String.build do |str|
to_json str
end
end
def to_json(io : IO)
JSON.build(io) do |json|
to_json(json)
end
end
def to_pretty_json(indent : String = " ")
String.build do |str|
to_pretty_json str, indent: indent
end
end
def to_pretty_json(io : IO, indent : String = " ")
JSON.build(io, indent: indent) do |json|
to_json(json)
end
end
end
struct Nil
def to_json(json : JSON::Builder)
json.null
end
end
struct Bool
def to_json(json : JSON::Builder)
json.bool(self)
end
end
struct Int
def to_json(json : JSON::Builder)
json.number(self)
end
end
struct Float
def to_json(json : JSON::Builder)
json.number(self)
end
end
class String
def to_json(json : JSON::Builder)
json.string(self)
end
end
struct Symbol
def to_json(json : JSON::Builder)
json.string(to_s)
end
end
class Array
def to_json(json : JSON::Builder)
json.array do
each &.to_json(json)
end
end
end
struct Set
def to_json(json : JSON::Builder)
json.array do
each &.to_json(json)
end
end
end
class Hash
def to_json(json : JSON::Builder)
json.object do
each do |key, value|
json.field key do
value.to_json(json)
end
end
end
end
end
struct Tuple
def to_json(json : JSON::Builder)
json.array do
{% for i in 0...T.size %}
self[{{i}}].to_json(json)
{% end %}
end
end
end
struct NamedTuple
def to_json(json : JSON::Builder)
json.object do
{% for key in T.keys %}
json.field {{key.stringify}} do
self[{{key.symbolize}}].to_json(json)
end
{% end %}
end
end
end
struct Time::Format
def to_json(value : Time, json : JSON::Builder)
format(value).to_json(json)
end
end
struct Enum
def to_json(json : JSON::Builder)
json.number(value)
end
end
struct Time
# Emits a string formated according to [RFC 3339](https://tools.ietf.org/html/rfc3339)
# ([ISO 8601](http://xml.coverpages.org/ISO-FDIS-8601.pdf) profile).
#
# The JSON format itself does not specify a time data type, this method just
# assumes that a string holding a RFC 3339 time format will be interpreted as
# a time value.
#
# See `#from_json` for reference.
def to_json(json : JSON::Builder)
json.string(Time::Format::RFC_3339.format(self, fraction_digits: 0))
end
end
# Converter to be used with `JSON.mapping` and `YAML.mapping`
# to serialize a `Time` instance as the number of seconds
# since the unix epoch. See `Time#to_unix`.
#
# ```
# require "json"
#
# class Person
# JSON.mapping({
# birth_date: {type: Time, converter: Time::EpochConverter},
# })
# end
#
# person = Person.from_json(%({"birth_date": 1459859781}))
# person.birth_date # => 2016-04-05 12:36:21 UTC
# person.to_json # => %({"birth_date":1459859781})
# ```
module Time::EpochConverter
def self.to_json(value : Time, json : JSON::Builder)
json.number(value.to_unix)
end
end
# Converter to be used with `JSON.mapping` and `YAML.mapping`
# to serialize a `Time` instance as the number of milliseconds
# since the unix epoch. See `Time#to_unix_ms`.
#
# ```
# require "json"
#
# class Timestamp
# JSON.mapping({
# value: {type: Time, converter: Time::EpochMillisConverter},
# })
# end
#
# timestamp = Timestamp.from_json(%({"value": 1459860483856}))
# timestamp.value # => 2016-04-05 12:48:03.856 UTC
# timestamp.to_json # => %({"value":1459860483856})
# ```
module Time::EpochMillisConverter
def self.to_json(value : Time, json : JSON::Builder)
json.number(value.to_unix_ms)
end
end
# Converter to be used with `JSON.mapping` to read the raw
# value of a JSON object property as a `String`.
#
# It can be useful to read ints and floats without losing precision,
# or to read an object and deserialize it later based on some
# condition.
#
# ```
# require "json"
#
# class Raw
# JSON.mapping({
# value: {type: String, converter: String::RawConverter},
# })
# end
#
# raw = Raw.from_json(%({"value": 123456789876543212345678987654321}))
# raw.value # => "123456789876543212345678987654321"
# raw.to_json # => %({"value":123456789876543212345678987654321})
# ```
module String::RawConverter
def self.to_json(value : String, json : JSON::Builder)
json.raw(value)
end
end