Created
March 24, 2024 04:46
-
-
Save danielsz/9cecc9d9c82808ad0a71a473824467d5 to your computer and use it in GitHub Desktop.
Cbor-clj parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns recursiveparser.cbor | |
(:require [clojure.tools.logging :as log]) | |
(:import [com.google.iot.cbor CborMap CborTextString CborInteger CborArray CborByteString CborSimple] | |
[co.nstant.in.cbor.model Map Array SimpleValue UnicodeString ByteString] | |
[peergos.shared.cbor CborObject$CborMap CborObject$CborString CborObject$CborByteArray CborObject$CborBoolean CborObject$CborList CborObject$CborLong CborObject$CborMerkleLink])) | |
(defprotocol Cbor | |
(parse [this])) | |
(extend-type CborMap | |
Cbor | |
(parse [this] | |
(let [m (into {} (.mapValue this))] ; or toNormalMap | |
(into {} (for [[k v] m] | |
[(keyword (parse k)) (parse v)]))))) | |
(extend-type Map | |
Cbor | |
(parse [this] | |
(let [m (zipmap (.getKeys this) (.getValues this))] | |
(into {} (for [[k v] m] | |
[(keyword (parse k)) (parse v)]))))) | |
(extend-type CborObject$CborMap | |
Cbor | |
(parse [this] | |
(let [sm (.values this) ; java.util.SortedMap | |
m (zipmap (.keySet sm) (.values sm))] | |
(into {} (for [[k v] m] | |
[(keyword (parse k)) (parse v)]))))) | |
(extend-type CborArray | |
Cbor | |
(parse [this] | |
(let [a (.listValue this)] | |
(mapv parse a)))) | |
(extend-type Array | |
Cbor | |
(parse [this] | |
(let [a (.getDataItems this)] | |
(mapv parse a)))) | |
(extend-type CborObject$CborList | |
Cbor | |
(parse [this] | |
(let [a (.value this)] | |
(mapv parse a)))) | |
(extend-type CborTextString | |
Cbor | |
(parse [this] | |
(.stringValue this))) | |
(extend-type CborObject$CborString | |
Cbor | |
(parse [this] | |
(.value this))) | |
(extend-type UnicodeString | |
Cbor | |
(parse [this] | |
(.getString this))) | |
(extend-type CborInteger | |
Cbor | |
(parse [this] | |
(.longValue this))) | |
(extend-type co.nstant.in.cbor.model.Number | |
Cbor | |
(parse [this] | |
(.getValue this))) | |
(extend-type CborObject$CborLong | |
Cbor | |
(parse [this] | |
(.value this))) | |
(extend-type CborByteString | |
Cbor | |
(parse [this] | |
(if (.isValidJson this) | |
(.toJsonString this) | |
(.byteArrayValue this)))) | |
(extend-type ByteString | |
Cbor | |
(parse [this] | |
(.getBytes this))) | |
(extend-type CborObject$CborByteArray | |
Cbor | |
(parse [this] | |
(.value this))) | |
(extend-type CborSimple | |
Cbor | |
(parse [this] | |
(case (.getValue this) | |
20 false | |
21 true | |
22 nil | |
23 nil))) | |
(extend-type SimpleValue | |
Cbor | |
(parse [this] | |
(case (.getValue this) | |
20 false | |
21 true | |
22 nil | |
23 nil))) | |
(extend-type CborObject$CborBoolean | |
Cbor | |
(parse [this] | |
(.value this))) | |
(extend-type CborObject$CborMerkleLink | |
Cbor | |
(parse [this] | |
(.links this))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment