i'm trying deserialize spotify metadata json web api (specifications). i'm using hyper retrieve json server , serde turn json can use within rust. json retrieved server fine, when try turn json object can used rust panicks , throws error:
thread '<main>' panicked @ 'called 'result::unwrap()' on 'err' value: syntaxerror("expected value", 11, 21)', ../src/libcore/result.rs:746
this not helpful in least way, because doesn't indicate things go wrong @ all. when searching web stumbled upon a serde issue, leads me think problem related nested structure of json.
can see things go wrong? fixing error best solution me, if crate turns out better solution i'd hear too. i've tried rustc-serialize, crate can't handle 'type' variables within json.
the code use is:
#![feature(custom_derive, plugin)] #![plugin(serde_macros)] #![feature(custom_attribute)] extern crate hyper; extern crate serde; extern crate serde_json; use std::io::read; use hyper::client; use hyper::header::connection; #[derive(serialize, deserialize)] struct track_full { album: album_simp, artists: vec<artist_simp>, available_markets: vec<string>, disc_number: u8, duration_ms: u32, explicit: bool, external_ids: external_ids, external_urls: external_urls, href: string, id: string, name: string, popularity: u8, preview_url: string, track_number: u8, #[serde(rename="type")] _type: string, uri: string } #[derive(serialize, deserialize)] struct album_simp { album_type: string, available_markets: vec<string>, external_urls: external_urls, href: string, id: string, images: vec<image>, name: string, #[serde(rename="type")] _type: string, uri: string } #[derive(serialize, deserialize)] struct artist_simp { external_urls: external_urls, href: string, id: string, name: string, #[serde(rename="type")] _type: string, uri: string } #[derive(serialize, deserialize)] struct external_ids { isrc: string } #[derive(serialize, deserialize)] struct external_urls { spotify: string } #[derive(serialize, deserialize)] struct image { height: u8, url: string, width: u8 } fn main() { // create client. let mut client = client::new(); // creating outgoing request. let mut res = client.get("https://api.spotify.com/v1/tracks/0egsygtp906u18l0oimnem") // set header .header(connection::close()) // let 'er go! .send().unwrap(); // read response. let mut body = string::new(); res.read_to_string(&mut body).unwrap(); println!("{}", body); let deserialized: track_full = serde_json::from_str(&body).unwrap(); }
the json:
{ "album" : { "album_type" : "album", "available_markets" : [ "ad", "ar", "at", "au", "be", "bg", "bo", "br", "ch", "cl", "co", "cr", "cy", "cz", "de", "dk", "do", "ec", "ee", "es", "fi", "fr", "gr", "gt", "hk", "hn", "hu", "ie", "is", "it", "li", "lt", "lu", "lv", "mc", "mt", "my", "ni", "nl", "no", "nz", "pa", "pe", "ph", "pl", "pt", "py", "ro", "se", "sg", "si", "sk", "sv", "tr", "tw", "uy" ], "external_urls" : { "spotify" : "https://open.spotify.com/album/6tjmqno44ye5bttxh8pop1" }, "href" : "https://api.spotify.com/v1/albums/6tjmqno44ye5bttxh8pop1", "id" : "6tjmqno44ye5bttxh8pop1", "images" : [ { "height" : 640, "url" : "https://i.scdn.co/image/8e13218039f81b000553e25522a7f0d7a0600f2e", "width" : 629 }, { "height" : 300, "url" : "https://i.scdn.co/image/8c1e066b5d1045038437d92815d49987f519e44f", "width" : 295 }, { "height" : 64, "url" : "https://i.scdn.co/image/d49268a8fc0768084f4750cf1647709e89a27172", "width" : 63 } ], "name" : "hot fuss", "type" : "album", "uri" : "spotify:album:6tjmqno44ye5bttxh8pop1" }, "artists" : [ { "external_urls" : { "spotify" : "https://open.spotify.com/artist/0c0xlulifjtagn6zncw2eu" }, "href" : "https://api.spotify.com/v1/artists/0c0xlulifjtagn6zncw2eu", "id" : "0c0xlulifjtagn6zncw2eu", "name" : "the killers", "type" : "artist", "uri" : "spotify:artist:0c0xlulifjtagn6zncw2eu" } ], "available_markets" : [ "ad", "ar", "at", "au", "be", "bg", "bo", "br", "ch", "cl", "co", "cr", "cy", "cz", "de", "dk", "do", "ec", "ee", "es", "fi", "fr", "gr", "gt", "hk", "hn", "hu", "ie", "is", "it", "li", "lt", "lu", "lv", "mc", "mt", "my", "ni", "nl", "no", "nz", "pa", "pe", "ph", "pl", "pt", "py", "ro", "se", "sg", "si", "sk", "sv", "tr", "tw", "uy" ], "disc_number" : 1, "duration_ms" : 222075, "explicit" : false, "external_ids" : { "isrc" : "usir20400274" }, "external_urls" : { "spotify" : "https://open.spotify.com/track/0egsygtp906u18l0oimnem" }, "href" : "https://api.spotify.com/v1/tracks/0egsygtp906u18l0oimnem", "id" : "0egsygtp906u18l0oimnem", "name" : "mr. brightside", "popularity" : 74, "preview_url" : "https://p.scdn.co/mp3-preview/934da7155ec15deb326635d69d050543ecbee2b4", "track_number" : 2, "type" : "track", "uri" : "spotify:track:0egsygtp906u18l0oimnem" }
you've attempted parse json , failed. when called unwrap
on result
, program panicked because of failure:
syntaxerror("expected value", 11, 21)
the documentation syntaxerror
says numbers line , column of error. line 11, column 21 is:
"height" : 640, ^
looking @ structure, have declared height u8
, 8-bit unsigned number. has allowed values of 0-255. 640 not fit that. increasing value u32
allows json parsed.
additionally, rust style use camelcase
identifiers without consecutive capital letters structs. external_urls
-> externalurls
. compiler warn this.
Comments
Post a Comment