Skip to content

Commit 5d5c9ac

Browse files
lachlanRX14
authored andcommitted
Add JSON support to UUID (#5551)
1 parent c0cdbc2 commit 5d5c9ac

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

spec/std/json/mapping_spec.cr

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require "spec"
22
require "json"
3+
require "uuid"
4+
require "uuid/json"
35
require "big/json"
46

57
private class JSONPerson
@@ -32,6 +34,10 @@ private class JSONWithBool
3234
JSON.mapping value: Bool
3335
end
3436

37+
private class JSONWithUUID
38+
JSON.mapping value: UUID
39+
end
40+
3541
private class JSONWithBigDecimal
3642
JSON.mapping value: BigDecimal
3743
end
@@ -249,6 +255,12 @@ describe "JSON mapping" do
249255
json.value.should be_false
250256
end
251257

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+
252264
it "parses json with Time::Format converter" do
253265
json = JSONWithTime.from_json(%({"value": "2014-10-31 23:37:16"}))
254266
json.value.should be_a(Time)

spec/std/json/serialization_spec.cr

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ require "spec"
22
require "json"
33
require "big"
44
require "big/json"
5+
require "uuid"
6+
require "uuid/json"
57

68
enum JSONSpecEnum
79
Zero
@@ -91,6 +93,24 @@ describe "JSON serialization" do
9193
big.should eq(BigFloat.new("1234"))
9294
end
9395

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+
94114
it "does for BigDecimal from int" do
95115
big = BigDecimal.from_json("1234")
96116
big.should be_a(BigDecimal)
@@ -281,6 +301,11 @@ describe "JSON serialization" do
281301
big = BigFloat.new("1234.567891011121314")
282302
big.to_json.should eq("1234.567891011121314")
283303
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
284309
end
285310

286311
describe "to_pretty_json" do

src/docs_main.cr

+1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ require "./string_scanner"
5757
require "./tempfile"
5858
require "./uri"
5959
require "./uuid"
60+
require "./uuid/json"
6061
require "./zip"
6162
require "./zlib"

src/uuid/json.cr

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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

0 commit comments

Comments
 (0)