Skip to content

Instantly share code, notes, and snippets.

Embed
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-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

This comment has been minimized.

Copy link

commented Sep 8, 2019

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
You can’t perform that action at this time.