Time: allow to use String representations, as produced by #to_json.

remotes/dev/pr-cbor-any-and-time
Karchnu 2020-11-26 06:51:29 +01:00
parent 199c8c0d79
commit 2fdfa94ea5
1 changed files with 16 additions and 10 deletions

View File

@ -134,6 +134,11 @@ end
# #
# [1]: https://tools.ietf.org/html/rfc7049#section-2.4.1 # [1]: https://tools.ietf.org/html/rfc7049#section-2.4.1
def Time.new(decoder : CBOR::Decoder) def Time.new(decoder : CBOR::Decoder)
# In case Time is formatted as a JSON#to_json String.
case decoder.current_token
when CBOR::Token::StringT
Time::Format::RFC_3339.parse(decoder.read_string)
else
case tag = decoder.read_tag case tag = decoder.read_tag
when CBOR::Tag::RFC3339Time when CBOR::Tag::RFC3339Time
Time::Format::RFC_3339.parse(decoder.read_string) Time::Format::RFC_3339.parse(decoder.read_string)
@ -148,6 +153,7 @@ def Time.new(decoder : CBOR::Decoder)
raise CBOR::ParseError.new("Expected tag to have value 0 or 1, got #{tag.value}") raise CBOR::ParseError.new("Expected tag to have value 0 or 1, got #{tag.value}")
end end
end end
end
# Reads the CBOR value as a BigInt. # Reads the CBOR value as a BigInt.
# If the next token is an integer, then the integer will be transformed as a # If the next token is an integer, then the integer will be transformed as a