using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; namespace I2.Loc.SimpleJSON { public class JSONClass : JSONNode, IEnumerable { public override JSONNode this[string aKey] { get { if (this.m_Dict.ContainsKey(aKey)) { return this.m_Dict[aKey]; } return new JSONLazyCreator(this, aKey); } set { if (this.m_Dict.ContainsKey(aKey)) { this.m_Dict[aKey] = value; } else { this.m_Dict.Add(aKey, value); } } } public override JSONNode this[int aIndex] { get { if (aIndex < 0 || aIndex >= this.m_Dict.Count) { return null; } return this.m_Dict.ElementAt(aIndex).Value; } set { if (aIndex < 0 || aIndex >= this.m_Dict.Count) { return; } string key = this.m_Dict.ElementAt(aIndex).Key; this.m_Dict[key] = value; } } public override int Count { get { return this.m_Dict.Count; } } public override void Add(string aKey, JSONNode aItem) { if (!string.IsNullOrEmpty(aKey)) { if (this.m_Dict.ContainsKey(aKey)) { this.m_Dict[aKey] = aItem; } else { this.m_Dict.Add(aKey, aItem); } } else { this.m_Dict.Add(Guid.NewGuid().ToString(), aItem); } } public override JSONNode Remove(string aKey) { if (!this.m_Dict.ContainsKey(aKey)) { return null; } JSONNode result = this.m_Dict[aKey]; this.m_Dict.Remove(aKey); return result; } public override JSONNode Remove(int aIndex) { if (aIndex < 0 || aIndex >= this.m_Dict.Count) { return null; } KeyValuePair keyValuePair = this.m_Dict.ElementAt(aIndex); this.m_Dict.Remove(keyValuePair.Key); return keyValuePair.Value; } public override JSONNode Remove(JSONNode aNode) { JSONNode result; try { KeyValuePair keyValuePair = (from k in this.m_Dict where k.Value == aNode select k).First>(); this.m_Dict.Remove(keyValuePair.Key); result = aNode; } catch { result = null; } return result; } public override IEnumerable Childs { get { foreach (KeyValuePair N in this.m_Dict) { yield return N.Value; } yield break; } } public IEnumerator GetEnumerator() { foreach (KeyValuePair N in this.m_Dict) { yield return N; } yield break; } public override string ToString() { string text = "{"; foreach (KeyValuePair keyValuePair in this.m_Dict) { if (text.Length > 2) { text += ", "; } string text2 = text; text = string.Concat(new string[] { text2, "\"", JSONNode.Escape(keyValuePair.Key), "\":", keyValuePair.Value.ToString() }); } text += "}"; return text; } public override string ToString(string aPrefix) { string text = "{ "; foreach (KeyValuePair keyValuePair in this.m_Dict) { if (text.Length > 3) { text += ", "; } text = text + "\n" + aPrefix + " "; string text2 = text; text = string.Concat(new string[] { text2, "\"", JSONNode.Escape(keyValuePair.Key), "\" : ", keyValuePair.Value.ToString(aPrefix + " ") }); } text = text + "\n" + aPrefix + "}"; return text; } public override void Serialize(BinaryWriter aWriter) { aWriter.Write(2); aWriter.Write(this.m_Dict.Count); foreach (string text in this.m_Dict.Keys) { aWriter.Write(text); this.m_Dict[text].Serialize(aWriter); } } private Dictionary m_Dict = new Dictionary(StringComparer.Ordinal); } }