Output List generated by Instapaper API as JSON string

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.

Thanks for any guidance!

You want the serde_json::to_string() free function.

Maybe I'm doing it wrong?

the trait `serde::ser::Serialize` is not implemented for `List`

Here's the code:

          let inbox = client.bookmarks().unwrap();

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:

In that case, the OP can:

  1. fork instapaper and add Serialize, Deserialize implementations, perhaps under a serde feature flag and try to get it merged into upstream
  2. write a mapping from instapaper types to custom re-implementations of the instapaper types that do implement Serialize
  3. write a mapping from instapaper types to json::Value
  4. emit the json by hand

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.