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