From ec1699aa25e2d6c051388e59b7b321786ced9afd Mon Sep 17 00:00:00 2001 From: Karchnu Date: Mon, 23 Nov 2020 12:36:44 +0100 Subject: [PATCH] CBOR::Any, complete. --- src/cbor/any.cr | 32 ++++++++++++-------------------- src/cbor/encoder.cr | 34 ++++++---------------------------- 2 files changed, 18 insertions(+), 48 deletions(-) diff --git a/src/cbor/any.cr b/src/cbor/any.cr index 72dfcc3..f6a551b 100644 --- a/src/cbor/any.cr +++ b/src/cbor/any.cr @@ -4,9 +4,15 @@ # ``` # 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]["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 `#[]?`, @@ -16,25 +22,13 @@ # when the underlying value is not a String will raise: the value won't automatically # be converted (parsed) to a `String`. struct CBOR::Any + # All possible CBOR types. - alias Type = Nil | - Bool | - String | - Bytes | + alias Type = Nil | Bool | String | Bytes | + Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 | Int64 | UInt64 | Int128 | + Float32 | Float64 | Array(Any) | - Hash(String, Any) | - Hash(Any, Any) | - Int8 | - UInt8 | - Int16 | - UInt16 | - Int32 | - UInt32 | - Int64 | - UInt64 | - Int128 | - Float32 | - Float64 + Hash(String, Any) | Hash(Any, Any) # Reads a `CBOR::Any` from a JSON::Any structure. def self.new(json : JSON::Any) @@ -102,13 +96,11 @@ struct CBOR::Any # Reads a `CBOR::Any` from a Decoder. def self.new(decoder : CBOR::Decoder) - puts "from a decoder" new decoder.read_value end # Reads a `CBOR::Any` from a buffer. def self.new(input : Slice(UInt8)) - puts "from a buffer" new CBOR::Decoder.new(input) end diff --git a/src/cbor/encoder.cr b/src/cbor/encoder.cr index c6d436b..b355a59 100644 --- a/src/cbor/encoder.cr +++ b/src/cbor/encoder.cr @@ -10,35 +10,13 @@ class CBOR::Encoder def initialize(@io : IO = IO::Memory.new) end + def write(cbor : CBOR::Any) + # Test each possible value of CBOR::Any + write cbor.raw + end + def write(j : JSON::Any) - # Test each possible value of JSON::Any - 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 + write j.raw end def write(value : Nil | Nil.class, use_undefined : Bool = false)