Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: How can I debug/inspect serde_json values in RustRover?
I'm not a novice programmer, but I am bit lost when trying to do my first debugging in Rust with RustRover. I suspect it might have something to do with how things are stored in collections, but I can't figure out how I can do something "simple" as looking into a serde_json Value
I have, to see what it contains. When I try to run functions on it, it gives complaints that certain fields don't exist.
This is the struct and part of the function that I am working with:
struct TidalSingleAlbumResponse {
data: serde_json::Value,
included: Vec<serde_json::Value>,
}
fn to_tidal_album(album: &TidalSingleAlbumResponse) -> Result<TidalAlbum, &str> {
let Some(serde_json::Value::String(id)) = album.data.get("id") else {
return Err("did not find id.");
};
// Title and barcode are in the "attributes" object
let Some(serde_json::Value::Object(attributes)) = album.data.get("attributes") else {
return Err("did not find attributes.");
};
let Some(serde_json::Value::String(title)) = attributes.get("title") else {
return Err("did not find title.");
};
// Get availability and convert it to Vec<String>
let Some(serde_json::Value::Array(availability)) = attributes.get("availability") else {
return Err("did not find availability.");
};
//// <--- SNIPPED CODE HERE --->
What I am trying to do is inspect album.data
and see what values it contains. Specifically, I am trying to see why on one JSON object it cannot find the "availability" key, when it is clear there in the response.
Expanding the whole tree on album.data
in the debugger tells me nothing useful, and as you can see: trying expressions like album.data.get("id")
do not work, while it does work in the function.
Also, something like album.data.as_object().unwrap().keys()
gives the following error in the debugger: error: extra tokens at EOF
.
So, this is what I tried to inspect the serde_json::Value
. How can I actually do something useful with the debugger in this case? Right now it feels like my only resort is to just log what is going on.
1 post - 1 participant
🏷️ Rust_feed