Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading/writing to js object #63

Open
simonacca opened this issue Jun 21, 2024 · 0 comments
Open

reading/writing to js object #63

simonacca opened this issue Jun 21, 2024 · 0 comments

Comments

@simonacca
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant