123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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<string, JSONNode> 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<string, JSONNode> keyValuePair = (from k in this.m_Dict
- where k.Value == aNode
- select k).First<KeyValuePair<string, JSONNode>>();
- this.m_Dict.Remove(keyValuePair.Key);
- result = aNode;
- }
- catch
- {
- result = null;
- }
- return result;
- }
- public override IEnumerable<JSONNode> Childs
- {
- get
- {
- foreach (KeyValuePair<string, JSONNode> N in this.m_Dict)
- {
- yield return N.Value;
- }
- yield break;
- }
- }
- public IEnumerator GetEnumerator()
- {
- foreach (KeyValuePair<string, JSONNode> N in this.m_Dict)
- {
- yield return N;
- }
- yield break;
- }
- public override string ToString()
- {
- string text = "{";
- foreach (KeyValuePair<string, JSONNode> 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<string, JSONNode> 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<string, JSONNode> m_Dict = new Dictionary<string, JSONNode>(StringComparer.Ordinal);
- }
- }
|