nim-serde/serde/cbor/jsonhook.nim
2025-05-22 16:39:37 +05:30

46 lines
1.2 KiB
Nim
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# This file is a modified version of Emery Hemingways CBOR library for Nim,
# originally available at https://github.com/ehmry/cbor-nim and released under The Unlicense.
import std/[base64, tables]
import ../json/stdjson
import ./types
import ./errors
import ./deserializer
proc toJsonHook*(n: CborNode): JsonNode =
case n.kind:
of cborUnsigned:
newJInt n.uint.BiggestInt
of cborNegative:
newJInt n.int.BiggestInt
of cborBytes:
newJString base64.encode(cast[string](n.bytes), safe = true)
of cborText:
newJString n.text
of cborArray:
let a = newJArray()
for e in n.seq.items:
a.add(e.toJsonHook)
a
of cborMap:
let o = newJObject()
for k, v in n.map.pairs:
if k.kind == cborText:
o[k.text] = v.toJsonHook
else:
o[$k] = v.toJsonHook
o
of cborTag: nil
of cborSimple:
if n.isBool:
newJBool(n.getBool())
elif n.isNull:
newJNull()
else: nil
of cborFloat:
newJFloat n.float
of cborRaw:
without parsed =? parseCbor(n.raw), error:
raise newCborError(error.msg)
toJsonHook(parsed)