Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Serializing CLJS Bean instances via transit preserving meta
$ clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "1.10.520"} com.cognitect/transit-cljs {:mvn/version "0.8.256"} com.cognitect/transit-js {:mvn/version "0.8.861"} cljs-bean {:mvn/version "1.4.0"}}}' -m cljs.main -re node -r
ClojureScript 1.10.520
(require '[cognitect.transit :as t]
         '[cljs-bean.core :refer [->clj]]
cljs.user=> (defn roundtrip [x]
              (let [w (t/writer :json
                        {:handlers (cljs-bean.transit/writer-handlers)
                         :transform t/write-meta})
                    r (t/reader :json)]
                (t/read r (t/write w x))))
cljs.user=> (def x (with-meta (->clj #js {:a 1}) {:id "some-ref" :ref "/some/path"}))
cljs.user=> (type x)
cljs.user=> (meta x)
{:id "some-ref", :ref "/some/path"}
cljs.user=> (def y (roundtrip x))
cljs.user=> y
{:a 1}
cljs.user=> (type y)
cljs.user=> (meta y)
{:id "some-ref", :ref "/some/path"}
Copy link

Commits in

(The need to ship with a newer transit-js doesn’t appear to be intentional; just a result of this commit fixing up the pom occurring after release: cognitect/transit-cljs@b5a67e7) — Mike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment