File tree 4 files changed +68
-0
lines changed
4 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1
1
require " spec"
2
2
require " json"
3
+ require " uuid"
4
+ require " uuid/json"
3
5
require " big/json"
4
6
5
7
private class JSONPerson
@@ -32,6 +34,10 @@ private class JSONWithBool
32
34
JSON .mapping value: Bool
33
35
end
34
36
37
+ private class JSONWithUUID
38
+ JSON .mapping value: UUID
39
+ end
40
+
35
41
private class JSONWithBigDecimal
36
42
JSON .mapping value: BigDecimal
37
43
end
@@ -249,6 +255,12 @@ describe "JSON mapping" do
249
255
json.value.should be_false
250
256
end
251
257
258
+ it " parses UUID" do
259
+ uuid = JSONWithUUID .from_json(%( {"value": "ba714f86-cac6-42c7-8956-bcf5105e1b81"}) )
260
+ uuid.should be_a(JSONWithUUID )
261
+ uuid.value.should eq(UUID .new(" ba714f86-cac6-42c7-8956-bcf5105e1b81" ))
262
+ end
263
+
252
264
it " parses json with Time::Format converter" do
253
265
json = JSONWithTime .from_json(%( {"value": "2014-10-31 23:37:16"}) )
254
266
json.value.should be_a(Time )
Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ require "spec"
2
2
require " json"
3
3
require " big"
4
4
require " big/json"
5
+ require " uuid"
6
+ require " uuid/json"
5
7
6
8
enum JSONSpecEnum
7
9
Zero
@@ -91,6 +93,24 @@ describe "JSON serialization" do
91
93
big.should eq(BigFloat .new(" 1234" ))
92
94
end
93
95
96
+ it " does for UUID (hyphenated)" do
97
+ uuid = UUID .from_json(" \" ee843b26-56d8-472b-b343-0b94ed9077ff\" " )
98
+ uuid.should be_a(UUID )
99
+ uuid.should eq(UUID .new(" ee843b26-56d8-472b-b343-0b94ed9077ff" ))
100
+ end
101
+
102
+ it " does for UUID (hex)" do
103
+ uuid = UUID .from_json(" \" ee843b2656d8472bb3430b94ed9077ff\" " )
104
+ uuid.should be_a(UUID )
105
+ uuid.should eq(UUID .new(" ee843b26-56d8-472b-b343-0b94ed9077ff" ))
106
+ end
107
+
108
+ it " does for UUID (urn)" do
109
+ uuid = UUID .from_json(" \" urn:uuid:ee843b26-56d8-472b-b343-0b94ed9077ff\" " )
110
+ uuid.should be_a(UUID )
111
+ uuid.should eq(UUID .new(" ee843b26-56d8-472b-b343-0b94ed9077ff" ))
112
+ end
113
+
94
114
it " does for BigDecimal from int" do
95
115
big = BigDecimal .from_json(" 1234" )
96
116
big.should be_a(BigDecimal )
@@ -281,6 +301,11 @@ describe "JSON serialization" do
281
301
big = BigFloat .new(" 1234.567891011121314" )
282
302
big.to_json.should eq(" 1234.567891011121314" )
283
303
end
304
+
305
+ it " does for UUID" do
306
+ uuid = UUID .new(" ee843b26-56d8-472b-b343-0b94ed9077ff" )
307
+ uuid.to_json.should eq(" \" ee843b26-56d8-472b-b343-0b94ed9077ff\" " )
308
+ end
284
309
end
285
310
286
311
describe " to_pretty_json" do
Original file line number Diff line number Diff line change @@ -57,5 +57,6 @@ require "./string_scanner"
57
57
require " ./tempfile"
58
58
require " ./uri"
59
59
require " ./uuid"
60
+ require " ./uuid/json"
60
61
require " ./zip"
61
62
require " ./zlib"
Original file line number Diff line number Diff line change
1
+ require " json"
2
+ require " uuid"
3
+
4
+ # Adds JSON support to `UUID` for use in a JSON mapping.
5
+ #
6
+ # NOTE: `require "uuid/json"` is required to opt-in to this feature.
7
+ #
8
+ # ```
9
+ # require "json"
10
+ # require "uuid"
11
+ # require "uuid/json"
12
+ #
13
+ # class Example
14
+ # JSON.mapping id: UUID
15
+ # end
16
+ #
17
+ # example = Example.from_json(%({"id": "ba714f86-cac6-42c7-8956-bcf5105e1b81"}))
18
+ #
19
+ # uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
20
+ # uuid.to_json # => "87b3042b-9b9a-41b7-8b15-a93d3f17025e"
21
+ # ```
22
+ struct UUID
23
+ def self.new (pull : JSON ::PullParser )
24
+ new(pull.read_string)
25
+ end
26
+
27
+ def to_json (json : JSON ::Builder )
28
+ json.string(to_s)
29
+ end
30
+ end
You can’t perform that action at this time.
0 commit comments