Simon Danisch / Feb 27 2019

CBOR

pkg"add https://github.com/JuliaIO/CBOR.jl.git#sd-round_trip"
using CBOR
using Plots; plotly()
open(io-> write(io, CBOR.encode([2,3,4,5])), "/results/cbor_array", "w");
cbor_array
open(io-> write(io, CBOR.encode(Dict("a" => 22, "b" => 33))), "/results/cbor_dict", "w");
cbor_dict
open(io-> write(io, CBOR.encode("hiiii")), "/results/cbor_string", "w");
cbor_string
open(io-> write(io, CBOR.encode(Pair(7777, "hiiii"))), "/results/cbor_tag", "w");
cbor_tag

Custom Julia Types

open(io-> write(io, CBOR.encode("a" => 22)), "/results/cbor_pair", "w");
cbor_pair
println(CBOR.decode(read(cbor_pair)))
using AbstractPlotting
open(io-> write(io, CBOR.encode(Point2f0(0, 1))), "/results/cbor_point", "w");
cbor_point
CBOR.decode(read(cbor_point)) == Point2f0(0, 1)

Closures:

a = 42
f = (x)-> a + x
f(22)
f2 = CBOR.decode(CBOR.encode(f))
f2(22)

Dict with closure:

d = CBOR.decode(CBOR.encode(Dict("myclosure" => f)))
d["myclosure"](22)

Random type containing Closure (TODO: doesn't yet work because of type missmatch)

d = CBOR.decode(CBOR.encode(f))
typeof(d)
using Serialization
io = IOBuffer(); serialize(io, "asd" => f); seekstart(io); 
typeof(deserialize(io)) |> println
isbitstype(typeof("asd" => f))
 pip install cbor2
import cbor2
bs = open(cbor_array, "rb");
result = cbor.loads(bs.read());
bs.close()
result
bs = open(cbor_dict, "rb");
result = cbor.loads(bs.read());
bs.close()
result
bs = open(cbor_string, "rb");
result = cbor.loads(bs.read());
bs.close()
result
bs = open(cbor_tag, "rb");
result = cbor.loads(bs.read());
bs.close()
result
result = cbor.loads(cbor.dumps((2,3,4)))
type(result)
bs = open(nil, "rb");
result = cbor.loads(bs.read());
bs.close()
result

bs = open(cbor_point, "rb");
result = cbor2.loads(bs.read());
bs.close()
result

1.
CBOR + Clojure

{:deps
 {
  org.clojure/clojure {:mvn/version "1.10.0"}
  mvxcvi/clj-cbor {:mvn/version "0.7.1"}
 }
}
deps.edn
Clojure
(require '[clj-cbor.core :as cbor])
(cbor/slurp cbor_array)
4[2,3,4,5]
(cbor/slurp cbor_dict)
2{"a"22"b"33}
(print (cbor/slurp cbor_point))