Esempio n. 1
0
        public static IEnumerable <SearchResult> GetTrackResults(JObject searchResults)
        {
            if (searchResults["results"]?["TRACK"]?["data"] == null)
            {
                Helpers.RedMessage("No track results found");
                yield break;
            }

            JEnumerable <JObject> tracks = searchResults["results"]["TRACK"]["data"].Children <JObject>();

            if (!tracks.Any())
            {
                Helpers.RedMessage("No track results found");
                yield break;
            }

            foreach (JObject track in tracks)
            {
                var id         = track?["SNG_ID"].Value <string>();
                var title      = track?["SNG_TITLE"].Value <string>();
                var artistName = track?["ART_NAME"].Value <string>();
                var albumName  = track?["ALB_TITLE"].Value <string>();

                yield return(new SearchResult
                {
                    Id = id,
                    OutputString = $"[Artist: {artistName}] [Track: {title}] [Album: {albumName}]",
                    Json = track
                });
            }
        }
Esempio n. 2
0
        private static List <object> toObjectData(JToken token, JEnumerable <JObject> included)
        {
            var list       = new List <object>();
            var ignoreCase = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance;

            // TODO: add deCamelCase
            var types = GetTypesWith <Type>(true);

            foreach (var data in token["data"].Children <JObject>())
            {
                var itemType = (string)data["type"];
                var type     = types.First(x => ((Type)x.GetCustomAttribute(typeof(Type))).Value == itemType);

                var item = Activator.CreateInstance(type);
                list.Add(item);

                // set id
                type.GetProperty("id", ignoreCase).SetValue(item, Convert.ChangeType(data["id"], type.GetProperty("id", ignoreCase).PropertyType), null);

                var ndata = data;
                // there is probably a better way todo this
                if (included.Any(x => (string)x["type"] == (string)data["type"] && (string)x["id"] == (string)data["id"]))
                {
                    // get data from included
                    ndata = included.First(x => (string)x["type"] == (string)data["type"] && (string)x["id"] == (string)data["id"]);
                }

                // set attributes
                if (ndata["attributes"] as JObject != null)
                {
                    foreach (var attr in ((JObject)ndata["attributes"]).Properties())
                    {
                        type.GetProperty(attr.Name, ignoreCase).SetValue(item, Convert.ChangeType(attr.Value, type.GetProperty(attr.Name, ignoreCase).PropertyType), null);
                    }
                }

                // set relationships
                if (ndata["relationships"] as JObject != null)
                {
                    foreach (var rela in ((JObject)ndata["relationships"]).Properties())
                    {
                        // will this work ?
                        // just hopes it's a ?????<T> container
                        type.GetProperty(rela.Name, ignoreCase).SetValue(item, ConvertList(toObjectData(rela.Value, included), type.GetProperty(rela.Name, ignoreCase).PropertyType), null);
                    }
                }
            }

            return(list);
        }
Esempio n. 3
0
 public JToken FindJObject(JEnumerable <JToken> children, string key)
 {
     foreach (var child in children)
     {
         if (child.Path.Contains(key))
         {
             return(child);
         }
         JEnumerable <JToken> descendants = child.Children();
         if (descendants.Any())
         {
             JToken found = FindJObject(descendants, key);
             if (found != null)
             {
                 return(found);
             }
         }
     }
     return(null);
 }