KsonObject.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 
  2. namespace etoy
  3. {
  4. class KsonObject
  5. {
  6. string _kson;
  7. Dictionary<int, string> _stringValues;
  8. Dictionary<int, KsonObject> _objects;
  9. Dictionary<int, List<KsonObject>> _arrayObjects;
  10. List<string> _tempValues;
  11. bool _is_object;
  12. bool _is_array;
  13. int _field_count;
  14. public Dictionary<int, string> StringValues => _stringValues;
  15. public Dictionary<int, KsonObject> KsonObjects => _objects;
  16. public Dictionary<int, List<KsonObject>> ArrayObjects => _arrayObjects;
  17. public string RawKson => _kson;
  18. public bool IsObject => _is_object;
  19. public bool IsArray => _is_array;
  20. public int FieldCount => _field_count;
  21. public KsonObject(string kson)
  22. {
  23. _kson = kson;
  24. _stringValues = new Dictionary<int, string>();
  25. _objects = new Dictionary<int, KsonObject>();
  26. _arrayObjects = new Dictionary<int, List<KsonObject>>();
  27. _tempValues = new List<string>();
  28. _is_object = _kson[0] == '{' && _kson[^1] == '}';
  29. _is_array = _kson[0] == '[' && _kson[^1] == ']';
  30. ParseValues(this);
  31. _tempValues.Clear();
  32. }
  33. void ParseValues(KsonObject obj)
  34. {
  35. var values = GetStringValues(obj._kson);
  36. _field_count = values.Length;
  37. for (int i = 0, length = values.Length; i < length; i++)
  38. {
  39. var value = values[i];
  40. bool is_object = value[0] == '{' && value[^1] == '}';
  41. bool is_array = value[0] == '[' && value[^1] == ']';
  42. if (is_object)
  43. {
  44. if (obj._is_array)
  45. {
  46. if (!obj._arrayObjects.TryGetValue(i, out var collection))
  47. {
  48. collection = new List<KsonObject>();
  49. obj._arrayObjects[i] = collection;
  50. }
  51. collection.Add(new KsonObject(value));
  52. }
  53. if (obj.IsObject)
  54. obj._objects.Add(i, new KsonObject(value));
  55. }
  56. else if (is_array)
  57. {
  58. var objValues = GetStringValues(value);
  59. List<KsonObject> list = new List<KsonObject>();
  60. foreach (var objValue in objValues)
  61. list.Add(new KsonObject(objValue));
  62. if (obj._is_array)
  63. {
  64. throw new System.Exception("不支持多维数组");
  65. }
  66. if (obj._is_object) obj._arrayObjects.Add(i, list);
  67. }
  68. else
  69. {
  70. obj._stringValues.Add(i, value);
  71. }
  72. }
  73. }
  74. string[] GetStringValues(string kson)
  75. {
  76. if (string.IsNullOrEmpty(kson))
  77. return new string[] { };
  78. if (kson.Length > 1)
  79. kson = kson[1..(kson.Length - 1)];
  80. _tempValues.Clear();
  81. int right, left = 0, indent = 0;
  82. for (int i = 0, length = kson.Length; i < length; i++)
  83. {
  84. char c = kson[i];
  85. if (c == '{' || c == '[')
  86. indent++;
  87. if (c == '}' || c == ']')
  88. indent--;
  89. if (indent != 0)
  90. continue;
  91. if (c == ',')
  92. {
  93. right = i;
  94. string value = kson[left..right];
  95. _tempValues.Add(value);
  96. left = i + 1;
  97. }
  98. if (i == length - 1)
  99. {
  100. string value = kson[left..length];
  101. _tempValues.Add(value);
  102. }
  103. }
  104. return _tempValues.ToArray();
  105. }
  106. }
  107. }