~arestifo/crystal-cbor

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 3

[PATCH crystal-cbor] Write JSON::Any

Details
Message ID
<160530607904.31916.16405687376914283761-0@git.sr.ht>
DKIM signature
missing
Download raw message
Patch: +33 -0
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

[crystal-cbor/patches/.build.yml] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<C72HIRXFW1EI.29Z15ZCCTHAZR@cirno2>
In-Reply-To
<160530607904.31916.16405687376914283761-0@git.sr.ht> (view parent)
DKIM signature
missing
Download raw message
crystal-cbor/patches/.build.yml: SUCCESS in 24s

[Write JSON::Any][0] from [~karchnu][1]

[0]: https://lists.sr.ht/~arestifo/crystal-cbor/patches/14986
[1]: karchnu@karchnu.fr

✓ #341774 SUCCESS crystal-cbor/patches/.build.yml https://builds.sr.ht/~arestifo/job/341774
Details
Message ID
<C7BL6NMSPFV4.2MGJO2XZRNLND@Albertos-MacBook-Pro.local>
In-Reply-To
<160530607904.31916.16405687376914283761-0@git.sr.ht> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
It would be great if you could add some tests for this
Details
Message ID
<9a8a9e31-b5dd-a273-aae1-cae7075ba0ff@karchnu.fr>
In-Reply-To
<C7BL6NMSPFV4.2MGJO2XZRNLND@Albertos-MacBook-Pro.local> (view parent)
DKIM signature
missing
Download raw message
On 24/11/2020 16:07, Alberto Restifo wrote:
> It would be great if you could add some tests for this

I did, and there is a lot of code I didn't send, yet.
Will do that probably this week-end, sooner if I find the motivation.

It will be soon up to par with the JSON library (I hope, at least).
For now, I do have something that is working, I did a few programs [1] but the API may be improved to be as simple as the JSON one.
I think about the main loop in my `cq` command [2], which is way too complex for what I want to do.


[1]: https://git.baguette.netlib.re/Baguette/cq
[2]: https://git.baguette.netlib.re/Baguette/cq/src/branch/master/src/cq.cr#L23
Reply to thread Export thread (mbox)