JSONArray.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace I2.Loc.SimpleJSON
  6. {
  7. public class JSONArray : JSONNode, IEnumerable
  8. {
  9. public override JSONNode this[int aIndex]
  10. {
  11. get
  12. {
  13. if (aIndex < 0 || aIndex >= this.m_List.Count)
  14. {
  15. return new JSONLazyCreator(this);
  16. }
  17. return this.m_List[aIndex];
  18. }
  19. set
  20. {
  21. if (aIndex < 0 || aIndex >= this.m_List.Count)
  22. {
  23. this.m_List.Add(value);
  24. }
  25. else
  26. {
  27. this.m_List[aIndex] = value;
  28. }
  29. }
  30. }
  31. public override JSONNode this[string aKey]
  32. {
  33. get
  34. {
  35. return new JSONLazyCreator(this);
  36. }
  37. set
  38. {
  39. this.m_List.Add(value);
  40. }
  41. }
  42. public override int Count
  43. {
  44. get
  45. {
  46. return this.m_List.Count;
  47. }
  48. }
  49. public override void Add(string aKey, JSONNode aItem)
  50. {
  51. this.m_List.Add(aItem);
  52. }
  53. public override JSONNode Remove(int aIndex)
  54. {
  55. if (aIndex < 0 || aIndex >= this.m_List.Count)
  56. {
  57. return null;
  58. }
  59. JSONNode result = this.m_List[aIndex];
  60. this.m_List.RemoveAt(aIndex);
  61. return result;
  62. }
  63. public override JSONNode Remove(JSONNode aNode)
  64. {
  65. this.m_List.Remove(aNode);
  66. return aNode;
  67. }
  68. public override IEnumerable<JSONNode> Childs
  69. {
  70. get
  71. {
  72. foreach (JSONNode N in this.m_List)
  73. {
  74. yield return N;
  75. }
  76. yield break;
  77. }
  78. }
  79. public IEnumerator GetEnumerator()
  80. {
  81. foreach (JSONNode N in this.m_List)
  82. {
  83. yield return N;
  84. }
  85. yield break;
  86. }
  87. public override string ToString()
  88. {
  89. string text = "[ ";
  90. foreach (JSONNode jsonnode in this.m_List)
  91. {
  92. if (text.Length > 2)
  93. {
  94. text += ", ";
  95. }
  96. text += jsonnode.ToString();
  97. }
  98. text += " ]";
  99. return text;
  100. }
  101. public override string ToString(string aPrefix)
  102. {
  103. string text = "[ ";
  104. foreach (JSONNode jsonnode in this.m_List)
  105. {
  106. if (text.Length > 3)
  107. {
  108. text += ", ";
  109. }
  110. text = text + "\n" + aPrefix + " ";
  111. text += jsonnode.ToString(aPrefix + " ");
  112. }
  113. text = text + "\n" + aPrefix + "]";
  114. return text;
  115. }
  116. public override void Serialize(BinaryWriter aWriter)
  117. {
  118. aWriter.Write(1);
  119. aWriter.Write(this.m_List.Count);
  120. for (int i = 0; i < this.m_List.Count; i++)
  121. {
  122. this.m_List[i].Serialize(aWriter);
  123. }
  124. }
  125. private List<JSONNode> m_List = new List<JSONNode>();
  126. }
  127. }