public static JsonPairs LookupToJson <TValue>(this Lookup <TValue> lookup) where TValue : IJsonSerializable
        {
            var jps = new JsonPairs();

            lookup.LookupToJson(jps);
            return(jps);
        }
        public static void LookupFromJsonValues(this Lookup <object> lookup, JsonPairs jps, Predicate <Tuple <string, JsonNode> > canAdd = null)
        {
            foreach (var kvp in jps.KeyValues)
            {
                var key = kvp.Key;
                var jv  = kvp.Value;
                if (canAdd?.Invoke(new Tuple <string, JsonNode>(key, jv)) == false)
                {
                    continue;
                }
                object val;
                switch (jv)
                {
                case JsonValue <Numeric> jvn:
                    if (int.TryParse(jvn.Value.Value, out var ival))
                    {
                        val = ival;
                    }
                    else
                    {
                        val = jvn.ToString();
                    }
                    break;

                case JsonValue <bool> jvb:
                    val = jvb.Value;
                    break;

                default:
                    val = jv.ToString();
                    break;
                }
                lookup.Add(key, val);
            }
        }
        public static void LookupToJsonValues(this Lookup <object> lookup, JsonPairs jps)
        {
            foreach (var kvp in lookup)
            {
                var      key   = kvp.Key;
                var      value = kvp.Value;
                JsonNode jv;
                switch (value)
                {
                case int ival:
                    jv = new JsonValue <Numeric> {
                        Value = new Numeric(ival.ToString())
                    };
                    break;

                case bool bval:
                    jv = new JsonValue <bool> {
                        Value = bval
                    };
                    break;

                default:
                    jv = new JsonValue <string> {
                        Value = value.ToString()
                    };
                    break;
                }
                jps.KeyValues.Add(key, jv);
            }
        }
        public static JsonPairs LookupToJsonValues(this Lookup <object> lookup)
        {
            var jps = new JsonPairs();

            lookup.LookupToJsonValues(jps);
            return(jps);
        }
 public static void LookupFromJson <TValue>(this Lookup <TValue> lookup, JsonPairs jps, Func <JsonNode, TValue> createValue)
 {
     foreach (var kvp in jps.KeyValues)
     {
         var key = kvp.Key;
         var jn  = kvp.Value;
         var val = createValue(jn);
         lookup.Add(key, val);
     }
 }
 public static void LookupToJson <TValue>(this Lookup <TValue> lookup, JsonPairs jps) where TValue : IJsonSerializable
 {
     foreach (var kvp in lookup)
     {
         var key   = kvp.Key;
         var value = kvp.Value;
         var cjn   = value.ToJson();
         jps.KeyValues.Add(key, cjn);
     }
 }
Example #7
0
 public static void SetValue(this JsonPairs pairs, string key, string value)
 {
     pairs.KeyValues[key] = new JsonValue <string> {
         Value = value
     };
 }
Example #8
0
        public static MediaInfo TryParse(JsonPairs mediaInfo)
        {
            if (!mediaInfo.TryGetValue("id", out string id))
            {
                return(null);
            }
            var mi = new MediaInfo {
                Id = id
            };

            if (mediaInfo.TryGetValue("title", out string title))
            {
                mi.Title = title;
            }
            if (mediaInfo.TryGetValue("type", out string category))
            {
                mi.Category = category;
            }
            if (mediaInfo.TryGetValue("date", out string date))
            {
                mi.DateStr = date;
            }
            if (mediaInfo.TryGetValue("role", out string role))
            {
                mi.Role = role;
            }
            if (mediaInfo.TryGetValue("director", out string director))
            {
                mi.Director = director;
            }
            if (mediaInfo.TryGetValue("producer", out string producer))
            {
                mi.Producer = producer;
            }
            if (mediaInfo.TryGetValue("playwright", out string playwright))
            {
                mi.Playwright = playwright;
            }
            if (mediaInfo.TryGetValue("adaptedFrom", out string adaptedFrom))
            {
                mi.AdaptedFrom = adaptedFrom;
            }
            if (mediaInfo.TryGetValue("remarks", out string remarks))
            {
                mi.Remarks = remarks;
            }
            if (mediaInfo.TryGetValue("link", out string link))
            {
                mi.ExternalLink = link;
            }
            if (mediaInfo.TryGetNode("songs", out JsonArray songs))
            {
                foreach (var p in songs.Items.OfType <JsonPairs>().Where(x => x.KeyValues.Count == 1))
                {
                    var pair = p.KeyValues.First();
                    if (JsonPairs.TryGetValue(pair, out string t))
                    {
                        var song = new Tuple <string, string>(pair.Key, t);
                        mi.Songs.Add(song);
                    }
                }
            }
            if (mediaInfo.TryGetNode("sources", out JsonArray sources))
            {
                // Ignoreing invalid ones
                foreach (var item in sources.Items.OfType <JsonPairs>())
                {
                    if (item.TryGetValue("name", out string name))
                    {
                        var source = new MediaSource
                        {
                            Name = name
                        };
                        if (item.TryGetValue("target", out string target))
                        {
                            source.Target = target;
                        }
                        if (item.TryGetNode("playlist", out JsonArray playlist))
                        {
                            foreach (var p in playlist.Items.OfType <JsonPairs>().Where(x => x.KeyValues.Count == 1))
                            {
                                var pair = p.KeyValues.First();
                                if (JsonPairs.TryGetValue(pair, out string t))
                                {
                                    var track = new Tuple <string, string>(pair.Key, t);
                                    source.Playlist.Add(track);
                                }
                            }
                        }
                        mi.Sources.Add(source);
                    }
                }
            }
            if (mediaInfo.TryGetValue("sourcesRemarks", out string sourcesRemarks))
            {
                mi.SourcesRemarks = sourcesRemarks;
            }
            return(mi);
        }
Example #9
0
        public JsonPairs WriteToJson()
        {
            var mikvp = new JsonPairs();

            if (Id != null)
            {
                mikvp.SetValue("id", Id);
            }
            if (Title != null)
            {
                mikvp.SetValue("title", Title);
            }
            if (Category != null)
            {
                mikvp.SetValue("type", Category);
            }
            if (DateStr != null)
            {
                mikvp.SetValue("date", DateStr);
            }
            if (Role != null)
            {
                mikvp.SetValue("role", Role);
            }
            if (Director != null)
            {
                mikvp.SetValue("director", Director);
            }
            if (Producer != null)
            {
                mikvp.SetValue("producer", Producer);
            }
            if (Playwright != null)
            {
                mikvp.SetValue("playwright", Playwright);
            }
            if (AdaptedFrom != null)
            {
                mikvp.SetValue("adaptedFrom", AdaptedFrom);
            }
            if (Remarks != null)
            {
                mikvp.SetValue("remarks", Remarks);
            }
            if (ExternalLink != null)
            {
                mikvp.SetValue("link", ExternalLink);
            }

            if (Songs.Count > 0)
            {
                var songs = new JsonArray();
                foreach (var song in Songs)
                {
                    var key = song.Item1;
                    var val = song.Item2;
                    var kvp = new JsonPairs();
                    kvp.SetValue(key, val);
                    songs.Items.Add(kvp);
                }
                mikvp.KeyValues["songs"] = songs;
            }

            if (Sources.Count > 0)
            {
                var sources = new JsonArray();
                foreach (var source in Sources)
                {
                    var srckvp = new JsonPairs();
                    srckvp.SetValue("name", source.Name);
                    if (source.Target != null)
                    {
                        srckvp.SetValue("target", source.Target);
                    }

                    if (source.Playlist.Count > 0)
                    {
                        var playlist = new JsonArray();
                        foreach (var t in source.Playlist)
                        {
                            var plkvp = new JsonPairs();
                            plkvp.SetValue(t.Item1, t.Item2);
                            playlist.Items.Add(plkvp);
                        }
                        srckvp.KeyValues["playlist"] = playlist;
                    }

                    sources.Items.Add(srckvp);
                }
                mikvp.KeyValues["sources"] = sources;
            }

            if (SourcesRemarks != null)
            {
                mikvp.SetValue("sourcesRemarks", SourcesRemarks);
            }

            return(mikvp);
        }