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

TryFromJsValue #1975

Closed
lastmjs opened this issue Mar 24, 2022 · 4 comments
Closed

TryFromJsValue #1975

lastmjs opened this issue Mar 24, 2022 · 4 comments
Labels
API E-Medium Medium difficulty problem enhancement New feature or request
Milestone

Comments

@lastmjs
Copy link
Contributor

lastmjs commented Mar 24, 2022

Working on Azle which is a JS environment for the Internet Computer, I need to have a general solution for converting JsValues into arbitrary types. I have been attempting to do this using serde_json::from_value(js_value.to_json(& mut context).unwrap()).unwrap() which works in many cases, but breaks down sometimes (for example with undefined and bigint). It's also a more complicated developer experience and probably less performant than a direct conversion from JsValue to an arbitrary type would be.

The below is from this comment: #1961 (comment)

This would make a lot of sense, yes. We could have a TryFromJsValue trait that would receive a context and a JsValue and try to perform the conversion. Just thinking out loud, but I could imagine a similar API to the Serializa/Deserialize APIs in serde.

I would like to start a discussion on some sort of TryFromJsValue. Is this something you think boa could/would want to support? If so, what should the API look like?

I would like this functionality ASAP and so if it would be accepted and I could receive some guidance, I am happy to do the work. If it's relatively simple for the maintainers to go ahead and do it then I am happy with that as well. Just hope this can be done.

Thanks!

@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 24, 2022

To add to this, going the other way would also be really nice. I would like to be able to convert values of arbitrary Rust types into JsValues, and that capability seems limited as of right now.

@Razican
Copy link
Member

Razican commented Mar 24, 2022

To add to this, going the other way would also be really nice. I would like to be able to convert values of arbitrary Rust types into JsValues, and that capability seems limited as of right now.

Well, this is already possible for base Rust types. Maybe there should be another trait to make it simpler.

In general, I imagine this to work similarly to the Serialize and Deserialize traits, and they might even be derivable if we create the proper logic. I guess the best approach is to check how is Serde doing it, and try to inspire the solution on it.

@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 29, 2022

Having this API implemented would solve a host of issues for my project: demergent-labs/azle#142

I am ready to start working on this as soon as tomorrow, it's a major blocking issue for me. If you would accept a PR let me know, and I would love some guidance. If you want to wait for whatever reason, I would really appreciate some initial guidance as I will need to implement this functionality for myself no matter what.

@lastmjs
Copy link
Contributor Author

lastmjs commented Mar 30, 2022

I've started working on this, considering two traits IntoJsValue and FromJsValue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API E-Medium Medium difficulty problem enhancement New feature or request
Projects
None yet
3 participants