From: Karchnu <karchnu@karchnu.fr>
---
This allows to write any JSON::Any data in the CBOR format.
The commit
only adds a `write(JSON::Any)` function in the decoder.
src/cbor/encoder.cr | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/src/cbor/encoder.cr b/src/cbor/encoder.cr
index 65e5e63..c6d436b 100644
--- a/src/cbor/encoder.cr+++ b/src/cbor/encoder.cr
@@ -1,3 +1,5 @@
+require "json"+class CBOR::Encoder
def self.new(io : IO = IO::Memory.new)
packer = new(io)
@@ -8,6 +10,37 @@ class CBOR::Encoder
def initialize(@io : IO = IO::Memory.new)
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+ end+ def write(value : Nil | Nil.class, use_undefined : Bool = false)
write(use_undefined ? SimpleValue::Undefined : SimpleValue::Null)
end
--
2.26.2