I’m brand new to Rust. I’m giving it a try as a result of the Instapaper API implementation. My application is working based on the examples provided there, but the output appears to be a Rust List object, rather than the JSON string that Instapaper normally returns. How do I get the List object output as JSON?
I’ve tried json::stringify as well as to_string() and serde_json but I don’t really understand the fundamentals well enough to get it to work.
Here’s an example API call from the examples provided with the crate:
println!("{:?}", client2.bookmarks().unwrap());
I understand that bookmarks() returns a list, but I don’t know how to cause it to be output as JSON so it can be manipulated with standard tools like JQ.
Obviously, you have to provide a Serialize impl for any type you want to use with Serde. In most cases, you can just #[derive(Serialize, Deserialize)].
It can't be done in this case, since the type in question is instapaper::List, not some local type. And it actually doesn't implement Serialize, since... I guess for crate authors this just doesn't make much sense? Like, if you've already got structured, strong-typed data, you can do any processing on that data (all its fields are public anyway), not converting it back to JSON?
Yeah, it can't be done, then. I somehow assumed OP was authoring this crate. In this case, the only practical solution I see is defining a newtype and implementing Serialize manually.
I guess OP is trying to output some standard representation that will be processed by downstream tools: