(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)))