struct UUID
inherits Struct
¶
Represents a UUID (Universally Unique IDentifier).
Class methods¶
.from_json_object_key?(key : String)
¶
(key : String)
Deserializes the given JSON key into a UUID
.
Note
require "uuid/json"
is required to opt-in to this feature.
.random(random = Random::Secure, variant = Variant::RFC4122, version = Version::V4)
¶
(random = Random::Secure, variant = Variant::RFC4122, version = Version::V4)
Generates RFC 4122 v4 UUID.
It is strongly recommended to use a cryptographically random source for
random, such as Random::Secure
.
.new(bytes : StaticArray(UInt8, 16), variant : UUID::Variant? = nil, version : UUID::Version? = nil)
¶
(bytes : StaticArray(UInt8, 16), variant : UUID::Variant? = nil, version : UUID::Version? = nil)
Generates UUID from bytes, applying version and variant to the UUID if present.
.new(slice : Slice(UInt8), variant = nil, version = nil)
¶
(slice : Slice(UInt8), variant = nil, version = nil)
Creates UUID from 16-bytes slice. Raises if slice isn't 16 bytes long. See
#initialize
for variant and version.
.new(uuid : UUID, variant = nil, version = nil)
¶
(uuid : UUID, variant = nil, version = nil)
Creates another UUID
which is a copy of uuid, but allows overriding
variant or version.
.new(value : String, variant = nil, version = nil)
¶
(value : String, variant = nil, version = nil)
Creates new UUID by decoding value
string from hyphenated (ie. ba714f86-cac6-42c7-8956-bcf5105e1b81
),
hexstring (ie. 89370a4ab66440c8add39e06f2bb6af6
) or URN (ie. urn:uuid:3f9eaf9e-cdb0-45cc-8ecb-0e5b2bfb0c20
)
format.
.new(pull : JSON::PullParser)
¶
(pull : JSON::PullParser)
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)
Methods¶
#==(other : self)
¶
(other : self)
#hash(hasher)
¶
(hasher)
#to_json(json : JSON::Builder)
¶
(json : JSON::Builder)
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\""