You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

111 lines
2.2 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Boomlagoon.JSON
  6. {
  7. public class JSONArray : IEnumerable<JSONValue>, IEnumerable
  8. {
  9. public JSONArray()
  10. {
  11. }
  12. public JSONArray(JSONArray array)
  13. {
  14. this.values = new List<JSONValue>();
  15. foreach (JSONValue value in array.values)
  16. {
  17. this.values.Add(new JSONValue(value));
  18. }
  19. }
  20. public void Add(JSONValue value)
  21. {
  22. this.values.Add(value);
  23. }
  24. public JSONValue this[int index]
  25. {
  26. get
  27. {
  28. return this.values[index];
  29. }
  30. set
  31. {
  32. this.values[index] = value;
  33. }
  34. }
  35. public int Length
  36. {
  37. get
  38. {
  39. return this.values.Count;
  40. }
  41. }
  42. public override string ToString()
  43. {
  44. StringBuilder stringBuilder = new StringBuilder();
  45. stringBuilder.Append('[');
  46. foreach (JSONValue jsonvalue in this.values)
  47. {
  48. stringBuilder.Append(jsonvalue.ToString());
  49. stringBuilder.Append(',');
  50. }
  51. if (this.values.Count > 0)
  52. {
  53. stringBuilder.Remove(stringBuilder.Length - 1, 1);
  54. }
  55. stringBuilder.Append(']');
  56. return stringBuilder.ToString();
  57. }
  58. public IEnumerator<JSONValue> GetEnumerator()
  59. {
  60. return this.values.GetEnumerator();
  61. }
  62. IEnumerator IEnumerable.GetEnumerator()
  63. {
  64. return this.values.GetEnumerator();
  65. }
  66. public static JSONArray Parse(string jsonString)
  67. {
  68. JSONObject jsonobject = JSONObject.Parse("{ \"array\" :" + jsonString + "}");
  69. return (jsonobject != null) ? jsonobject.GetValue("array").Array : null;
  70. }
  71. public void Clear()
  72. {
  73. this.values.Clear();
  74. }
  75. public void Remove(int index)
  76. {
  77. if (index >= 0 && index < this.values.Count)
  78. {
  79. this.values.RemoveAt(index);
  80. }
  81. else
  82. {
  83. JSONLogger.Error(string.Format("index out of range: {0} (Expected 0 <= index < {1})", index, this.values.Count));
  84. }
  85. }
  86. public static JSONArray operator +(JSONArray lhs, JSONArray rhs)
  87. {
  88. JSONArray jsonarray = new JSONArray(lhs);
  89. foreach (JSONValue value in rhs.values)
  90. {
  91. jsonarray.Add(value);
  92. }
  93. return jsonarray;
  94. }
  95. private readonly List<JSONValue> values = new List<JSONValue>();
  96. }
  97. }