JsonToken.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. namespace Google.Protobuf
  34. {
  35. internal sealed class JsonToken : IEquatable<JsonToken>
  36. {
  37. // Tokens with no value can be reused.
  38. private static readonly JsonToken _true = new JsonToken(TokenType.True);
  39. private static readonly JsonToken _false = new JsonToken(TokenType.False);
  40. private static readonly JsonToken _null = new JsonToken(TokenType.Null);
  41. private static readonly JsonToken startObject = new JsonToken(TokenType.StartObject);
  42. private static readonly JsonToken endObject = new JsonToken(TokenType.EndObject);
  43. private static readonly JsonToken startArray = new JsonToken(TokenType.StartArray);
  44. private static readonly JsonToken endArray = new JsonToken(TokenType.EndArray);
  45. private static readonly JsonToken endDocument = new JsonToken(TokenType.EndDocument);
  46. internal static JsonToken Null { get { return _null; } }
  47. internal static JsonToken False { get { return _false; } }
  48. internal static JsonToken True { get { return _true; } }
  49. internal static JsonToken StartObject{ get { return startObject; } }
  50. internal static JsonToken EndObject { get { return endObject; } }
  51. internal static JsonToken StartArray { get { return startArray; } }
  52. internal static JsonToken EndArray { get { return endArray; } }
  53. internal static JsonToken EndDocument { get { return endDocument; } }
  54. internal static JsonToken Name(string name)
  55. {
  56. return new JsonToken(TokenType.Name, stringValue: name);
  57. }
  58. internal static JsonToken Value(string value)
  59. {
  60. return new JsonToken(TokenType.StringValue, stringValue: value);
  61. }
  62. internal static JsonToken Value(double value)
  63. {
  64. return new JsonToken(TokenType.Number, numberValue: value);
  65. }
  66. internal enum TokenType
  67. {
  68. Null,
  69. False,
  70. True,
  71. StringValue,
  72. Number,
  73. Name,
  74. StartObject,
  75. EndObject,
  76. StartArray,
  77. EndArray,
  78. EndDocument
  79. }
  80. // A value is a string, number, array, object, null, true or false
  81. // Arrays and objects have start/end
  82. // A document consists of a value
  83. // Objects are name/value sequences.
  84. private readonly TokenType type;
  85. private readonly string stringValue;
  86. private readonly double numberValue;
  87. internal TokenType Type { get { return type; } }
  88. internal string StringValue { get { return stringValue; } }
  89. internal double NumberValue { get { return numberValue; } }
  90. private JsonToken(TokenType type, string stringValue = null, double numberValue = 0)
  91. {
  92. this.type = type;
  93. this.stringValue = stringValue;
  94. this.numberValue = numberValue;
  95. }
  96. public override bool Equals(object obj)
  97. {
  98. return Equals(obj as JsonToken);
  99. }
  100. public override int GetHashCode()
  101. {
  102. unchecked
  103. {
  104. int hash = 17;
  105. hash = hash * 31 + (int) type;
  106. hash = hash * 31 + stringValue == null ? 0 : stringValue.GetHashCode();
  107. hash = hash * 31 + numberValue.GetHashCode();
  108. return hash;
  109. }
  110. }
  111. public override string ToString()
  112. {
  113. switch (type)
  114. {
  115. case TokenType.Null:
  116. return "null";
  117. case TokenType.True:
  118. return "true";
  119. case TokenType.False:
  120. return "false";
  121. case TokenType.Name:
  122. return "name (" + stringValue + ")";
  123. case TokenType.StringValue:
  124. return "value (" + stringValue + ")";
  125. case TokenType.Number:
  126. return "number (" + numberValue + ")";
  127. case TokenType.StartObject:
  128. return "start-object";
  129. case TokenType.EndObject:
  130. return "end-object";
  131. case TokenType.StartArray:
  132. return "start-array";
  133. case TokenType.EndArray:
  134. return "end-array";
  135. case TokenType.EndDocument:
  136. return "end-document";
  137. default:
  138. throw new InvalidOperationException("Token is of unknown type " + type);
  139. }
  140. }
  141. public bool Equals(JsonToken other)
  142. {
  143. if (ReferenceEquals(other, null))
  144. {
  145. return false;
  146. }
  147. // Note use of other.numberValue.Equals rather than ==, so that NaN compares appropriately.
  148. return other.type == type && other.stringValue == stringValue && other.numberValue.Equals(numberValue);
  149. }
  150. }
  151. }