/* Copyright 2010-2014 MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; namespace MongoDB.Bson.IO { /// /// Represents a JSON token type. /// public enum JsonTokenType { /// /// An invalid token. /// Invalid, /// /// A begin array token (a '['). /// BeginArray, /// /// A begin object token (a '{'). /// BeginObject, /// /// An end array token (a ']'). /// EndArray, /// /// A left parenthesis (a '('). /// LeftParen, /// /// A right parenthesis (a ')'). /// RightParen, /// /// An end object token (a '}'). /// EndObject, /// /// A colon token (a ':'). /// Colon, /// /// A comma token (a ','). /// Comma, /// /// A DateTime token. /// DateTime, /// /// A Double token. /// Double, /// /// An Int32 token. /// Int32, /// /// And Int64 token. /// Int64, /// /// An ObjectId token. /// ObjectId, /// /// A regular expression token. /// RegularExpression, /// /// A string token. /// String, /// /// An unquoted string token. /// UnquotedString, /// /// An end of file token. /// EndOfFile } /// /// Represents a JSON token. /// public class JsonToken { // private fields private JsonTokenType _type; private string _lexeme; // constructors /// /// Initializes a new instance of the JsonToken class. /// /// The token type. /// The lexeme. public JsonToken(JsonTokenType type, string lexeme) { _type = type; _lexeme = lexeme; } // public properties /// /// Gets the token type. /// public JsonTokenType Type { get { return _type; } } /// /// Gets the lexeme. /// public string Lexeme { get { return _lexeme; } } /// /// Gets the value of a DateTime token. /// public virtual BsonDateTime DateTimeValue { get { throw new NotSupportedException(); } } /// /// Gets the value of a Double token. /// public virtual double DoubleValue { get { throw new NotSupportedException(); } } /// /// Gets the value of an Int32 token. /// public virtual int Int32Value { get { throw new NotSupportedException(); } } /// /// Gets the value of an Int64 token. /// public virtual long Int64Value { get { throw new NotSupportedException(); } } /// /// Gets a value indicating whether this token is number. /// /// /// true if this token is number; otherwise, false. /// public virtual bool IsNumber { get { return false; } } /// /// Gets the value of an ObjectId token. /// public virtual ObjectId ObjectIdValue { get { throw new NotSupportedException(); } } /// /// Gets the value of a regular expression token. /// public virtual BsonRegularExpression RegularExpressionValue { get { throw new NotSupportedException(); } } /// /// Gets the value of a string token. /// public virtual string StringValue { get { throw new NotSupportedException(); } } } /// /// Represents a DateTime JSON token. /// public class DateTimeJsonToken : JsonToken { // private fields private BsonDateTime _value; // constructors /// /// Initializes a new instance of the DateTimeJsonToken class. /// /// The lexeme. /// The DateTime value. public DateTimeJsonToken(string lexeme, BsonDateTime value) : base(JsonTokenType.DateTime, lexeme) { _value = value; } // public properties /// /// Gets the value of a DateTime token. /// public override BsonDateTime DateTimeValue { get { return _value; } } } /// /// Represents a Double JSON token. /// public class DoubleJsonToken : JsonToken { // private fields private double _value; // constructors /// /// Initializes a new instance of the DoubleJsonToken class. /// /// The lexeme. /// The Double value. public DoubleJsonToken(string lexeme, double value) : base(JsonTokenType.Double, lexeme) { _value = value; } // public properties /// /// Gets the value of a Double token. /// public override double DoubleValue { get { return _value; } } /// /// Gets the value of an Int32 token. /// public override int Int32Value { get { return (int)_value; } } /// /// Gets the value of an Int64 token. /// public override long Int64Value { get { return (long)_value; } } /// /// Gets a value indicating whether this token is number. /// /// /// true if this token is number; otherwise, false. /// public override bool IsNumber { get { return true; } } } /// /// Represents an Int32 JSON token. /// public class Int32JsonToken : JsonToken { // private fields private int _value; // constructors /// /// Initializes a new instance of the Int32JsonToken class. /// /// The lexeme. /// The Int32 value. public Int32JsonToken(string lexeme, int value) : base(JsonTokenType.Int32, lexeme) { _value = value; } // public properties /// /// Gets the value of a Double token. /// public override double DoubleValue { get { return _value; } } /// /// Gets the value of an Int32 token. /// public override int Int32Value { get { return _value; } } /// /// Gets the value of an Int32 token as an Int64. /// public override long Int64Value { get { return _value; } } /// /// Gets a value indicating whether this token is number. /// /// /// true if this token is number; otherwise, false. /// public override bool IsNumber { get { return true; } } } /// /// Represents an Int64 JSON token. /// public class Int64JsonToken : JsonToken { // private fields private long _value; // constructors /// /// Initializes a new instance of the Int64JsonToken class. /// /// The lexeme. /// The Int64 value. public Int64JsonToken(string lexeme, long value) : base(JsonTokenType.Int64, lexeme) { _value = value; } // public properties /// /// Gets the value of a Double token. /// public override double DoubleValue { get { return _value; } } /// /// Gets the value of an Int32 token. /// public override int Int32Value { get { return (int)_value; } } /// /// Gets the value of an Int64 token. /// public override long Int64Value { get { return _value; } } /// /// Gets a value indicating whether this token is number. /// /// /// true if this token is number; otherwise, false. /// public override bool IsNumber { get { return true; } } } /// /// Represents an ObjectId JSON token. /// public class ObjectIdJsonToken : JsonToken { // private fields private ObjectId _value; // constructors /// /// Initializes a new instance of the ObjectIdJsonToken class. /// /// The lexeme. /// The ObjectId value. public ObjectIdJsonToken(string lexeme, ObjectId value) : base(JsonTokenType.ObjectId, lexeme) { _value = value; } // public properties /// /// Gets the value of an ObjectId token. /// public override ObjectId ObjectIdValue { get { return _value; } } } /// /// Represents a regular expression JSON token. /// public class RegularExpressionJsonToken : JsonToken { // private fields private BsonRegularExpression _value; // constructors /// /// Initializes a new instance of the RegularExpressionJsonToken class. /// /// The lexeme. /// The BsonRegularExpression value. public RegularExpressionJsonToken(string lexeme, BsonRegularExpression value) : base(JsonTokenType.RegularExpression, lexeme) { _value = value; } // public properties /// /// Gets the value of a regular expression token. /// public override BsonRegularExpression RegularExpressionValue { get { return _value; } } } /// /// Represents a String JSON token. /// public class StringJsonToken : JsonToken { // private fields private string _value; // constructors /// /// Initializes a new instance of the StringJsonToken class. /// /// The token type. /// The lexeme. /// The String value. public StringJsonToken(JsonTokenType type, string lexeme, string value) : base(type, lexeme) { _value = value; } // public properties /// /// Gets the value of an String token. /// public override string StringValue { get { return _value; } } } }