internal static LastAlbum ParseJToken(JToken token) { var a = new LastAlbum(); a.Id = token.Value <string>("id"); var artistToken = token["artist"]; switch (artistToken.Type) { case JTokenType.String: a.ArtistName = token.Value <string>("artist"); break; case JTokenType.Object: a.ArtistName = artistToken.Value <string>("name"); break; } var tracksToken = token.SelectToken("tracks"); if (tracksToken != null) { var trackToken = tracksToken.SelectToken("track"); if (trackToken != null) { a.Tracks = trackToken.Type == JTokenType.Array ? trackToken.Children().Select(t => LastTrack.ParseJToken(t, a.Name)) : new List <LastTrack>() { LastTrack.ParseJToken(trackToken, a.Name) } } ; } else { a.Tracks = Enumerable.Empty <LastTrack>(); } var tagsToken = token.SelectToken("toptags"); if (tagsToken != null) { var tagToken = tagsToken.SelectToken("tag"); if (tagToken != null) { a.TopTags = tagToken.Type == JTokenType.Array ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1)) : new List <LastTag> { LastTag.ParseJToken(tagToken) }; } } else { a.TopTags = Enumerable.Empty <LastTag>(); } a.ListenerCount = token.Value <int?>("listeners"); a.Mbid = token.Value <string>("mbid"); a.Name = token.Value <string>("name"); var playCountStr = token.Value <string>("playcount"); int playCount; if (int.TryParse(playCountStr, out playCount)) { a.PlayCount = playCount; } var images = token.SelectToken("image"); if (images != null) { var imageCollection = LastImageSet.ParseJToken(images); a.Images = imageCollection; } a.Url = new Uri(token.Value <string>("url"), UriKind.Absolute); var dateString = token.Value <string>("releasedate"); DateTimeOffset releaseDate; if (DateTimeOffset.TryParse(dateString, out releaseDate)) { a.ReleaseDateUtc = releaseDate; } var wikiToken = token["wiki"]; if (wikiToken != null) { a.Wiki = LastWiki.ParseJToken(wikiToken); } return(a); }
internal static LastArtist ParseJToken(JToken token) { var a = new LastArtist(); a.Id = token.Value <string>("id"); a.Name = token.Value <string>("name"); a.Mbid = token.Value <string>("mbid"); var url = token.Value <string>("url"); var playCountStr = token.Value <string>("playcount"); int playCount; if (int.TryParse(playCountStr, out playCount)) { a.PlayCount = playCount; } var matchStr = token.Value <string>("match"); double match; if (double.TryParse(matchStr, out match)) { a.MatchSimilar = match; } // for some stupid reason the api returns the url without http in the get similar method, WHY? if (!url.StartsWith("http")) { url = "http://" + url; } a.Url = new Uri(url, UriKind.Absolute); a.OnTour = Convert.ToBoolean(token.Value <int>("ontour")); var statsToken = token.SelectToken("stats"); if (statsToken != null) { a.Stats = LastStats.ParseJToken(statsToken); } var bioToken = token.SelectToken("bio"); if (bioToken != null) { a.Bio = LastWiki.ParseJToken(bioToken); } var tagsToken = token.SelectToken("tags"); if (tagsToken != null) { var tagToken = tagsToken.SelectToken("tag"); if (tagToken != null) { a.Tags = tagToken.Type == JTokenType.Array ? tagToken.Children().Select(token1 => LastTag.ParseJToken(token1)) : new List <LastTag> { LastTag.ParseJToken(tagToken) }; } } var images = token.SelectToken("image"); if (images != null && images.HasValues) { var imageCollection = LastImageSet.ParseJToken(images); a.MainImage = imageCollection; } var similarToken = token.SelectToken("similar"); if (similarToken != null) { a.Similar = new List <LastArtist>(); var similarArtists = similarToken.SelectToken("artist"); if (similarArtists != null && similarArtists.Children().Any()) { // array notation isn't used on the api when only one object is available if (similarArtists.Type != JTokenType.Array) { var item = ParseJToken(similarArtists); a.Similar.Add(item); } else { var items = similarArtists.Children().Select(ParseJToken); a.Similar.AddRange(items); } } } return(a); }