Example #1
0
        public override void WriteValue(string value)
        {
            BsonToken @null;

            base.WriteValue(value);
            if (value == null)
            {
                @null = BsonEmpty.Null;
            }
            else
            {
                @null = new BsonString(value, true);
            }
            this.AddToken(@null);
        }
Example #2
0
        private Task WriteTokenInternalAsync(BsonToken t, CancellationToken cancellationToken)
        {
            switch (t.Type)
            {
            case BsonType.Object:
                return(WriteObjectAsync((BsonObject)t, cancellationToken));

            case BsonType.Array:
                return(WriteArrayAsync((BsonArray)t, cancellationToken));

            case BsonType.Integer:
                return(_asyncWriter.WriteAsync(Convert.ToInt32(((BsonValue)t).Value, CultureInfo.InvariantCulture), cancellationToken));

            case BsonType.Long:
                return(_asyncWriter.WriteAsync(Convert.ToInt64(((BsonValue)t).Value, CultureInfo.InvariantCulture), cancellationToken));

            case BsonType.Number:
                return(_asyncWriter.WriteAsync(Convert.ToDouble(((BsonValue)t).Value, CultureInfo.InvariantCulture), cancellationToken));

            case BsonType.String:
                BsonString bsonString = (BsonString)t;
                return(WriteStringAsync((string)bsonString.Value, bsonString.ByteCount, bsonString.CalculatedSize - 4, cancellationToken));

            case BsonType.Boolean:
                return(_asyncWriter.WriteAsync((bool)((BsonValue)t).Value, cancellationToken));

            case BsonType.Null:
            case BsonType.Undefined:
                return(AsyncUtils.CompletedTask);

            case BsonType.Date:
                BsonValue value = (BsonValue)t;
                return(_asyncWriter.WriteAsync(TicksFromDateObject(value.Value), cancellationToken));

            case BsonType.Binary:
                return(WriteBinaryAsync((BsonBinary)t, cancellationToken));

            case BsonType.Oid:
                return(_asyncWriter.WriteAsync((byte[])((BsonValue)t).Value, cancellationToken));

            case BsonType.Regex:
                return(WriteRegexAsync((BsonRegex)t, cancellationToken));

            default:
                throw new ArgumentOutOfRangeException(nameof(t), "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
        }
Example #3
0
        private void WriteTokenInternal(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Object:
            {
                BsonObject value = (BsonObject)t;
                _writer.Write(value.CalculatedSize);
                foreach (BsonProperty property in value)
                {
                    _writer.Write((sbyte)property.Value.Type);
                    WriteString((string)property.Name.Value, property.Name.ByteCount, null);
                    WriteTokenInternal(property.Value);
                }
                _writer.Write((byte)0);
            }
            break;

            case BsonType.Array:
            {
                BsonArray value = (BsonArray)t;
                _writer.Write(value.CalculatedSize);
                int index = 0;
                foreach (BsonToken c in value)
                {
                    _writer.Write((sbyte)c.Type);
                    WriteString(index.ToString(CultureInfo.InvariantCulture), MathUtils.IntLength(index), null);
                    WriteTokenInternal(c);
                    index++;
                }
                _writer.Write((byte)0);
            }
            break;

            case BsonType.Integer:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write(Convert.ToInt32(value.Value, CultureInfo.InvariantCulture));
            }
            break;

            case BsonType.Long:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write(Convert.ToInt64(value.Value, CultureInfo.InvariantCulture));
            }
            break;

            case BsonType.Number:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write(Convert.ToDouble(value.Value, CultureInfo.InvariantCulture));
            }
            break;

            case BsonType.String:
            {
                BsonString value = (BsonString)t;
                WriteString((string)value.Value, value.ByteCount, value.CalculatedSize - 4);
            }
            break;

            case BsonType.Boolean:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write((bool)value.Value);
            }
            break;

            case BsonType.Null:
            case BsonType.Undefined:
                break;

            case BsonType.Date:
            {
                BsonValue value = (BsonValue)t;

                long ticks = 0;

                if (value.Value is DateTime)
                {
                    DateTime dateTime = (DateTime)value.Value;
                    if (DateTimeKindHandling == DateTimeKind.Utc)
                    {
                        dateTime = dateTime.ToUniversalTime();
                    }
                    else if (DateTimeKindHandling == DateTimeKind.Local)
                    {
                        dateTime = dateTime.ToLocalTime();
                    }

                    ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTime, false);
                }
#if !PocketPC && !NET20
                else
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)value.Value;
                    ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
                }
#endif

                _writer.Write(ticks);
            }
            break;

            case BsonType.Binary:
            {
                BsonValue value = (BsonValue)t;

                byte[] data = (byte[])value.Value;
                _writer.Write(data.Length);
                _writer.Write((byte)BsonBinaryType.Binary);
                _writer.Write(data);
            }
            break;

            case BsonType.Oid:
            {
                BsonValue value = (BsonValue)t;

                byte[] data = (byte[])value.Value;
                _writer.Write(data);
            }
            break;

            case BsonType.Regex:
            {
                BsonRegex value = (BsonRegex)t;

                WriteString((string)value.Pattern.Value, value.Pattern.ByteCount, null);
                WriteString((string)value.Options.Value, value.Options.ByteCount, null);
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
        }
Example #4
0
        private int CalculateSize(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Object:
            {
                BsonObject value = (BsonObject)t;

                int bases = 4;
                foreach (BsonProperty p in value)
                {
                    int size = 1;
                    size += CalculateSize(p.Name);
                    size += CalculateSize(p.Value);

                    bases += size;
                }
                bases += 1;
                value.CalculatedSize = bases;
                return(bases);
            }

            case BsonType.Array:
            {
                BsonArray value = (BsonArray)t;

                int size  = 4;
                int index = 0;
                foreach (BsonToken c in value)
                {
                    size += 1;
                    size += CalculateSize(MathUtils.IntLength(index));
                    size += CalculateSize(c);
                    index++;
                }
                size += 1;
                value.CalculatedSize = size;

                return(value.CalculatedSize);
            }

            case BsonType.Integer:
                return(4);

            case BsonType.Long:
                return(8);

            case BsonType.Number:
                return(8);

            case BsonType.String:
            {
                BsonString value = (BsonString)t;
                string     s     = (string)value.Value;
                value.ByteCount      = (s != null) ? Encoding.GetByteCount(s) : 0;
                value.CalculatedSize = CalculateSizeWithLength(value.ByteCount, value.IncludeLength);

                return(value.CalculatedSize);
            }

            case BsonType.Boolean:
                return(1);

            case BsonType.Null:
            case BsonType.Undefined:
                return(0);

            case BsonType.Date:
                return(8);

            case BsonType.Binary:
            {
                BsonValue value = (BsonValue)t;

                byte[] data = (byte[])value.Value;
                value.CalculatedSize = 4 + 1 + data.Length;

                return(value.CalculatedSize);
            }

            case BsonType.Oid:
                return(12);

            case BsonType.Regex:
            {
                BsonRegex value = (BsonRegex)t;
                int       size  = 0;
                size += CalculateSize(value.Pattern);
                size += CalculateSize(value.Options);
                value.CalculatedSize = size;

                return(value.CalculatedSize);
            }

            default:
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
        }
 public BsonRegex(string pattern, string options)
 {
     Pattern = new BsonString(pattern, false);
     Options = new BsonString(options, false);
 }
Example #6
0
        private int CalculateSize(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Number:
            {
                return(8);
            }

            case BsonType.String:
            {
                BsonString bsonString = (BsonString)t;
                string     value      = (string)bsonString.Value;
                bsonString.ByteCount      = (value != null ? BsonBinaryWriter.Encoding.GetByteCount(value) : 0);
                bsonString.CalculatedSize = this.CalculateSizeWithLength(bsonString.ByteCount, bsonString.IncludeLength);
                return(bsonString.CalculatedSize);
            }

            case BsonType.Object:
            {
                BsonObject bsonObjects = (BsonObject)t;
                int        num         = 4;
                foreach (BsonProperty bsonProperty in bsonObjects)
                {
                    int num1 = 1 + this.CalculateSize(bsonProperty.Name);
                    num1 += this.CalculateSize(bsonProperty.Value);
                    num  += num1;
                }
                num++;
                bsonObjects.CalculatedSize = num;
                return(num);
            }

            case BsonType.Array:
            {
                BsonArray bsonArrays = (BsonArray)t;
                int       num2       = 4;
                ulong     num3       = (ulong)0;
                foreach (BsonToken bsonToken in bsonArrays)
                {
                    num2++;
                    num2 += this.CalculateSize(MathUtils.IntLength(num3));
                    num2 += this.CalculateSize(bsonToken);
                    num3 += (long)1;
                }
                num2++;
                bsonArrays.CalculatedSize = num2;
                return(bsonArrays.CalculatedSize);
            }

            case BsonType.Binary:
            {
                BsonBinary length   = (BsonBinary)t;
                byte[]     numArray = (byte[])length.Value;
                length.CalculatedSize = 5 + (int)numArray.Length;
                return(length.CalculatedSize);
            }

            case BsonType.Undefined:
            case BsonType.Null:
            {
                return(0);
            }

            case BsonType.Oid:
            {
                return(12);
            }

            case BsonType.Boolean:
            {
                return(1);
            }

            case BsonType.Date:
            {
                return(8);
            }

            case BsonType.Regex:
            {
                BsonRegex bsonRegex = (BsonRegex)t;
                int       num4      = 0 + this.CalculateSize(bsonRegex.Pattern);
                num4 += this.CalculateSize(bsonRegex.Options);
                bsonRegex.CalculatedSize = num4;
                return(bsonRegex.CalculatedSize);
            }

            case BsonType.Reference:
            case BsonType.Code:
            case BsonType.Symbol:
            case BsonType.CodeWScope:
            case BsonType.TimeStamp:
            {
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }

            case BsonType.Integer:
            {
                return(4);
            }

            case BsonType.Long:
            {
                return(8);
            }

            default:
            {
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
            }
        }
Example #7
0
        private void WriteTokenInternal(BsonToken t)
        {
            int?nullable;

            switch (t.Type)
            {
            case BsonType.Number:
            {
                BsonValue bsonValue = (BsonValue)t;
                this._writer.Write(Convert.ToDouble(bsonValue.Value, CultureInfo.InvariantCulture));
                return;
            }

            case BsonType.String:
            {
                BsonString bsonString = (BsonString)t;
                this.WriteString((string)bsonString.Value, bsonString.ByteCount, new int?(bsonString.CalculatedSize - 4));
                return;
            }

            case BsonType.Object:
            {
                BsonObject bsonObjects = (BsonObject)t;
                this._writer.Write(bsonObjects.CalculatedSize);
                foreach (BsonProperty bsonProperty in bsonObjects)
                {
                    this._writer.Write((sbyte)bsonProperty.Value.Type);
                    nullable = null;
                    this.WriteString((string)bsonProperty.Name.Value, bsonProperty.Name.ByteCount, nullable);
                    this.WriteTokenInternal(bsonProperty.Value);
                }
                this._writer.Write((byte)0);
                return;
            }

            case BsonType.Array:
            {
                BsonArray bsonArrays = (BsonArray)t;
                this._writer.Write(bsonArrays.CalculatedSize);
                ulong num = (ulong)0;
                foreach (BsonToken bsonToken in bsonArrays)
                {
                    this._writer.Write((sbyte)bsonToken.Type);
                    nullable = null;
                    this.WriteString(num.ToString(CultureInfo.InvariantCulture), MathUtils.IntLength(num), nullable);
                    this.WriteTokenInternal(bsonToken);
                    num += (long)1;
                }
                this._writer.Write((byte)0);
                return;
            }

            case BsonType.Binary:
            {
                BsonBinary bsonBinary = (BsonBinary)t;
                byte[]     value      = (byte[])bsonBinary.Value;
                this._writer.Write((int)value.Length);
                this._writer.Write((byte)bsonBinary.BinaryType);
                this._writer.Write(value);
                return;
            }

            case BsonType.Undefined:
            case BsonType.Null:
            {
                return;
            }

            case BsonType.Oid:
            {
                byte[] numArray = (byte[])((BsonValue)t).Value;
                this._writer.Write(numArray);
                return;
            }

            case BsonType.Boolean:
            {
                BsonValue bsonValue1 = (BsonValue)t;
                this._writer.Write((bool)bsonValue1.Value);
                return;
            }

            case BsonType.Date:
            {
                BsonValue bsonValue2      = (BsonValue)t;
                long      javaScriptTicks = (long)0;
                if (!(bsonValue2.Value is DateTime))
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)bsonValue2.Value;
                    javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
                }
                else
                {
                    DateTime universalTime = (DateTime)bsonValue2.Value;
                    if (this.DateTimeKindHandling == DateTimeKind.Utc)
                    {
                        universalTime = universalTime.ToUniversalTime();
                    }
                    else if (this.DateTimeKindHandling == DateTimeKind.Local)
                    {
                        universalTime = universalTime.ToLocalTime();
                    }
                    javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(universalTime, false);
                }
                this._writer.Write(javaScriptTicks);
                return;
            }

            case BsonType.Regex:
            {
                BsonRegex bsonRegex = (BsonRegex)t;
                nullable = null;
                this.WriteString((string)bsonRegex.Pattern.Value, bsonRegex.Pattern.ByteCount, nullable);
                nullable = null;
                this.WriteString((string)bsonRegex.Options.Value, bsonRegex.Options.ByteCount, nullable);
                return;
            }

            case BsonType.Reference:
            case BsonType.Code:
            case BsonType.Symbol:
            case BsonType.CodeWScope:
            case BsonType.TimeStamp:
            {
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }

            case BsonType.Integer:
            {
                BsonValue bsonValue3 = (BsonValue)t;
                this._writer.Write(Convert.ToInt32(bsonValue3.Value, CultureInfo.InvariantCulture));
                return;
            }

            case BsonType.Long:
            {
                BsonValue bsonValue4 = (BsonValue)t;
                this._writer.Write(Convert.ToInt64(bsonValue4.Value, CultureInfo.InvariantCulture));
                return;
            }

            default:
            {
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
            }
        }
Example #8
0
        private void WriteTokenInternal(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Object:
            {
                BsonObject value = (BsonObject)t;
                _writer.Write(value.CalculatedSize);
                foreach (BsonProperty property in value)
                {
                    _writer.Write((sbyte)property.Value.Type);
                    WriteString((string)property.Name.Value, property.Name.ByteCount, null);
                    WriteTokenInternal(property.Value);
                }
                _writer.Write((byte)0);
            }
            break;

            case BsonType.Array:
            {
                BsonArray value = (BsonArray)t;
                _writer.Write(value.CalculatedSize);
                ulong index = 0;
                foreach (BsonToken c in value)
                {
                    _writer.Write((sbyte)c.Type);
                    WriteString(index.ToString(CultureInfo.InvariantCulture), MathUtils.IntLength(index), null);
                    WriteTokenInternal(c);
                    index++;
                }
                _writer.Write((byte)0);
            }
            break;

            case BsonType.Integer:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write(Convert.ToInt32(value.Value, CultureInfo.InvariantCulture));
            }
            break;

            case BsonType.Long:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write(Convert.ToInt64(value.Value, CultureInfo.InvariantCulture));
            }
            break;

            case BsonType.Number:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write(Convert.ToDouble(value.Value, CultureInfo.InvariantCulture));
            }
            break;

            case BsonType.String:
            {
                BsonString value = (BsonString)t;
                WriteString((string)value.Value, value.ByteCount, value.CalculatedSize - 4);
            }
            break;

            case BsonType.Boolean:
                _writer.Write(t == BsonBoolean.True);
                break;

            case BsonType.Null:
            case BsonType.Undefined:
                break;

            case BsonType.Date:
            {
                BsonValue value = (BsonValue)t;
                _writer.Write(TicksFromDateObject(value.Value));
            }
            break;

            case BsonType.Binary:
            {
                BsonBinary value = (BsonBinary)t;

                byte[] data = (byte[])value.Value;
                _writer.Write(data.Length);
                _writer.Write((byte)value.BinaryType);
                _writer.Write(data);
            }
            break;

            case BsonType.Oid:
            {
                BsonValue value = (BsonValue)t;

                byte[] data = (byte[])value.Value;
                _writer.Write(data);
            }
            break;

            case BsonType.Regex:
            {
                BsonRegex value = (BsonRegex)t;

                WriteString((string)value.Pattern.Value, value.Pattern.ByteCount, null);
                WriteString((string)value.Options.Value, value.Options.ByteCount, null);
            }
            break;

            default:
                throw new ArgumentOutOfRangeException(nameof(t), "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
        }
        private void WriteTokenInternal(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Number:
                this._writer.Write(Convert.ToDouble(((BsonValue)t).Value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            case BsonType.String:
                BsonString bsonString = (BsonString)t;
                this.WriteString((string)bsonString.Value, bsonString.ByteCount, new int?(bsonString.CalculatedSize - 4));
                break;

            case BsonType.Object:
                BsonObject bsonObject = (BsonObject)t;
                this._writer.Write(bsonObject.CalculatedSize);
                foreach (BsonProperty bsonProperty in bsonObject)
                {
                    this._writer.Write((sbyte)bsonProperty.Value.Type);
                    this.WriteString((string)bsonProperty.Name.Value, bsonProperty.Name.ByteCount, new int?());
                    this.WriteTokenInternal(bsonProperty.Value);
                }
                this._writer.Write((byte)0);
                break;

            case BsonType.Array:
                BsonArray bsonArray = (BsonArray)t;
                this._writer.Write(bsonArray.CalculatedSize);
                ulong i = 0;
                foreach (BsonToken t1 in bsonArray)
                {
                    this._writer.Write((sbyte)t1.Type);
                    this.WriteString(i.ToString((IFormatProvider)CultureInfo.InvariantCulture), MathUtils.IntLength(i), new int?());
                    this.WriteTokenInternal(t1);
                    ++i;
                }
                this._writer.Write((byte)0);
                break;

            case BsonType.Binary:
                BsonBinary bsonBinary = (BsonBinary)t;
                byte[]     buffer     = (byte[])bsonBinary.Value;
                this._writer.Write(buffer.Length);
                this._writer.Write((byte)bsonBinary.BinaryType);
                this._writer.Write(buffer);
                break;

            case BsonType.Undefined:
                break;

            case BsonType.Oid:
                this._writer.Write((byte[])((BsonValue)t).Value);
                break;

            case BsonType.Boolean:
                this._writer.Write((bool)((BsonValue)t).Value);
                break;

            case BsonType.Date:
                BsonValue bsonValue = (BsonValue)t;
                long      javaScriptTicks;
                if (bsonValue.Value is DateTime)
                {
                    DateTime dateTime = (DateTime)bsonValue.Value;
                    if (this.DateTimeKindHandling == DateTimeKind.Utc)
                    {
                        dateTime = dateTime.ToUniversalTime();
                    }
                    else if (this.DateTimeKindHandling == DateTimeKind.Local)
                    {
                        dateTime = dateTime.ToLocalTime();
                    }
                    javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTime, false);
                }
                else
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)bsonValue.Value;
                    javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
                }
                this._writer.Write(javaScriptTicks);
                break;

            case BsonType.Null:
                break;

            case BsonType.Regex:
                BsonRegex bsonRegex = (BsonRegex)t;
                this.WriteString((string)bsonRegex.Pattern.Value, bsonRegex.Pattern.ByteCount, new int?());
                this.WriteString((string)bsonRegex.Options.Value, bsonRegex.Options.ByteCount, new int?());
                break;

            case BsonType.Integer:
                this._writer.Write(Convert.ToInt32(((BsonValue)t).Value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            case BsonType.Long:
                this._writer.Write(Convert.ToInt64(((BsonValue)t).Value, (IFormatProvider)CultureInfo.InvariantCulture));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(t), "Unexpected token when writing BSON: {0}".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)t.Type));
            }
        }
        private int CalculateSize(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Number:
                return(8);

            case BsonType.String:
                BsonString bsonString = (BsonString)t;
                string     s          = (string)bsonString.Value;
                bsonString.ByteCount = s != null?BsonBinaryWriter.Encoding.GetByteCount(s) : 0;

                bsonString.CalculatedSize = this.CalculateSizeWithLength(bsonString.ByteCount, bsonString.IncludeLength);
                return(bsonString.CalculatedSize);

            case BsonType.Object:
                BsonObject bsonObject = (BsonObject)t;
                int        num1       = 4;
                foreach (BsonProperty bsonProperty in bsonObject)
                {
                    int num2 = 1 + this.CalculateSize((BsonToken)bsonProperty.Name) + this.CalculateSize(bsonProperty.Value);
                    num1 += num2;
                }
                int num3 = num1 + 1;
                bsonObject.CalculatedSize = num3;
                return(num3);

            case BsonType.Array:
                BsonArray bsonArray = (BsonArray)t;
                int       num4      = 4;
                ulong     i         = 0;
                foreach (BsonToken t1 in bsonArray)
                {
                    ++num4;
                    num4 += this.CalculateSize(MathUtils.IntLength(i));
                    num4 += this.CalculateSize(t1);
                    ++i;
                }
                int num5 = num4 + 1;
                bsonArray.CalculatedSize = num5;
                return(bsonArray.CalculatedSize);

            case BsonType.Binary:
                BsonBinary bsonBinary = (BsonBinary)t;
                bsonBinary.CalculatedSize = 5 + ((byte[])bsonBinary.Value).Length;
                return(bsonBinary.CalculatedSize);

            case BsonType.Undefined:
            case BsonType.Null:
                return(0);

            case BsonType.Oid:
                return(12);

            case BsonType.Boolean:
                return(1);

            case BsonType.Date:
                return(8);

            case BsonType.Regex:
                BsonRegex bsonRegex = (BsonRegex)t;
                int       num6      = 0 + this.CalculateSize((BsonToken)bsonRegex.Pattern) + this.CalculateSize((BsonToken)bsonRegex.Options);
                bsonRegex.CalculatedSize = num6;
                return(bsonRegex.CalculatedSize);

            case BsonType.Integer:
                return(4);

            case BsonType.Long:
                return(8);

            default:
                throw new ArgumentOutOfRangeException(nameof(t), "Unexpected token when writing BSON: {0}".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)t.Type));
            }
        }
        private int CalculateSize(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Number:
                return(8);

            case BsonType.String:
            {
                BsonString str = (BsonString)t;
                string     s   = (string)str.Value;
                str.ByteCount      = (s != null) ? Encoding.GetByteCount(s) : 0;
                str.CalculatedSize = this.CalculateSizeWithLength(str.ByteCount, str.IncludeLength);
                return(str.CalculatedSize);
            }

            case BsonType.Object:
            {
                BsonObject obj2 = (BsonObject)t;
                int        num  = 4;
                foreach (BsonProperty property in obj2)
                {
                    int num2 = 1;
                    num2 += this.CalculateSize(property.Name);
                    num2 += this.CalculateSize(property.Value);
                    num  += num2;
                }
                num++;
                obj2.CalculatedSize = num;
                return(num);
            }

            case BsonType.Array:
            {
                BsonArray array = (BsonArray)t;
                int       num3  = 4;
                ulong     i     = 0L;
                foreach (BsonToken token in array)
                {
                    num3++;
                    num3 += this.CalculateSize(MathUtils.IntLength(i));
                    num3 += this.CalculateSize(token);
                    i    += (ulong)1L;
                }
                num3++;
                array.CalculatedSize = num3;
                return(array.CalculatedSize);
            }

            case BsonType.Binary:
            {
                BsonBinary binary1 = (BsonBinary)t;
                byte[]     buffer  = (byte[])binary1.Value;
                binary1.CalculatedSize = 5 + buffer.Length;
                return(binary1.CalculatedSize);
            }

            case BsonType.Undefined:
            case BsonType.Null:
                return(0);

            case BsonType.Oid:
                return(12);

            case BsonType.Boolean:
                return(1);

            case BsonType.Date:
                return(8);

            case BsonType.Regex:
            {
                BsonRegex regex = (BsonRegex)t;
                int       num5  = 0;
                num5 += this.CalculateSize(regex.Pattern);
                num5 += this.CalculateSize(regex.Options);
                regex.CalculatedSize = num5;
                return(regex.CalculatedSize);
            }

            case BsonType.Integer:
                return(4);

            case BsonType.Long:
                return(8);
            }
            throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
        }
        private void WriteTokenInternal(BsonToken t)
        {
            int?     nullable;
            long     num2;
            DateTime time;

            switch (t.Type)
            {
            case BsonType.Number:
            {
                BsonValue value4 = (BsonValue)t;
                this._writer.Write(Convert.ToDouble(value4.Value, CultureInfo.InvariantCulture));
                return;
            }

            case BsonType.String:
            {
                BsonString str = (BsonString)t;
                this.WriteString((string)str.Value, str.ByteCount, new int?(str.CalculatedSize - 4));
                return;
            }

            case BsonType.Object:
            {
                BsonObject obj2 = (BsonObject)t;
                this._writer.Write(obj2.CalculatedSize);
                foreach (BsonProperty property in obj2)
                {
                    this._writer.Write((sbyte)property.Value.Type);
                    nullable = null;
                    this.WriteString((string)property.Name.Value, property.Name.ByteCount, nullable);
                    this.WriteTokenInternal(property.Value);
                }
                this._writer.Write((byte)0);
                return;
            }

            case BsonType.Array:
            {
                BsonArray array = (BsonArray)t;
                this._writer.Write(array.CalculatedSize);
                ulong i = 0L;
                foreach (BsonToken token in array)
                {
                    this._writer.Write((sbyte)token.Type);
                    nullable = null;
                    this.WriteString(i.ToString(CultureInfo.InvariantCulture), MathUtils.IntLength(i), nullable);
                    this.WriteTokenInternal(token);
                    i += (ulong)1L;
                }
                this._writer.Write((byte)0);
                return;
            }

            case BsonType.Binary:
            {
                BsonBinary binary = (BsonBinary)t;
                byte[]     buffer = (byte[])binary.Value;
                this._writer.Write(buffer.Length);
                this._writer.Write((byte)binary.BinaryType);
                this._writer.Write(buffer);
                return;
            }

            case BsonType.Undefined:
            case BsonType.Null:
                return;

            case BsonType.Oid:
            {
                byte[] buffer2 = (byte[])((BsonValue)t).Value;
                this._writer.Write(buffer2);
                return;
            }

            case BsonType.Boolean:
            {
                BsonValue value5 = (BsonValue)t;
                this._writer.Write((bool)value5.Value);
                return;
            }

            case BsonType.Date:
            {
                BsonValue value6 = (BsonValue)t;
                num2 = 0L;
                if (!(value6.Value is DateTime))
                {
                    DateTimeOffset offset = (DateTimeOffset)value6.Value;
                    num2 = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(offset.UtcDateTime, offset.Offset);
                    goto Label_02E9;
                }
                time = (DateTime)value6.Value;
                if (this.DateTimeKindHandling != DateTimeKind.Utc)
                {
                    if (this.DateTimeKindHandling == DateTimeKind.Local)
                    {
                        time = time.ToLocalTime();
                    }
                    break;
                }
                time = time.ToUniversalTime();
                break;
            }

            case BsonType.Regex:
            {
                BsonRegex regex = (BsonRegex)t;
                nullable = null;
                this.WriteString((string)regex.Pattern.Value, regex.Pattern.ByteCount, nullable);
                this.WriteString((string)regex.Options.Value, regex.Options.ByteCount, null);
                return;
            }

            case BsonType.Integer:
            {
                BsonValue value2 = (BsonValue)t;
                this._writer.Write(Convert.ToInt32(value2.Value, CultureInfo.InvariantCulture));
                return;
            }

            case BsonType.Long:
            {
                BsonValue value3 = (BsonValue)t;
                this._writer.Write(Convert.ToInt64(value3.Value, CultureInfo.InvariantCulture));
                return;
            }

            default:
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
            num2 = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(time, false);
Label_02E9:
            this._writer.Write(num2);
        }
Example #13
0
 public BsonRegex(string pattern, string options)
 {
     this.Pattern = new BsonString((object)pattern, false);
     this.Options = new BsonString((object)options, false);
 }
Example #14
0
        private int CalculateSize(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Number:
                return(8);

            case BsonType.String:
            {
                BsonString bsonString = (BsonString)t;
                string     text       = (string)bsonString.Value;
                bsonString.ByteCount      = ((text == null) ? 0 : BsonBinaryWriter.Encoding.GetByteCount(text));
                bsonString.CalculatedSize = this.CalculateSizeWithLength(bsonString.ByteCount, bsonString.IncludeLength);
                return(bsonString.CalculatedSize);
            }

            case BsonType.Object:
            {
                BsonObject bsonObject = (BsonObject)t;
                int        num        = 4;
                foreach (BsonProperty bsonProperty in bsonObject)
                {
                    int num2 = 1;
                    num2 += this.CalculateSize(bsonProperty.Name);
                    num2 += this.CalculateSize(bsonProperty.Value);
                    num  += num2;
                }
                num++;
                bsonObject.CalculatedSize = num;
                return(num);
            }

            case BsonType.Array:
            {
                BsonArray bsonArray = (BsonArray)t;
                int       num3      = 4;
                int       num4      = 0;
                foreach (BsonToken t2 in bsonArray)
                {
                    num3++;
                    num3 += this.CalculateSize(MathUtils.IntLength(num4));
                    num3 += this.CalculateSize(t2);
                    num4++;
                }
                num3++;
                bsonArray.CalculatedSize = num3;
                return(bsonArray.CalculatedSize);
            }

            case BsonType.Binary:
            {
                BsonValue bsonValue = (BsonValue)t;
                byte[]    array     = (byte[])bsonValue.Value;
                bsonValue.CalculatedSize = 5 + array.Length;
                return(bsonValue.CalculatedSize);
            }

            case BsonType.Undefined:
            case BsonType.Null:
                return(0);

            case BsonType.Oid:
                return(12);

            case BsonType.Boolean:
                return(1);

            case BsonType.Date:
                return(8);

            case BsonType.Regex:
            {
                BsonRegex bsonRegex = (BsonRegex)t;
                int       num5      = 0;
                num5 += this.CalculateSize(bsonRegex.Pattern);
                num5 += this.CalculateSize(bsonRegex.Options);
                bsonRegex.CalculatedSize = num5;
                return(bsonRegex.CalculatedSize);
            }

            case BsonType.Integer:
                return(4);

            case BsonType.Long:
                return(8);
            }
            throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, new object[]
            {
                t.Type
            }));
        }
        private void WriteTokenInternal(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Object:
            {
                BsonObject bsonObject = (BsonObject)t;
                _writer.Write(bsonObject.CalculatedSize);
                foreach (BsonProperty item in bsonObject)
                {
                    _writer.Write((sbyte)item.Value.Type);
                    WriteString((string)item.Name.Value, item.Name.ByteCount, null);
                    WriteTokenInternal(item.Value);
                }
                _writer.Write((byte)0);
                break;
            }

            case BsonType.Array:
            {
                BsonArray bsonArray = (BsonArray)t;
                _writer.Write(bsonArray.CalculatedSize);
                int num2 = 0;
                foreach (BsonToken item2 in bsonArray)
                {
                    _writer.Write((sbyte)item2.Type);
                    WriteString(num2.ToString(CultureInfo.InvariantCulture), MathUtils.IntLength(num2), null);
                    WriteTokenInternal(item2);
                    num2++;
                }
                _writer.Write((byte)0);
                break;
            }

            case BsonType.Integer:
            {
                BsonValue bsonValue = (BsonValue)t;
                _writer.Write(Convert.ToInt32(bsonValue.Value, CultureInfo.InvariantCulture));
                break;
            }

            case BsonType.Long:
            {
                BsonValue bsonValue = (BsonValue)t;
                _writer.Write(Convert.ToInt64(bsonValue.Value, CultureInfo.InvariantCulture));
                break;
            }

            case BsonType.Number:
            {
                BsonValue bsonValue = (BsonValue)t;
                _writer.Write(Convert.ToDouble(bsonValue.Value, CultureInfo.InvariantCulture));
                break;
            }

            case BsonType.String:
            {
                BsonString bsonString = (BsonString)t;
                WriteString((string)bsonString.Value, bsonString.ByteCount, bsonString.CalculatedSize - 4);
                break;
            }

            case BsonType.Boolean:
            {
                BsonValue bsonValue = (BsonValue)t;
                _writer.Write((bool)bsonValue.Value);
                break;
            }

            case BsonType.Undefined:
            case BsonType.Null:
                break;

            case BsonType.Date:
            {
                BsonValue bsonValue = (BsonValue)t;
                long      num       = 0L;
                if (bsonValue.Value is DateTime)
                {
                    DateTime dateTime = (DateTime)bsonValue.Value;
                    if (DateTimeKindHandling == DateTimeKind.Utc)
                    {
                        dateTime = dateTime.ToUniversalTime();
                    }
                    else if (DateTimeKindHandling == DateTimeKind.Local)
                    {
                        dateTime = dateTime.ToLocalTime();
                    }
                    num = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTime, convertToUtc: false);
                }
                else
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)bsonValue.Value;
                    num = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
                }
                _writer.Write(num);
                break;
            }

            case BsonType.Binary:
            {
                BsonValue bsonValue = (BsonValue)t;
                byte[]    buffer    = (byte[])bsonValue.Value;
                _writer.Write(buffer.Length);
                _writer.Write((byte)0);
                _writer.Write(buffer);
                break;
            }

            case BsonType.Oid:
            {
                BsonValue bsonValue = (BsonValue)t;
                byte[]    buffer    = (byte[])bsonValue.Value;
                _writer.Write(buffer);
                break;
            }

            case BsonType.Regex:
            {
                BsonRegex bsonRegex = (BsonRegex)t;
                WriteString((string)bsonRegex.Pattern.Value, bsonRegex.Pattern.ByteCount, null);
                WriteString((string)bsonRegex.Options.Value, bsonRegex.Options.ByteCount, null);
                break;
            }

            default:
                throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
        }
Example #16
0
        private void WriteTokenInternal(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Number:
            {
                BsonValue bsonValue = (BsonValue)t;
                this._writer.Write(Convert.ToDouble(bsonValue.Value, CultureInfo.InvariantCulture));
                return;
            }

            case BsonType.String:
            {
                BsonString bsonString = (BsonString)t;
                this.WriteString((string)bsonString.Value, bsonString.ByteCount, new int?(bsonString.CalculatedSize - 4));
                return;
            }

            case BsonType.Object:
            {
                BsonObject bsonObject = (BsonObject)t;
                this._writer.Write(bsonObject.CalculatedSize);
                foreach (BsonProperty bsonProperty in bsonObject)
                {
                    this._writer.Write((sbyte)bsonProperty.Value.Type);
                    this.WriteString((string)bsonProperty.Name.Value, bsonProperty.Name.ByteCount, null);
                    this.WriteTokenInternal(bsonProperty.Value);
                }
                this._writer.Write(0);
                return;
            }

            case BsonType.Array:
            {
                BsonArray bsonArray = (BsonArray)t;
                this._writer.Write(bsonArray.CalculatedSize);
                int num = 0;
                foreach (BsonToken bsonToken in bsonArray)
                {
                    this._writer.Write((sbyte)bsonToken.Type);
                    this.WriteString(num.ToString(CultureInfo.InvariantCulture), MathUtils.IntLength(num), null);
                    this.WriteTokenInternal(bsonToken);
                    num++;
                }
                this._writer.Write(0);
                return;
            }

            case BsonType.Binary:
            {
                BsonValue bsonValue2 = (BsonValue)t;
                byte[]    array      = (byte[])bsonValue2.Value;
                this._writer.Write(array.Length);
                this._writer.Write(0);
                this._writer.Write(array);
                return;
            }

            case BsonType.Undefined:
            case BsonType.Null:
                return;

            case BsonType.Oid:
            {
                BsonValue bsonValue3 = (BsonValue)t;
                byte[]    buffer     = (byte[])bsonValue3.Value;
                this._writer.Write(buffer);
                return;
            }

            case BsonType.Boolean:
            {
                BsonValue bsonValue4 = (BsonValue)t;
                this._writer.Write((bool)bsonValue4.Value);
                return;
            }

            case BsonType.Date:
            {
                BsonValue bsonValue5 = (BsonValue)t;
                long      value;
                if (bsonValue5.Value is DateTime)
                {
                    DateTime dateTime = (DateTime)bsonValue5.Value;
                    if (this.DateTimeKindHandling == DateTimeKind.Utc)
                    {
                        dateTime = dateTime.ToUniversalTime();
                    }
                    else if (this.DateTimeKindHandling == DateTimeKind.Local)
                    {
                        dateTime = dateTime.ToLocalTime();
                    }
                    value = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTime, false);
                }
                else
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)bsonValue5.Value;
                    value = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
                }
                this._writer.Write(value);
                return;
            }

            case BsonType.Regex:
            {
                BsonRegex bsonRegex = (BsonRegex)t;
                this.WriteString((string)bsonRegex.Pattern.Value, bsonRegex.Pattern.ByteCount, null);
                this.WriteString((string)bsonRegex.Options.Value, bsonRegex.Options.ByteCount, null);
                return;
            }

            case BsonType.Integer:
            {
                BsonValue bsonValue6 = (BsonValue)t;
                this._writer.Write(Convert.ToInt32(bsonValue6.Value, CultureInfo.InvariantCulture));
                return;
            }

            case BsonType.Long:
            {
                BsonValue bsonValue7 = (BsonValue)t;
                this._writer.Write(Convert.ToInt64(bsonValue7.Value, CultureInfo.InvariantCulture));
                return;
            }
            }
            throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, new object[]
            {
                t.Type
            }));
        }
Example #17
0
        private void WriteTokenInternal(BsonToken t)
        {
            switch (t.Type)
            {
            case BsonType.Number:
            {
                BsonValue bsonValue = (BsonValue)t;
                this._writer.Write(Convert.ToDouble(bsonValue.Value, CultureInfo.get_InvariantCulture()));
                return;
            }

            case BsonType.String:
            {
                BsonString bsonString = (BsonString)t;
                this.WriteString((string)bsonString.Value, bsonString.ByteCount, new int?(bsonString.CalculatedSize - 4));
                return;
            }

            case BsonType.Object:
            {
                BsonObject bsonObject = (BsonObject)t;
                this._writer.Write(bsonObject.CalculatedSize);
                using (IEnumerator <BsonProperty> enumerator = bsonObject.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        BsonProperty current = enumerator.get_Current();
                        this._writer.Write((sbyte)current.Value.Type);
                        this.WriteString((string)current.Name.Value, current.Name.ByteCount, default(int?));
                        this.WriteTokenInternal(current.Value);
                    }
                }
                this._writer.Write(0);
                return;
            }

            case BsonType.Array:
            {
                BsonArray bsonArray = (BsonArray)t;
                this._writer.Write(bsonArray.CalculatedSize);
                int num = 0;
                using (IEnumerator <BsonToken> enumerator2 = bsonArray.GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        BsonToken current2 = enumerator2.get_Current();
                        this._writer.Write((sbyte)current2.Type);
                        this.WriteString(num.ToString(CultureInfo.get_InvariantCulture()), MathUtils.IntLength(num), default(int?));
                        this.WriteTokenInternal(current2);
                        num++;
                    }
                }
                this._writer.Write(0);
                return;
            }

            case BsonType.Binary:
            {
                BsonValue bsonValue2 = (BsonValue)t;
                byte[]    array      = (byte[])bsonValue2.Value;
                this._writer.Write(array.Length);
                this._writer.Write(0);
                this._writer.Write(array);
                return;
            }

            case BsonType.Undefined:
            case BsonType.Null:
                return;

            case BsonType.Oid:
            {
                BsonValue bsonValue3 = (BsonValue)t;
                byte[]    array2     = (byte[])bsonValue3.Value;
                this._writer.Write(array2);
                return;
            }

            case BsonType.Boolean:
            {
                BsonValue bsonValue4 = (BsonValue)t;
                this._writer.Write((bool)bsonValue4.Value);
                return;
            }

            case BsonType.Date:
            {
                BsonValue bsonValue5 = (BsonValue)t;
                long      num2;
                if (bsonValue5.Value is DateTime)
                {
                    DateTime dateTime = (DateTime)bsonValue5.Value;
                    if (this.DateTimeKindHandling == 1)
                    {
                        dateTime = dateTime.ToUniversalTime();
                    }
                    else if (this.DateTimeKindHandling == 2)
                    {
                        dateTime = dateTime.ToLocalTime();
                    }
                    num2 = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTime, false);
                }
                else
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)bsonValue5.Value;
                    num2 = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.get_UtcDateTime(), dateTimeOffset.get_Offset());
                }
                this._writer.Write(num2);
                return;
            }

            case BsonType.Regex:
            {
                BsonRegex bsonRegex = (BsonRegex)t;
                this.WriteString((string)bsonRegex.Pattern.Value, bsonRegex.Pattern.ByteCount, default(int?));
                this.WriteString((string)bsonRegex.Options.Value, bsonRegex.Options.ByteCount, default(int?));
                return;
            }

            case BsonType.Integer:
            {
                BsonValue bsonValue6 = (BsonValue)t;
                this._writer.Write(Convert.ToInt32(bsonValue6.Value, CultureInfo.get_InvariantCulture()));
                return;
            }

            case BsonType.Long:
            {
                BsonValue bsonValue7 = (BsonValue)t;
                this._writer.Write(Convert.ToInt64(bsonValue7.Value, CultureInfo.get_InvariantCulture()));
                return;
            }
            }
            throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
            {
                t.Type
            }));
        }