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

Serialize a UniFFI object to JSON from the foreign platform code #2357

Open
chris13524 opened this issue Dec 17, 2024 · 2 comments
Open

Serialize a UniFFI object to JSON from the foreign platform code #2357

chris13524 opened this issue Dec 17, 2024 · 2 comments

Comments

@chris13524
Copy link

chris13524 commented Dec 17, 2024

Consumers of my Rust code, in Kotlin and Swift, want to serialize several of our #[uniffi::Record] objects to a JSON string (in order to pass it through yet another layer of FFI). Is this possible currently?

In the docs it looks like UniFFI supports exposing functions from standard Rust crates, however serde::Serialize is not listed here.

I've tried creating a generic helper function to serialize any serializable object. However it looks like #[uniffi::export] doesn't support functions with generic arguments. I could create a separate exported function for each JSON-serializable type, but this would be inconvenient.

For Swift specifically, ideally the generated type implements Encodable.

@mhammond
Copy link
Member

As you noted, functions with generic args don't work. While records can't have methods, you could have a namespace function which does the serialize - eg fn serialize_my_record(record: &MyRecord) -> String

Re Encodable, #1905 sketches out a proposal which seems workable, but I don't think anyone has started working on that at this stage.

@chris13524
Copy link
Author

you could have a namespace function which does the serialize - eg fn serialize_my_record(record: &MyRecord) -> String

Got it, so I need to make many functions, one for each type they want to serialize.

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

2 participants