Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active September 8, 2019 21:33
Show Gist options
  • Save mfikes/3a160a1504debd31e5771736256ca022 to your computer and use it in GitHub Desktop.
Save mfikes/3a160a1504debd31e5771736256ca022 to your computer and use it in GitHub Desktop.
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-bean.transit])
nil
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/roundtrip
cljs.user=> (def x (with-meta (->clj #js {:a 1}) {:id "some-ref" :ref "/some/path"}))
#'cljs.user/x
cljs.user=> (type x)
cljs-bean.core/Bean
cljs.user=> (meta x)
{:id "some-ref", :ref "/some/path"}
cljs.user=> (def y (roundtrip x))
#'cljs.user/y
cljs.user=> y
{:a 1}
cljs.user=> (type y)
cljs.core/PersistentArrayMap
cljs.user=> (meta y)
{:id "some-ref", :ref "/some/path"}
@martinklepsch
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