cognitect / transit-cljs

Transit for ClojureScript
http://transit-format.org
Apache License 2.0
323 stars 20 forks source link

reading/writing to js object #63

Open simonacca opened 2 months ago

simonacca commented 2 months ago

tl;dr: seems like there's no public api in transit-cljs to de/serialize to and from JS objects. If that's the case, could one be added?

Hi there,
my use case for transit is to store cljs data structures in IndexedDB. IndexedDB can store any serializable object as a value (see spec).

I looked into the on-disk size of storing transit values as a string (that is, a value produced by calling transit-cljs's write) vs a "js object" representation (that is, what's produced by calling JSON.parse onto the previous string value).

It looks like the js object representation is about 20% more efficient in terms of disk size than the string representation for my workload.

That said, there doesn't seem to be a public api to produce the "js object" representation from transit-cljs. Is that so? If yes, could one be added?

In terms of API, it could be one more reader/write type, perhaps :js-object and :js-object-verbose.

Thanks!

P.S. Here's an attempt of hacking this together: The writer seems straightforward enough: (.write writer in #js {"marshalTop" false}). While for the reader we have to hook into more internal components

(let [reader (transit/reader :json {})
        cache (.-cache reader)
        decoded (-> reader .-unmarshaller .-decoder (.decode my-js-object))]
    (.clear cache)
    decoded)