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