Example #1
0
        private BsonType MapTokenTypeToBsonType(Newtonsoft.Json.JsonToken tokenType)
        {
            switch (tokenType)
            {
            case Newtonsoft.Json.JsonToken.Boolean: return(BsonType.Boolean);

            case Newtonsoft.Json.JsonToken.Bytes: return(BsonType.Binary);

            case Newtonsoft.Json.JsonToken.Date: return(BsonType.DateTime);

            case Newtonsoft.Json.JsonToken.Float: return(BsonType.Double);

            case Newtonsoft.Json.JsonToken.Null: return(BsonType.Null);

            case Newtonsoft.Json.JsonToken.StartArray: return(BsonType.Array);

            case Newtonsoft.Json.JsonToken.String: return(BsonType.String);

            case Newtonsoft.Json.JsonToken.Undefined: return(BsonType.Undefined);

            case Newtonsoft.Json.JsonToken.Integer: return(BsonType.Int64);

            case Newtonsoft.Json.JsonToken.StartObject: return(BsonType.Document);

            default:
                var message = string.Format("Error reading BsonValue. Unexpected token: {0}.", tokenType);
                throw new Newtonsoft.Json.JsonReaderException(message);
            }
        }
Example #2
0
 private static void ConsumeToken(ref Utf8JsonStreamReader reader, JsonTokenType expected)
 {
     if (reader.Read() && expected == reader.TokenType)
     {
         return;
     }
     ThrowUnexpectedTokenException(ref reader, expected);
 }
 static void ReadAndValidate(Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.JsonToken token)
 {
     reader.Read();
     if (reader.TokenType != token)
     {
         throw new Exception();
     }
 }
 private static void ConsumeToken(ref Utf8JsonStreamReader reader, JsonTokenType expected)
 {
     if (reader.Read() && expected == reader.TokenType)
     {
         return;
     }
     throw new WorkloadManifestFormatException(Strings.ExpectedTokenAtOffset, expected, reader.TokenStartIndex);
 }
Example #5
0
        protected virtual void SetCurrentToken(Newtonsoft.Json.JsonToken tokenType, object value = null, BsonValue bsonValue = null)
        {
            _tokenType = tokenType;
            _value     = value;
            _bsonValue = bsonValue;

            switch (tokenType)
            {
            case Newtonsoft.Json.JsonToken.Boolean:
            case Newtonsoft.Json.JsonToken.Bytes:
            case Newtonsoft.Json.JsonToken.Date:
            case Newtonsoft.Json.JsonToken.Float:
            case Newtonsoft.Json.JsonToken.Integer:
            case Newtonsoft.Json.JsonToken.Null:
            case Newtonsoft.Json.JsonToken.Raw:
            case Newtonsoft.Json.JsonToken.String:
            case Newtonsoft.Json.JsonToken.Undefined:
                UpdateIndex();
                break;

            case Newtonsoft.Json.JsonToken.EndArray:
                ValidateEndToken(tokenType, MongoContainerType.Array);
                break;

            case Newtonsoft.Json.JsonToken.EndConstructor:
                ValidateEndToken(tokenType, MongoContainerType.Constructor);
                break;

            case Newtonsoft.Json.JsonToken.EndObject:
                ValidateEndToken(tokenType, MongoContainerType.Object);
                break;

            case Newtonsoft.Json.JsonToken.PropertyName:
                _currentPosition.PropertyName = (string)value;
                break;

            case Newtonsoft.Json.JsonToken.StartArray:
                Push(MongoContainerType.Array);
                break;

            case Newtonsoft.Json.JsonToken.StartConstructor:
                Push(MongoContainerType.Constructor);
                break;

            case Newtonsoft.Json.JsonToken.StartObject:
                Push(MongoContainerType.Object);
                break;

            case Newtonsoft.Json.JsonToken.Comment:
            case Newtonsoft.Json.JsonToken.None:
                break;

            default:
                var message = string.Format("Unexpected token type: {0}.", tokenType);
                throw new Newtonsoft.Json.JsonReaderException(message);
            }
        }
Example #6
0
 protected void ReadExpectedToken(Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.JsonToken expectedTokenType)
 {
     ReadToken(reader);
     if (reader.TokenType != expectedTokenType)
     {
         var message = string.Format("Expected token {0}, but got {1}.", expectedTokenType, reader.TokenType);
         throw new Newtonsoft.Json.JsonReaderException(message);
     }
 }
Example #7
0
        private void ValidateEndToken(Newtonsoft.Json.JsonToken tokenType, MongoContainerType expectedContainerType)
        {
            var poppedContainerType = Pop();

            if (poppedContainerType != expectedContainerType)
            {
                var message = string.Format(
                    "JsonToken {0} is not valid for closing JsonType {1}.", tokenType, poppedContainerType);
                throw new Newtonsoft.Json.JsonReaderException(message);
            }
        }
        private static void ReadValue(DataRow row, DataColumn column, object Value, Newtonsoft.Json.JsonToken tokenType, bool ColumnIsArray = false)
        {
            if (tokenType == Newtonsoft.Json.JsonToken.StartArray || tokenType == Newtonsoft.Json.JsonToken.StartObject || tokenType == Newtonsoft.Json.JsonToken.StartConstructor || tokenType == Newtonsoft.Json.JsonToken.EndConstructor || tokenType == Newtonsoft.Json.JsonToken.EndArray || tokenType == Newtonsoft.Json.JsonToken.EndObject)
            {
                return;
            }

            if (ColumnIsArray)
            {
                System.Collections.IList list = null;

                if (row[column] == DBNull.Value || row[column] == null)
                {
                    list = (System.Collections.IList)Activator.CreateInstance(column.DataType);
                }
                else
                {
                    list = (System.Collections.IList)row[column];
                }

                list.Add(Value);
                row[column] = list;
            }
            else
            {
                if (tokenType == Newtonsoft.Json.JsonToken.String)
                {
                    row[column] = Value ?? null;
                }
                else if (tokenType == Newtonsoft.Json.JsonToken.Date)
                {
                    row[column] = Value ?? null;
                }
                else if (tokenType == Newtonsoft.Json.JsonToken.Integer)
                {
                    row[column] = Value ?? 0;
                }
                else if (tokenType == Newtonsoft.Json.JsonToken.Float)
                {
                    row[column] = Value ?? 0;
                }
                else if (tokenType == Newtonsoft.Json.JsonToken.Boolean)
                {
                    row[column] = Value ?? false;
                }
                else
                {
                    row[column] = Value ?? null;
                }
            }
        }
        public static bool IsScalar(this Newtonsoft.Json.JsonToken token)
        {
            switch (token)
            {
            case Newtonsoft.Json.JsonToken.EndArray:
            case Newtonsoft.Json.JsonToken.EndConstructor:
            case Newtonsoft.Json.JsonToken.EndObject:
            case Newtonsoft.Json.JsonToken.StartArray:
            case Newtonsoft.Json.JsonToken.StartConstructor:
            case Newtonsoft.Json.JsonToken.StartObject:
                return(false);

            default:
                return(true);
            }
        }
Example #10
0
        // protected methods
        protected virtual void ReplaceCurrentToken(Newtonsoft.Json.JsonToken tokenType, object value = null, BsonValue bsonValue = null)
        {
            if (!tokenType.IsScalar())
            {
                var message = string.Format("New tokenType must be scalar, not: {0}.", tokenType);
                throw new ArgumentException(message, "tokenType");
            }
            if (!_tokenType.IsScalar() && _tokenType != Newtonsoft.Json.JsonToken.EndArray && _tokenType != Newtonsoft.Json.JsonToken.EndObject)
            {
                var message = string.Format("Current tokenType must be scalar, not: {0}.", _tokenType);
                throw new InvalidOperationException(message);
            }

            _tokenType = tokenType;
            _value     = value;
            _bsonValue = bsonValue;
        }
        public static bool IsPrimitive(this Newtonsoft.Json.JsonToken token)
        {
            switch (token)
            {
            case Newtonsoft.Json.JsonToken.Integer:
            case Newtonsoft.Json.JsonToken.Float:
            case Newtonsoft.Json.JsonToken.String:
            case Newtonsoft.Json.JsonToken.Boolean:
            case Newtonsoft.Json.JsonToken.Undefined:
            case Newtonsoft.Json.JsonToken.Null:
            case Newtonsoft.Json.JsonToken.Date:
            case Newtonsoft.Json.JsonToken.Bytes:
                return(true);

            default:
                return(false);
            }
        }
        public void TestBsonArray()
        {
            var ar = new BsonArray {
                new BsonInt32(200)
            };
            BsonValue el       = ar[0];
            var       b        = el.IsBsonDocument;
            var       json     = el.ToString();
            var       document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(json);

            var x = new Newtonsoft.Json.JsonToken();


            JObject o   = JObject.Parse(String.Format("{{\"Array\": {0}}}", json));
            var     jar = (JArray)o["Array"];

            json = jar[0].ToString();
        }
 /// <inheritdoc/>
 protected override void WriteEnd(Newtonsoft.Json.JsonToken token)
 {
     base.WriteEnd(token);
 }
Example #14
0
 protected new void SetToken(Newtonsoft.Json.JsonToken newToken, object value)
 {
     throw new NotSupportedException();
 }
Example #15
0
        public void Read_should_return_true_when_wrapped_reader_state_is_Initial(string json, Newtonsoft.Json.JsonToken expectedTokenType, object expectedValue, string expectedBsonValue)
        {
            var subject = CreateSubject(json);

            subject.WrappedReader.State.Should().Be(BsonReaderState.Initial);

            var result = subject.Read();

            result.Should().BeTrue();
            subject.TokenType.Should().Be(expectedTokenType);
            subject.Value.Should().Be(expectedValue);
            subject.BsonValue.Should().Be(expectedBsonValue);
        }
Example #16
0
        public void Read_should_return_true_when_wrapped_reader_state_is_Value(string json, Newtonsoft.Json.JsonToken expectedTokenType, object expectedValue, string expectedBsonValue)
        {
            var subject = CreateSubject(json);

            subject.Read(); // StartObject
            subject.Read(); // PropertyName
            subject.WrappedReader.State.Should().Be(BsonReaderState.Value);

            var result = subject.Read();

            result.Should().BeTrue();
            subject.TokenType.Should().Be(expectedTokenType);
            if (subject.Value is byte[])
            {
                ((byte[])subject.Value).Should().Equal((byte[])ParseExpectedValue(expectedValue));
            }
            else
            {
                subject.Value.Should().Be(ParseExpectedValue(expectedValue));
            }
            subject.BsonValue.Should().Be(expectedBsonValue);
        }
        // protected methods
        protected virtual void ReplaceCurrentToken(Newtonsoft.Json.JsonToken tokenType, object value = null, BsonValue bsonValue = null)
        {
            if (!tokenType.IsScalar())
            {
                var message = string.Format("New tokenType must be scalar, not: {0}.", tokenType);
                throw new ArgumentException(message, "tokenType");
            }
            if (!_tokenType.IsScalar() && _tokenType != Newtonsoft.Json.JsonToken.EndArray && _tokenType != Newtonsoft.Json.JsonToken.EndObject)
            {
                var message = string.Format("Current tokenType must be scalar, not: {0}.", _tokenType);
                throw new InvalidOperationException(message);
            }

            _tokenType = tokenType;
            _value = value;
            _bsonValue = bsonValue;
        }
Example #18
0
        private static void ThrowUnexpectedTokenException(ref Utf8JsonStreamReader reader, JsonTokenType expected)
        {
            string key;

            if (expected.IsBool())
            {
                key = Strings.ExpectedBoolAtOffset;
            }
            else if (expected.IsInt())
            {
                key = Strings.ExpectedIntegerAtOffset;
            }
            else if (expected == JsonTokenType.String)
            {
                key = Strings.ExpectedStringAtOffset;
            }
            else
            {
                throw new WorkloadManifestFormatException(Strings.ExpectedTokenAtOffset, expected, reader.TokenStartIndex);
            }

            throw new WorkloadManifestFormatException(key, reader.TokenStartIndex);
        }
        protected virtual void SetCurrentToken(Newtonsoft.Json.JsonToken tokenType, object value = null, BsonValue bsonValue = null)
        {
            _tokenType = tokenType;
            _value = value;
            _bsonValue = bsonValue;

            switch (tokenType)
            {
                case Newtonsoft.Json.JsonToken.Boolean:
                case Newtonsoft.Json.JsonToken.Bytes:
                case Newtonsoft.Json.JsonToken.Date:
                case Newtonsoft.Json.JsonToken.Float:
                case Newtonsoft.Json.JsonToken.Integer:
                case Newtonsoft.Json.JsonToken.Null:
                case Newtonsoft.Json.JsonToken.Raw:
                case Newtonsoft.Json.JsonToken.String:
                case Newtonsoft.Json.JsonToken.Undefined:
                    UpdateIndex();
                    break;

                case Newtonsoft.Json.JsonToken.EndArray:
                    ValidateEndToken(tokenType, MongoContainerType.Array);
                    break;

                case Newtonsoft.Json.JsonToken.EndConstructor:
                    ValidateEndToken(tokenType, MongoContainerType.Constructor);
                    break;

                case Newtonsoft.Json.JsonToken.EndObject:
                    ValidateEndToken(tokenType, MongoContainerType.Object);
                    break;

                case Newtonsoft.Json.JsonToken.PropertyName:
                    _currentPosition.PropertyName = (string)value;
                    break;

                case Newtonsoft.Json.JsonToken.StartArray:
                    Push(MongoContainerType.Array);
                    break;

                case Newtonsoft.Json.JsonToken.StartConstructor:
                    Push(MongoContainerType.Constructor);
                    break;

                case Newtonsoft.Json.JsonToken.StartObject:
                    Push(MongoContainerType.Object);
                    break;

                case Newtonsoft.Json.JsonToken.Comment:
                case Newtonsoft.Json.JsonToken.None:
                    break;

                default:
                    var message = string.Format("Unexpected token type: {0}.", tokenType);
                    throw new Newtonsoft.Json.JsonReaderException(message);
            }
        }