How do I make Objects, Maps, etc. in rusty_v8 into easy-to-understand String?
Hello. I wrote the following code in rusty_v8
Numbers such as 1+1 and strings such as "Hello!" are output correctly, but objects such as
{ a: 1 }
are displayed as [object Object]
. Is there any way to fix this?2 Replies
The issue you're encountering is due to the fact that the
to_string
method in V8 converts JavaScript objects to their default string representation, which for objects is [object Object]
.
To get a string representation of an object that includes its properties, you would typically use JSON.stringify
in JavaScript. However, the rusty_v8
crate does not provide a direct way to call JavaScript functions like JSON.stringify
.
The documentation does not provide a clear solution for this specific issue. However, you might consider creating a JavaScript function in your runtime that uses JSON.stringify
and then call this function from Rust.
Please note that this advice is not explicitly supported by the documentation and might require additional research and testing.JSON.stringify can't handle Map or something class