123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Topten.JsonKit
- {
-
-
-
- public static class IDictionaryExtensions
- {
-
-
-
-
-
-
-
-
- public static bool WalkPath(
- this IDictionary<string, object> This,
- string Path,
- bool create,
- Func<IDictionary<string,object>,string, bool> leafCallback
- )
- {
-
- var parts = Path.Split('.');
- for (int i = 0; i < parts.Length-1; i++)
- {
- object val;
- if (!This.TryGetValue(parts[i], out val))
- {
- if (!create)
- return false;
- val = new Dictionary<string, object>();
- This[parts[i]] = val;
- }
- This = (IDictionary<string,object>)val;
- }
-
- return leafCallback(This, parts[parts.Length-1]);
- }
-
-
-
-
-
-
- public static bool PathExists(this IDictionary<string, object> This, string Path)
- {
- return This.WalkPath(Path, false, (dict, key) => dict.ContainsKey(key));
- }
-
-
-
-
-
-
-
-
- public static object GetPath(this IDictionary<string, object> This, Type type, string Path, object def)
- {
- This.WalkPath(Path, false, (dict, key) =>
- {
- object val;
- if (dict.TryGetValue(key, out val))
- {
- if (val == null)
- def = val;
- else if (type.IsAssignableFrom(val.GetType()))
- def = val;
- else
- def = Json.Reparse(type, val);
- }
- return true;
- });
- return def;
- }
-
-
-
-
-
-
-
- public static T GetObjectAtPath<T>(this IDictionary<string, object> This, string Path) where T:class,new()
- {
- T retVal = null;
- This.WalkPath(Path, true, (dict, key) =>
- {
- object val;
- dict.TryGetValue(key, out val);
- retVal = val as T;
- if (retVal == null)
- {
- retVal = val == null ? new T() : Json.Reparse<T>(val);
- dict[key] = retVal;
- }
- return true;
- });
- return retVal;
- }
-
-
-
-
-
-
-
-
- public static T GetPath<T>(this IDictionary<string, object> This, string Path, T def = default(T))
- {
- return (T)This.GetPath(typeof(T), Path, def);
- }
-
-
-
-
-
-
- public static void SetPath(this IDictionary<string, object> This, string Path, object value)
- {
- This.WalkPath(Path, true, (dict, key) => { dict[key] = value; return true; });
- }
- }
- }
|