JSONClass.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace I2.Loc.SimpleJSON
  7. {
  8. public class JSONClass : JSONNode, IEnumerable
  9. {
  10. public override JSONNode this[string aKey]
  11. {
  12. get
  13. {
  14. if (this.m_Dict.ContainsKey(aKey))
  15. {
  16. return this.m_Dict[aKey];
  17. }
  18. return new JSONLazyCreator(this, aKey);
  19. }
  20. set
  21. {
  22. if (this.m_Dict.ContainsKey(aKey))
  23. {
  24. this.m_Dict[aKey] = value;
  25. }
  26. else
  27. {
  28. this.m_Dict.Add(aKey, value);
  29. }
  30. }
  31. }
  32. public override JSONNode this[int aIndex]
  33. {
  34. get
  35. {
  36. if (aIndex < 0 || aIndex >= this.m_Dict.Count)
  37. {
  38. return null;
  39. }
  40. return this.m_Dict.ElementAt(aIndex).Value;
  41. }
  42. set
  43. {
  44. if (aIndex < 0 || aIndex >= this.m_Dict.Count)
  45. {
  46. return;
  47. }
  48. string key = this.m_Dict.ElementAt(aIndex).Key;
  49. this.m_Dict[key] = value;
  50. }
  51. }
  52. public override int Count
  53. {
  54. get
  55. {
  56. return this.m_Dict.Count;
  57. }
  58. }
  59. public override void Add(string aKey, JSONNode aItem)
  60. {
  61. if (!string.IsNullOrEmpty(aKey))
  62. {
  63. if (this.m_Dict.ContainsKey(aKey))
  64. {
  65. this.m_Dict[aKey] = aItem;
  66. }
  67. else
  68. {
  69. this.m_Dict.Add(aKey, aItem);
  70. }
  71. }
  72. else
  73. {
  74. this.m_Dict.Add(Guid.NewGuid().ToString(), aItem);
  75. }
  76. }
  77. public override JSONNode Remove(string aKey)
  78. {
  79. if (!this.m_Dict.ContainsKey(aKey))
  80. {
  81. return null;
  82. }
  83. JSONNode result = this.m_Dict[aKey];
  84. this.m_Dict.Remove(aKey);
  85. return result;
  86. }
  87. public override JSONNode Remove(int aIndex)
  88. {
  89. if (aIndex < 0 || aIndex >= this.m_Dict.Count)
  90. {
  91. return null;
  92. }
  93. KeyValuePair<string, JSONNode> keyValuePair = this.m_Dict.ElementAt(aIndex);
  94. this.m_Dict.Remove(keyValuePair.Key);
  95. return keyValuePair.Value;
  96. }
  97. public override JSONNode Remove(JSONNode aNode)
  98. {
  99. JSONNode result;
  100. try
  101. {
  102. KeyValuePair<string, JSONNode> keyValuePair = (from k in this.m_Dict
  103. where k.Value == aNode
  104. select k).First<KeyValuePair<string, JSONNode>>();
  105. this.m_Dict.Remove(keyValuePair.Key);
  106. result = aNode;
  107. }
  108. catch
  109. {
  110. result = null;
  111. }
  112. return result;
  113. }
  114. public override IEnumerable<JSONNode> Childs
  115. {
  116. get
  117. {
  118. foreach (KeyValuePair<string, JSONNode> N in this.m_Dict)
  119. {
  120. yield return N.Value;
  121. }
  122. yield break;
  123. }
  124. }
  125. public IEnumerator GetEnumerator()
  126. {
  127. foreach (KeyValuePair<string, JSONNode> N in this.m_Dict)
  128. {
  129. yield return N;
  130. }
  131. yield break;
  132. }
  133. public override string ToString()
  134. {
  135. string text = "{";
  136. foreach (KeyValuePair<string, JSONNode> keyValuePair in this.m_Dict)
  137. {
  138. if (text.Length > 2)
  139. {
  140. text += ", ";
  141. }
  142. string text2 = text;
  143. text = string.Concat(new string[]
  144. {
  145. text2,
  146. "\"",
  147. JSONNode.Escape(keyValuePair.Key),
  148. "\":",
  149. keyValuePair.Value.ToString()
  150. });
  151. }
  152. text += "}";
  153. return text;
  154. }
  155. public override string ToString(string aPrefix)
  156. {
  157. string text = "{ ";
  158. foreach (KeyValuePair<string, JSONNode> keyValuePair in this.m_Dict)
  159. {
  160. if (text.Length > 3)
  161. {
  162. text += ", ";
  163. }
  164. text = text + "\n" + aPrefix + " ";
  165. string text2 = text;
  166. text = string.Concat(new string[]
  167. {
  168. text2,
  169. "\"",
  170. JSONNode.Escape(keyValuePair.Key),
  171. "\" : ",
  172. keyValuePair.Value.ToString(aPrefix + " ")
  173. });
  174. }
  175. text = text + "\n" + aPrefix + "}";
  176. return text;
  177. }
  178. public override void Serialize(BinaryWriter aWriter)
  179. {
  180. aWriter.Write(2);
  181. aWriter.Write(this.m_Dict.Count);
  182. foreach (string text in this.m_Dict.Keys)
  183. {
  184. aWriter.Write(text);
  185. this.m_Dict[text].Serialize(aWriter);
  186. }
  187. }
  188. private Dictionary<string, JSONNode> m_Dict = new Dictionary<string, JSONNode>(StringComparer.Ordinal);
  189. }
  190. }