using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Dynamic; using System.Linq; using System.Text; using System.Web; using System.Web.Script.Serialization; namespace com.rwmanila { public class apiExt : api { // Fields private JavaScriptSerializer jss; // Constructor public apiExt(string userId = "") : base(ctorUserModify(userId), ctorKeyModify(userId)) { this.userId = (string.IsNullOrWhiteSpace(userId) ? "public" : userId.ToLowerInvariant()); // Initialize jss jss = new JavaScriptSerializer(); jss.RegisterConverters(new[] { new DynamicJsonConverter() }); } // Methods private static Dictionary getKeys() { // Load Keys (preferably from a secure database, but in-memory would work too) Dictionary r = new Dictionary(); r.Add("test.user", "c9d525ea08f16a40082d069d30d4a7e92549e4b4"); return r; } private static string ctorUserModify(string userId) { userId = (string.IsNullOrWhiteSpace(userId) ? api.publicUserId : userId.ToLowerInvariant()); // Check if key exists in store Dictionary keyStore = getKeys(); if (keyStore.ContainsKey(userId)) return userId; else // if not, return public return api.publicUserId; } private static string ctorKeyModify(string userId) { userId = (string.IsNullOrWhiteSpace(userId) ? api.publicUserId : userId.ToLowerInvariant()); // Check if key exists in store Dictionary keyStore = getKeys(); if (keyStore.ContainsKey(userId)) return keyStore[userId]; else // if not, return public return api.publicAPIKey; } public dynamic send(string commandGroup, string command, Dictionary parameters) { try { return jss.Deserialize( this.sendJSON(commandGroup, command, parameters).Replace('.', '_'), typeof(object)); } catch (Exception ex) { return ex; } } #region Helper Parser Classes private sealed class DynamicJsonConverter : JavaScriptConverter { public override object Deserialize(IDictionary dictionary, Type type, JavaScriptSerializer serializer) { if (dictionary == null) throw new ArgumentNullException("dictionary"); return type == typeof(object) ? new DynamicJsonObject(dictionary) : null; } public override IDictionary Serialize(object obj, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IEnumerable SupportedTypes { get { return new ReadOnlyCollection(new List(new[] { typeof(object) })); } } private sealed class DynamicJsonObject : DynamicObject { private readonly IDictionary _dictionary; public DynamicJsonObject(IDictionary dictionary) { if (dictionary == null) throw new ArgumentNullException("dictionary"); _dictionary = dictionary; } public override string ToString() { var sb = new StringBuilder("{"); ToString(sb); return sb.ToString(); } private void ToString(StringBuilder sb) { var firstInDictionary = true; foreach (var pair in _dictionary) { if (!firstInDictionary) sb.Append(","); firstInDictionary = false; var value = pair.Value; var name = pair.Key; if (value is string) { sb.AppendFormat("{0}:\"{1}\"", name, value); } else if (value is IDictionary) { new DynamicJsonObject((IDictionary)value).ToString(sb); } else if (value is ArrayList) { sb.Append(name + ":["); var firstInArray = true; foreach (var arrayValue in (ArrayList)value) { if (!firstInArray) sb.Append(","); firstInArray = false; if (arrayValue is IDictionary) new DynamicJsonObject((IDictionary)arrayValue).ToString(sb); else if (arrayValue is string) sb.AppendFormat("\"{0}\"", arrayValue); else sb.AppendFormat("{0}", arrayValue); } sb.Append("]"); } else { sb.AppendFormat("{0}:{1}", name, value); } } sb.Append("}"); } public override bool TryGetMember(GetMemberBinder binder, out object result) { if (!_dictionary.TryGetValue(binder.Name, out result)) { // return null to avoid exception. caller can check for null this way... result = null; return true; } var dictionary = result as IDictionary; if (dictionary != null) { result = new DynamicJsonObject(dictionary); return true; } var arrayList = result as ArrayList; if (arrayList != null && arrayList.Count > 0) { if (arrayList[0] is IDictionary) result = new List(arrayList.Cast>().Select(x => new DynamicJsonObject(x))); else result = new List(arrayList.Cast()); } return true; } } #endregion } } }