/// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer)
        {
            long ticks;

            if (value is DateTime dateTime)
            {
                DateTime utcDateTime = dateTime.ToUniversalTime();
                ticks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(utcDateTime);
            }
#if HAVE_DATE_TIME_OFFSET
            else if (value is DateTimeOffset dateTimeOffset)
            {
                DateTimeOffset utcDateTimeOffset = dateTimeOffset.ToUniversalTime();
                ticks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(utcDateTimeOffset.UtcDateTime);
            }
#endif
            else
            {
                throw new JsonSerializationException("Expected date object value.");
            }

            writer.WriteStartConstructor("Date");
            writer.WriteValue(ticks);
            writer.WriteEndConstructor();
        }
Beispiel #2
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            long ticks;

            if (value is DateTime)
            {
                var dateTime    = (DateTime)value;
                var utcDateTime = dateTime.ToUniversalTime();
                ticks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(utcDateTime);
            }
            else if (value is DateTimeOffset)
            {
                var dateTimeOffset    = (DateTimeOffset)value;
                var utcDateTimeOffset = dateTimeOffset.ToUniversalTime();
                ticks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(utcDateTimeOffset.UtcDateTime);
            }
            else
            {
                throw new JsonSerializationException("Expected date object value.");
            }

            writer.WriteStartConstructor("Date");
            writer.WriteValue(ticks);
            writer.WriteEndConstructor();
        }
Beispiel #3
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            long   javaScriptTicks;
            object obj  = value;
            object obj1 = obj;

            if (!(obj is DateTime))
            {
                object obj2 = value;
                obj1 = obj2;
                if (!(obj2 is DateTimeOffset))
                {
                    throw new JsonSerializationException("Expected date object value.");
                }
                DateTimeOffset universalTime = ((DateTimeOffset)obj1).ToUniversalTime();
                javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(universalTime.UtcDateTime);
            }
            else
            {
                javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(((DateTime)obj1).ToUniversalTime());
            }
            writer.WriteStartConstructor("Date");
            writer.WriteValue(javaScriptTicks);
            writer.WriteEndConstructor();
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (!(value is DateTime))
            {
                throw new JsonSerializationException("Expected date object value.");
            }
            long javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(((DateTime)value).ToUniversalTime());

            writer.WriteStartConstructor("Date");
            writer.WriteValue(javaScriptTicks);
            writer.WriteEndConstructor();
        }
        public void ReadOffsetMSDateTimeOffset()
        {
            char[]          c         = @"12345/Date(1418924498000+0800)/12345".ToCharArray();
            StringReference reference = new StringReference(c, 5, c.Length - 10);

            DateTimeOffset d;

            DateTimeUtils.TryParseDateTimeOffset(reference, null, CultureInfo.InvariantCulture, out d);

            long initialTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(d.DateTime, d.Offset);

            Assert.AreEqual(1418924498000, initialTicks);
            Assert.AreEqual(8, d.Offset.Hours);
        }
		// Token: 0x060014BC RID: 5308 RVA: 0x0006E30C File Offset: 0x0006C50C
		public override void WriteJson(JsonWriter writer, [Nullable(2)] object value, JsonSerializer serializer)
		{
			long value2;
			if (value is DateTime)
			{
				value2 = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(((DateTime)value).ToUniversalTime());
			}
			else
			{
				if (!(value is DateTimeOffset))
				{
					throw new JsonSerializationException("Expected date object value.");
				}
				value2 = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(((DateTimeOffset)value).ToUniversalTime().UtcDateTime);
			}
			writer.WriteStartConstructor("Date");
			writer.WriteValue(value2);
			writer.WriteEndConstructor();
		}
        /// <summary>Writes the JSON representation of the object.</summary>
        /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            long javaScriptTicks;

            switch (value)
            {
            case DateTime dateTime:
                javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTime.ToUniversalTime());
                break;

            case DateTimeOffset dateTimeOffset:
                javaScriptTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.ToUniversalTime().UtcDateTime);
                break;

            default:
                throw new JsonSerializationException("Expected date object value.");
            }
            writer.WriteStartConstructor("Date");
            writer.WriteValue(javaScriptTicks);
            writer.WriteEndConstructor();
        }
Beispiel #8
0
        private long TicksFromDateObject(object value)
        {
#if HAVE_DATE_TIME_OFFSET
            if (value is DateTimeOffset)
            {
                DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
                return(DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset));
            }
#endif
            DateTime dateTime = (DateTime)value;
            if (DateTimeKindHandling == DateTimeKind.Utc)
            {
                dateTime = dateTime.ToUniversalTime();
            }
            else if (DateTimeKindHandling == DateTimeKind.Local)
            {
                dateTime = dateTime.ToLocalTime();
            }

            return(DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTime, false));
        }
        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:
            {
                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 = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTime, false);
                }
                else
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)value.Value;
                    ticks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
                }

                _writer.Write(ticks);
            }
            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("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
            }
        }
Beispiel #10
0
        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(t == BsonBoolean.True);
                break;

            case BsonType.Date:
                BsonValue bsonValue = (BsonValue)t;
                long      num       = 0;
                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();
                    }
                    num = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTime, false);
                }
                this._writer.Write(num);
                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));
            }
        }
Beispiel #11
0
        public void WriteDateTime()
        {
            DateTimeResult result = null;

            result = TestDateTime("DateTime Max", DateTime.MaxValue);
            Assert.AreEqual("9999-12-31T23:59:59.9999999", result.IsoDateRoundtrip);
            Assert.AreEqual("9999-12-31T23:59:59.9999999" + GetOffset(DateTime.MaxValue, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("9999-12-31T23:59:59.9999999", result.IsoDateUnspecified);
            Assert.AreEqual("9999-12-31T23:59:59.9999999Z", result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(253402300799999)\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(253402300799999" + GetOffset(DateTime.MaxValue, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(253402300799999)\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(253402300799999)\/", result.MsDateUtc);

            DateTime year2000local  = new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Local);
            string   localToUtcDate = year2000local.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK");

            result = TestDateTime("DateTime Local", year2000local);
            Assert.AreEqual("2000-01-01T01:01:01" + GetOffset(year2000local, DateFormatHandling.IsoDateFormat), result.IsoDateRoundtrip);
            Assert.AreEqual("2000-01-01T01:01:01" + GetOffset(year2000local, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("2000-01-01T01:01:01", result.IsoDateUnspecified);
            Assert.AreEqual(localToUtcDate, result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000local) + GetOffset(year2000local, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000local) + GetOffset(year2000local, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000local) + GetOffset(year2000local, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000local) + @")\/", result.MsDateUtc);

            DateTime millisecondsLocal = new DateTime(2000, 1, 1, 1, 1, 1, 999, DateTimeKind.Local);

            localToUtcDate = millisecondsLocal.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK");

            result = TestDateTime("DateTime Local with milliseconds", millisecondsLocal);
            Assert.AreEqual("2000-01-01T01:01:01.999" + GetOffset(millisecondsLocal, DateFormatHandling.IsoDateFormat), result.IsoDateRoundtrip);
            Assert.AreEqual("2000-01-01T01:01:01.999" + GetOffset(millisecondsLocal, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("2000-01-01T01:01:01.999", result.IsoDateUnspecified);
            Assert.AreEqual(localToUtcDate, result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(millisecondsLocal) + GetOffset(millisecondsLocal, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(millisecondsLocal) + GetOffset(millisecondsLocal, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(millisecondsLocal) + GetOffset(millisecondsLocal, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(millisecondsLocal) + @")\/", result.MsDateUtc);

            DateTime ticksLocal = new DateTime(634663873826822481, DateTimeKind.Local);

            localToUtcDate = ticksLocal.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK");

            result = TestDateTime("DateTime Local with ticks", ticksLocal);
            Assert.AreEqual("2012-03-03T16:03:02.6822481" + GetOffset(ticksLocal, DateFormatHandling.IsoDateFormat), result.IsoDateRoundtrip);
            Assert.AreEqual("2012-03-03T16:03:02.6822481" + GetOffset(ticksLocal, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("2012-03-03T16:03:02.6822481", result.IsoDateUnspecified);
            Assert.AreEqual(localToUtcDate, result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(ticksLocal) + GetOffset(ticksLocal, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(ticksLocal) + GetOffset(ticksLocal, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(ticksLocal) + GetOffset(ticksLocal, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(ticksLocal) + @")\/", result.MsDateUtc);

            DateTime year2000Unspecified = new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified);

            result = TestDateTime("DateTime Unspecified", year2000Unspecified);
            Assert.AreEqual("2000-01-01T01:01:01", result.IsoDateRoundtrip);
            Assert.AreEqual("2000-01-01T01:01:01" + GetOffset(year2000Unspecified, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("2000-01-01T01:01:01", result.IsoDateUnspecified);
            Assert.AreEqual("2000-01-01T01:01:01Z", result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000Unspecified) + GetOffset(year2000Unspecified, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000Unspecified) + GetOffset(year2000Unspecified, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000Unspecified) + GetOffset(year2000Unspecified, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(year2000Unspecified.ToLocalTime()) + @")\/", result.MsDateUtc);

            DateTime year2000Utc    = new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc);
            string   utcTolocalDate = year2000Utc.ToLocalTime().ToString("yyyy-MM-ddTHH:mm:ss");

            result = TestDateTime("DateTime Utc", year2000Utc);
            Assert.AreEqual("2000-01-01T01:01:01Z", result.IsoDateRoundtrip);
            Assert.AreEqual(utcTolocalDate + GetOffset(year2000Utc, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("2000-01-01T01:01:01", result.IsoDateUnspecified);
            Assert.AreEqual("2000-01-01T01:01:01Z", result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(946688461000)\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(946688461000" + GetOffset(year2000Utc, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(DateTime.SpecifyKind(year2000Utc, DateTimeKind.Unspecified)) + GetOffset(year2000Utc, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(946688461000)\/", result.MsDateUtc);

            DateTime unixEpoc = new DateTime(621355968000000000, DateTimeKind.Utc);

            utcTolocalDate = unixEpoc.ToLocalTime().ToString("yyyy-MM-ddTHH:mm:ss");

            result = TestDateTime("DateTime Unix Epoc", unixEpoc);
            Assert.AreEqual("1970-01-01T00:00:00Z", result.IsoDateRoundtrip);
            Assert.AreEqual(utcTolocalDate + GetOffset(unixEpoc, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("1970-01-01T00:00:00", result.IsoDateUnspecified);
            Assert.AreEqual("1970-01-01T00:00:00Z", result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(0)\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(0" + GetOffset(unixEpoc, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(" + DateTimeUtils.ConvertDateTimeToJavaScriptTicks(DateTime.SpecifyKind(unixEpoc, DateTimeKind.Unspecified)) + GetOffset(unixEpoc, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(0)\/", result.MsDateUtc);

            result = TestDateTime("DateTime Min", DateTime.MinValue);
            Assert.AreEqual("0001-01-01T00:00:00", result.IsoDateRoundtrip);
            Assert.AreEqual("0001-01-01T00:00:00" + GetOffset(DateTime.MinValue, DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("0001-01-01T00:00:00", result.IsoDateUnspecified);
            Assert.AreEqual("0001-01-01T00:00:00Z", result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(-62135596800000)\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(-62135596800000" + GetOffset(DateTime.MinValue, DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(-62135596800000)\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(-62135596800000)\/", result.MsDateUtc);

            result = TestDateTime("DateTime Default", default(DateTime));
            Assert.AreEqual("0001-01-01T00:00:00", result.IsoDateRoundtrip);
            Assert.AreEqual("0001-01-01T00:00:00" + GetOffset(default(DateTime), DateFormatHandling.IsoDateFormat), result.IsoDateLocal);
            Assert.AreEqual("0001-01-01T00:00:00", result.IsoDateUnspecified);
            Assert.AreEqual("0001-01-01T00:00:00Z", result.IsoDateUtc);
            Assert.AreEqual(@"\/Date(-62135596800000)\/", result.MsDateRoundtrip);
            Assert.AreEqual(@"\/Date(-62135596800000" + GetOffset(default(DateTime), DateFormatHandling.MicrosoftDateFormat) + @")\/", result.MsDateLocal);
            Assert.AreEqual(@"\/Date(-62135596800000)\/", result.MsDateUnspecified);
            Assert.AreEqual(@"\/Date(-62135596800000)\/", result.MsDateUtc);

#if !NET20
            result = TestDateTime("DateTimeOffset TimeSpan Zero", new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));
            Assert.AreEqual("2000-01-01T01:01:01+00:00", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(946688461000+0000)\/", result.MsDateRoundtrip);

            result = TestDateTime("DateTimeOffset TimeSpan 1 hour", new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.FromHours(1)));
            Assert.AreEqual("2000-01-01T01:01:01+01:00", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(946684861000+0100)\/", result.MsDateRoundtrip);

            result = TestDateTime("DateTimeOffset TimeSpan 1.5 hour", new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.FromHours(1.5)));
            Assert.AreEqual("2000-01-01T01:01:01+01:30", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(946683061000+0130)\/", result.MsDateRoundtrip);

            result = TestDateTime("DateTimeOffset TimeSpan 13 hour", new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.FromHours(13)));
            Assert.AreEqual("2000-01-01T01:01:01+13:00", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(946641661000+1300)\/", result.MsDateRoundtrip);

            result = TestDateTime("DateTimeOffset TimeSpan with ticks", new DateTimeOffset(634663873826822481, TimeSpan.Zero));
            Assert.AreEqual("2012-03-03T16:03:02.6822481+00:00", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(1330790582682+0000)\/", result.MsDateRoundtrip);

            result = TestDateTime("DateTimeOffset Min", DateTimeOffset.MinValue);
            Assert.AreEqual("0001-01-01T00:00:00+00:00", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(-62135596800000+0000)\/", result.MsDateRoundtrip);

            result = TestDateTime("DateTimeOffset Max", DateTimeOffset.MaxValue);
            Assert.AreEqual("9999-12-31T23:59:59.9999999+00:00", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(253402300799999+0000)\/", result.MsDateRoundtrip);

            result = TestDateTime("DateTimeOffset Default", default(DateTimeOffset));
            Assert.AreEqual("0001-01-01T00:00:00+00:00", result.IsoDateRoundtrip);
            Assert.AreEqual(@"\/Date(-62135596800000+0000)\/", result.MsDateRoundtrip);
#endif
        }
Beispiel #12
0
        // Token: 0x060015B7 RID: 5559 RVA: 0x00071E94 File Offset: 0x00070094
        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);
                ulong num = 0UL;
                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 += 1UL;
                }
                this._writer.Write(0);
                return;
            }

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

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

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

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

            case BsonType.Date:
            {
                BsonValue bsonValue2 = (BsonValue)t;
                object    value      = bsonValue2.Value;
                long      value2;
                if (value is DateTime)
                {
                    DateTime dateTime = (DateTime)value;
                    if (this.DateTimeKindHandling == DateTimeKind.Utc)
                    {
                        dateTime = dateTime.ToUniversalTime();
                    }
                    else if (this.DateTimeKindHandling == DateTimeKind.Local)
                    {
                        dateTime = dateTime.ToLocalTime();
                    }
                    value2 = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTime, false);
                }
                else
                {
                    DateTimeOffset dateTimeOffset = (DateTimeOffset)bsonValue2.Value;
                    value2 = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
                }
                this._writer.Write(value2);
                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 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;
            }
            }
            throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
        }
Beispiel #13
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));
            }
            }
        }
        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);
        }