JSONArray.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. /**
  6. * An array is an ordered collection of values.
  7. * An array begins with [ (left bracket) and ends with ] (right bracket).
  8. * Values are separated by , (comma).
  9. * @author zeh
  10. */
  11. public class JSONArray:JSONValue {
  12. // Constants
  13. private const char CHAR_START = '[';
  14. private const char CHAR_END = ']';
  15. private const char CHAR_ITEM_SEPARATOR = ',';
  16. // Enums
  17. private enum ParsingState {
  18. Start,
  19. BeforeValue,
  20. AfterValue,
  21. End,
  22. }
  23. // Properties
  24. private IList value;
  25. // ================================================================================================================
  26. // CONSTRUCTOR ----------------------------------------------------------------------------------------------------
  27. public JSONArray() : base() {
  28. }
  29. public JSONArray(object value) : base(value) {
  30. }
  31. public JSONArray(StringBuilder input, int position) : base(input, position) {
  32. }
  33. // ================================================================================================================
  34. // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
  35. public static bool matchesInput(StringBuilder input, int position) {
  36. return JSONParser.compareStringValue(CHAR_START.ToString(), input, position + JSONParser.countWhitespaceCharacters(input, position));
  37. }
  38. public static bool matchesValue(object value) {
  39. return value is IList;
  40. }
  41. public override object getValue() {
  42. return value;
  43. }
  44. public override void setValue(object newValue) {
  45. value = (IList)newValue;
  46. }
  47. // ================================================================================================================
  48. // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
  49. protected override void parseValueFromInput() {
  50. ParsingState parsingState = ParsingState.Start;
  51. char c;
  52. int i = 0;
  53. JSONValue valueObject;
  54. value = new List<object>();
  55. while (i < input.Length && parsingState != ParsingState.End) {
  56. c = input.ToString(inputStart + i, 1)[0]; // TODO: is this efficient?
  57. switch (parsingState) {
  58. case ParsingState.Start:
  59. if (c == CHAR_START) {
  60. // Starting array
  61. parsingState = ParsingState.BeforeValue;
  62. } else {
  63. Debug.LogError("Invalid character \"" + c + "\" when expecting array start");
  64. }
  65. break;
  66. case ParsingState.BeforeValue:
  67. if (!JSONParser.isWhitespace(c)) {
  68. valueObject = JSONParser.parse(input, inputStart + i);
  69. i += valueObject.getInputLength() - 1;
  70. value.Add(valueObject.getValue());
  71. parsingState = ParsingState.AfterValue;
  72. }
  73. break;
  74. case ParsingState.AfterValue:
  75. if (!JSONParser.isWhitespace(c)) {
  76. if (c == CHAR_END) {
  77. parsingState = ParsingState.End;
  78. } else if (c == CHAR_ITEM_SEPARATOR) {
  79. parsingState = ParsingState.BeforeValue;
  80. } else {
  81. Debug.LogError("Invalid character \"" + c + "\" when expecting array end or item separator");
  82. }
  83. }
  84. break;
  85. }
  86. i++;
  87. }
  88. inputLength = i;
  89. }
  90. protected override string stringifyValue(int indentLevel, JSONStringifyOptions options) {
  91. var t = new StringBuilder();
  92. bool hasItemsInList = false;
  93. t.Append(CHAR_START);
  94. foreach (object valueItem in value) {
  95. if (hasItemsInList) t.Append(CHAR_ITEM_SEPARATOR);
  96. if (options.lineFeedOnArrays) {
  97. t.Append(options.lineFeed + JSONEncoder.getIndents(options.individualIndent, indentLevel + 1));
  98. } else {
  99. t.Append(options.arrayAfterSeparator);
  100. }
  101. t.Append(JSONEncoder.encode(valueItem, indentLevel + 1, options));
  102. hasItemsInList = true;
  103. }
  104. t.Append(options.lineFeed + JSONEncoder.getIndents(options.individualIndent, indentLevel));
  105. t.Append(CHAR_END);
  106. return t.ToString();
  107. }
  108. }