/
json.cr
38 lines (36 loc) · 888 Bytes
/
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
require "json"
require "uuid"
struct UUID
# Creates UUID from JSON using `JSON::PullParser`.
#
# NOTE: `require "uuid/json"` is required to opt-in to this feature.
#
# ```
# require "json"
# require "uuid"
# require "uuid/json"
#
# class Example
# include JSON::Serializable
#
# property id : UUID
# end
#
# example = Example.from_json(%({"id": "ba714f86-cac6-42c7-8956-bcf5105e1b81"}))
# example.id # => UUID(ba714f86-cac6-42c7-8956-bcf5105e1b81)
# ```
def self.new(pull : JSON::PullParser)
new(pull.read_string)
end
# Returns UUID as JSON value.
#
# NOTE: `require "uuid/json"` is required to opt-in to this feature.
#
# ```
# uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
# uuid.to_json # => "\"87b3042b-9b9a-41b7-8b15-a93d3f17025e\""
# ```
def to_json(json : JSON::Builder)
json.string(to_s)
end
end