您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

137 行
2.7 KiB

  1. using System;
  2. using System.Globalization;
  3. namespace Boomlagoon.JSON
  4. {
  5. public class JSONValue
  6. {
  7. public JSONValue(JSONValueType type)
  8. {
  9. this.Type = type;
  10. }
  11. public JSONValue(string str)
  12. {
  13. this.Type = JSONValueType.String;
  14. this.Str = str;
  15. }
  16. public JSONValue(double number)
  17. {
  18. this.Type = JSONValueType.Number;
  19. this.Number = number;
  20. }
  21. public JSONValue(JSONObject obj)
  22. {
  23. if (obj == null)
  24. {
  25. this.Type = JSONValueType.Null;
  26. }
  27. else
  28. {
  29. this.Type = JSONValueType.Object;
  30. this.Obj = obj;
  31. }
  32. }
  33. public JSONValue(JSONArray array)
  34. {
  35. this.Type = JSONValueType.Array;
  36. this.Array = array;
  37. }
  38. public JSONValue(bool boolean)
  39. {
  40. this.Type = JSONValueType.Boolean;
  41. this.Boolean = boolean;
  42. }
  43. public JSONValue(JSONValue value)
  44. {
  45. this.Type = value.Type;
  46. switch (this.Type)
  47. {
  48. case JSONValueType.String:
  49. this.Str = value.Str;
  50. break;
  51. case JSONValueType.Number:
  52. this.Number = value.Number;
  53. break;
  54. case JSONValueType.Object:
  55. if (value.Obj != null)
  56. {
  57. this.Obj = new JSONObject(value.Obj);
  58. }
  59. break;
  60. case JSONValueType.Array:
  61. this.Array = new JSONArray(value.Array);
  62. break;
  63. case JSONValueType.Boolean:
  64. this.Boolean = value.Boolean;
  65. break;
  66. }
  67. }
  68. public JSONValueType Type { get; private set; }
  69. public string Str { get; set; }
  70. public double Number { get; set; }
  71. public JSONObject Obj { get; set; }
  72. public JSONArray Array { get; set; }
  73. public bool Boolean { get; set; }
  74. public JSONValue Parent { get; set; }
  75. public static implicit operator JSONValue(string str)
  76. {
  77. return new JSONValue(str);
  78. }
  79. public static implicit operator JSONValue(double number)
  80. {
  81. return new JSONValue(number);
  82. }
  83. public static implicit operator JSONValue(JSONObject obj)
  84. {
  85. return new JSONValue(obj);
  86. }
  87. public static implicit operator JSONValue(JSONArray array)
  88. {
  89. return new JSONValue(array);
  90. }
  91. public static implicit operator JSONValue(bool boolean)
  92. {
  93. return new JSONValue(boolean);
  94. }
  95. public override string ToString()
  96. {
  97. switch (this.Type)
  98. {
  99. case JSONValueType.String:
  100. return "\"" + this.Str + "\"";
  101. case JSONValueType.Number:
  102. return this.Number.ToString(CultureInfo.InvariantCulture);
  103. case JSONValueType.Object:
  104. return this.Obj.ToString();
  105. case JSONValueType.Array:
  106. return this.Array.ToString();
  107. case JSONValueType.Boolean:
  108. return (!this.Boolean) ? "false" : "true";
  109. case JSONValueType.Null:
  110. return "null";
  111. default:
  112. return "null";
  113. }
  114. }
  115. }
  116. }