#if net4 using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; namespace fastJSON { internal class DynamicJson : DynamicObject { private IDictionary _dictionary { get; set; } private List _list { get; set; } public DynamicJson(string json) { var parse = fastJSON.JSON.Parse(json); if (parse is IDictionary) _dictionary = (IDictionary)parse; else _list = (List)parse; } private DynamicJson(object dictionary) { if (dictionary is IDictionary) _dictionary = (IDictionary)dictionary; } public override IEnumerable GetDynamicMemberNames() { return _dictionary.Keys.ToList(); } public override bool TryGetIndex(GetIndexBinder binder, Object[] indexes, out Object result) { var index = indexes[0]; if (index is int) { result = _list[(int) index]; } else { result = _dictionary[(string) index]; } if (result is IDictionary) result = new DynamicJson(result as IDictionary); return true; } public override bool TryGetMember(GetMemberBinder binder, out object result) { if (_dictionary.TryGetValue(binder.Name, out result) == false) if (_dictionary.TryGetValue(binder.Name.ToLower(), out result) == false) return false;// throw new Exception("property not found " + binder.Name); if (result is IDictionary) { result = new DynamicJson(result as IDictionary); } else if (result is List) { List list = new List(); foreach (object item in (List)result) { if (item is IDictionary) list.Add(new DynamicJson(item as IDictionary)); else list.Add(item); } result = list; } return _dictionary.ContainsKey(binder.Name); } } } #endif