CBOR::Any, complete.

any
Karchnu 2020-11-23 12:36:44 +01:00
parent 7a30bcf60b
commit ec1699aa25
2 changed files with 18 additions and 48 deletions

View File

@ -4,9 +4,15 @@
# ``` # ```
# require "json" # require "json"
# #
# obj = CBOR.parse(%({"access": [{"name": "mapping", "speed": "fast"}, {"name": "any", "speed": "slow"}]})) # obj = CBOR::Any.new JSON.parse(%({"access": [{"name": "mapping", "speed": "fast"}, {"name": "any", "speed": "slow"}]}))
# obj["access"][1]["name"].as_s # => "any" # obj["access"][1]["name"].as_s # => "any"
# obj["access"][1]["speed"].as_s # => "slow" # obj["access"][1]["speed"].as_s # => "slow"
#
# # through a cbor buffer
# hash = {"access" => [{"name" => "mapping", "speed" => "fast"}, {"name" => "any", "speed" => "slow"}]}
# obj2 = CBOR::Any.new hash.to_cbor
# obj2["access"][1]["name"].as_s # => "any"
# obj2["access"][1]["speed"].as_s # => "slow"
# ``` # ```
# #
# Note that methods used to traverse a CBOR structure, `#[]` and `#[]?`, # Note that methods used to traverse a CBOR structure, `#[]` and `#[]?`,
@ -16,25 +22,13 @@
# when the underlying value is not a String will raise: the value won't automatically # when the underlying value is not a String will raise: the value won't automatically
# be converted (parsed) to a `String`. # be converted (parsed) to a `String`.
struct CBOR::Any struct CBOR::Any
# All possible CBOR types. # All possible CBOR types.
alias Type = Nil | alias Type = Nil | Bool | String | Bytes |
Bool | Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 | Int64 | UInt64 | Int128 |
String | Float32 | Float64 |
Bytes |
Array(Any) | Array(Any) |
Hash(String, Any) | Hash(String, Any) | Hash(Any, Any)
Hash(Any, Any) |
Int8 |
UInt8 |
Int16 |
UInt16 |
Int32 |
UInt32 |
Int64 |
UInt64 |
Int128 |
Float32 |
Float64
# Reads a `CBOR::Any` from a JSON::Any structure. # Reads a `CBOR::Any` from a JSON::Any structure.
def self.new(json : JSON::Any) def self.new(json : JSON::Any)
@ -102,13 +96,11 @@ struct CBOR::Any
# Reads a `CBOR::Any` from a Decoder. # Reads a `CBOR::Any` from a Decoder.
def self.new(decoder : CBOR::Decoder) def self.new(decoder : CBOR::Decoder)
puts "from a decoder"
new decoder.read_value new decoder.read_value
end end
# Reads a `CBOR::Any` from a buffer. # Reads a `CBOR::Any` from a buffer.
def self.new(input : Slice(UInt8)) def self.new(input : Slice(UInt8))
puts "from a buffer"
new CBOR::Decoder.new(input) new CBOR::Decoder.new(input)
end end

View File

@ -10,35 +10,13 @@ class CBOR::Encoder
def initialize(@io : IO = IO::Memory.new) def initialize(@io : IO = IO::Memory.new)
end end
def write(cbor : CBOR::Any)
# Test each possible value of CBOR::Any
write cbor.raw
end
def write(j : JSON::Any) def write(j : JSON::Any)
# Test each possible value of JSON::Any write j.raw
case j
when .as_bool?
write j.as_bool
when .as_i?
write j.as_i
when .as_i64?
write j.as_i64
when .as_f?
write j.as_f
when .as_f32?
write j.as_f32
when .as_s?
write j.as_s
when .as_a?
write j.as_a
when .as_h?
write j.as_h
else
# JSON::Any ".as_bool?" function fails.
case j.raw
when Bool
write j.as_bool
when Nil
else
raise "Unknown kind of JSON: #{j.raw}"
end
end
end end
def write(value : Nil | Nil.class, use_undefined : Bool = false) def write(value : Nil | Nil.class, use_undefined : Bool = false)