Skip to content

Commit

Permalink
Add JSON support to UUID (#5551)
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlan authored and RX14 committed Mar 18, 2018
1 parent c0cdbc2 commit 5d5c9ac
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/std/json/mapping_spec.cr
@@ -1,5 +1,7 @@
require "spec"
require "json"
require "uuid"
require "uuid/json"
require "big/json"

private class JSONPerson
Expand Down Expand Up @@ -32,6 +34,10 @@ private class JSONWithBool
JSON.mapping value: Bool
end

private class JSONWithUUID
JSON.mapping value: UUID
end

private class JSONWithBigDecimal
JSON.mapping value: BigDecimal
end
Expand Down Expand Up @@ -249,6 +255,12 @@ describe "JSON mapping" do
json.value.should be_false
end

it "parses UUID" do
uuid = JSONWithUUID.from_json(%({"value": "ba714f86-cac6-42c7-8956-bcf5105e1b81"}))
uuid.should be_a(JSONWithUUID)
uuid.value.should eq(UUID.new("ba714f86-cac6-42c7-8956-bcf5105e1b81"))
end

it "parses json with Time::Format converter" do
json = JSONWithTime.from_json(%({"value": "2014-10-31 23:37:16"}))
json.value.should be_a(Time)
Expand Down
25 changes: 25 additions & 0 deletions spec/std/json/serialization_spec.cr
Expand Up @@ -2,6 +2,8 @@ require "spec"
require "json"
require "big"
require "big/json"
require "uuid"
require "uuid/json"

enum JSONSpecEnum
Zero
Expand Down Expand Up @@ -91,6 +93,24 @@ describe "JSON serialization" do
big.should eq(BigFloat.new("1234"))
end

it "does for UUID (hyphenated)" do
uuid = UUID.from_json("\"ee843b26-56d8-472b-b343-0b94ed9077ff\"")
uuid.should be_a(UUID)
uuid.should eq(UUID.new("ee843b26-56d8-472b-b343-0b94ed9077ff"))
end

it "does for UUID (hex)" do
uuid = UUID.from_json("\"ee843b2656d8472bb3430b94ed9077ff\"")
uuid.should be_a(UUID)
uuid.should eq(UUID.new("ee843b26-56d8-472b-b343-0b94ed9077ff"))
end

it "does for UUID (urn)" do
uuid = UUID.from_json("\"urn:uuid:ee843b26-56d8-472b-b343-0b94ed9077ff\"")
uuid.should be_a(UUID)
uuid.should eq(UUID.new("ee843b26-56d8-472b-b343-0b94ed9077ff"))
end

it "does for BigDecimal from int" do
big = BigDecimal.from_json("1234")
big.should be_a(BigDecimal)
Expand Down Expand Up @@ -281,6 +301,11 @@ describe "JSON serialization" do
big = BigFloat.new("1234.567891011121314")
big.to_json.should eq("1234.567891011121314")
end

it "does for UUID" do
uuid = UUID.new("ee843b26-56d8-472b-b343-0b94ed9077ff")
uuid.to_json.should eq("\"ee843b26-56d8-472b-b343-0b94ed9077ff\"")
end
end

describe "to_pretty_json" do
Expand Down
1 change: 1 addition & 0 deletions src/docs_main.cr
Expand Up @@ -57,5 +57,6 @@ require "./string_scanner"
require "./tempfile"
require "./uri"
require "./uuid"
require "./uuid/json"
require "./zip"
require "./zlib"
30 changes: 30 additions & 0 deletions src/uuid/json.cr
@@ -0,0 +1,30 @@
require "json"
require "uuid"

# Adds JSON support to `UUID` for use in a JSON mapping.
#
# NOTE: `require "uuid/json"` is required to opt-in to this feature.
#
# ```
# require "json"
# require "uuid"
# require "uuid/json"
#
# class Example
# JSON.mapping id: UUID
# end
#
# example = Example.from_json(%({"id": "ba714f86-cac6-42c7-8956-bcf5105e1b81"}))
#
# uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
# uuid.to_json # => "87b3042b-9b9a-41b7-8b15-a93d3f17025e"
# ```
struct UUID
def self.new(pull : JSON::PullParser)
new(pull.read_string)
end

def to_json(json : JSON::Builder)
json.string(to_s)
end
end

0 comments on commit 5d5c9ac

Please sign in to comment.