Closed Ziothh closed 10 months ago
Hi @Ziothh! I'm not aware of any crate to do that, no. The SWC plugin is more of a compiler extension that injects SuperJSON calls into a Next.js app during compile-time - it doesn't perform any de/serialisation in Rust.
SuperJSON is built to serialize exactly the data types that exist within Javascript. Other languages have different datatypes, and any conversion between the two will always be messy. So I don't think a JS/Rust interop would be super useful.
What's your usecase for this, and what other solutions have you looked at?
Hi @Skn0tt, thanks for the quick answer.
I came across this issue on prisma-client-rust
, where a query returns an i64
(BigInt in js) and gets passed to rspc
(like tRPC
but for js client <-> rust server).
BigInt can't be serialized to JSON, as you guys well know, so I was thinking about making a PR to rspc
to add transformer support like tRPC.
I think it would be useful if rust servers and js clients could communicate with SuperJSON. Some examples:
js <-> rust
BigInt <-> i64
Date <-> chrono::DateTime
Map <-> std::collections::HashMap
As a cherry on top specta
could then add support for these types to be generated for an even more seamless integration between the two languages.
That sounds interesting! There’s no such package currently. If you want to write one, i’m happy to assist.
There’s two main challenges I see with it, that will probably need solving:
According to the docs, rspc already has a code-processing step. My gut feeling is that it might be easier to have RSPC generate an API client that contains all the JS <-> RS conversion needed for the specific RPC calls, as opposed to writing a SuperJSON adapter.
I'm definitely down to write the package/crate but I'm a bit busy right now. After some research I found out that it's not trivial to translate the SuperJSON spec to serde so that's going to be fun.
My gut feeling is that it might be easier to have RSPC generate an API client that contains all the JS <-> RS conversion needed for the specific RPC calls, ... This is true but I'd like the crate to also be standalone to generate JSON files a be usable in API frameworks like axum.
I'll close this for now and I'll hit you up if I need your assistance :). Thanks for the help!
Does there exist a crate that can serialize
superjson
from and toserde-rs
structs?Correct me if I'm wrong, but the
next.js
swc
plugin just acts as a bridge between 2node
processes/modules right?