/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing property value of the JSON that is being converted.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { #if !NET20 Type t = (ReflectionUtils.IsNullableType(objectType)) ? Nullable.GetUnderlyingType(objectType) : objectType; #endif if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullable(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); } return(null); } if (reader.TokenType != JsonToken.StartConstructor || !string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal)) { throw JsonSerializationException.Create(reader, "Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value)); } reader.Read(); if (reader.TokenType != JsonToken.Integer) { throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } long ticks = (long)reader.Value; DateTime d = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks); reader.Read(); if (reader.TokenType != JsonToken.EndConstructor) { throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } #if !NET20 if (t == typeof(DateTimeOffset)) { return(new DateTimeOffset(d)); } #endif return(d); }
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullable(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)objectType)); } return((object)null); } if (reader.TokenType != JsonToken.StartConstructor || !string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal)) { throw JsonSerializationException.Create(reader, "Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith( (IFormatProvider)CultureInfo.InvariantCulture, (object)reader.TokenType, reader.Value)); } reader.Read(); if (reader.TokenType != JsonToken.Integer) { throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected Integer, got {0}.".FormatWith( (IFormatProvider)CultureInfo.InvariantCulture, (object)reader.TokenType)); } DateTime dateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime((long)reader.Value); reader.Read(); if (reader.TokenType != JsonToken.EndConstructor) { throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith( (IFormatProvider)CultureInfo.InvariantCulture, (object)reader.TokenType)); } if (ReflectionUtils.IsNullableType(objectType)) { Nullable.GetUnderlyingType(objectType); } return((object)dateTime); }
public static DateTime ParseDateMicrosoft(string text) { string value = text.Substring(6, text.Length - 8); int index = value.IndexOf('+', 1); if (index == -1) { index = value.IndexOf('-', 1); } if (index != -1) { value = value.Substring(0, index); } long javaScriptTicks = long.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture); DateTime utcDateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(javaScriptTicks); return(utcDateTime); }
private void WriteConstructorDate(JsonReader reader) { if (!reader.Read()) { throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); } if (reader.TokenType != JsonToken.Integer) { throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + reader.TokenType, null); } DateTime time = DateTimeUtils.ConvertJavaScriptTicksToDateTime((long)reader.Value); if (!reader.Read()) { throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); } if (reader.TokenType != JsonToken.EndConstructor) { throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + reader.TokenType, null); } this.WriteValue(time); }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: double d = ReadDouble(); if (_floatParseHandling == FloatParseHandling.Decimal) { SetToken(JsonToken.Float, Convert.ToDecimal(d, CultureInfo.InvariantCulture)); } else { SetToken(JsonToken.Float, d); } break; case BsonType.String: case BsonType.Symbol: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.Object: { SetToken(JsonToken.StartObject); ContainerContext newContext = new ContainerContext(BsonType.Object); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Array: { SetToken(JsonToken.StartArray); ContainerContext newContext = new ContainerContext(BsonType.Array); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Binary: SetToken(JsonToken.Bytes, ReadBinary()); break; case BsonType.Undefined: SetToken(JsonToken.Undefined); break; case BsonType.Oid: byte[] oid = ReadBytes(12); SetToken(JsonToken.Bytes, oid); break; case BsonType.Boolean: bool b = Convert.ToBoolean(ReadByte()); SetToken(JsonToken.Boolean, b); break; case BsonType.Date: long ticks = ReadInt64(); DateTime utcDateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks); DateTime dateTime; switch (DateTimeKindHandling) { case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified); break; case DateTimeKind.Local: dateTime = utcDateTime.ToLocalTime(); break; default: dateTime = utcDateTime; break; } SetToken(JsonToken.Date, dateTime); break; case BsonType.Null: SetToken(JsonToken.Null); break; case BsonType.Regex: string expression = ReadString(); string modifiers = ReadString(); string regex = @"/" + expression + @"/" + modifiers; SetToken(JsonToken.String, regex); break; case BsonType.Reference: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.ReferenceStart; break; case BsonType.Code: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.CodeWScope: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.CodeWScopeStart; break; case BsonType.Integer: SetToken(JsonToken.Integer, (long)ReadInt32()); break; case BsonType.TimeStamp: case BsonType.Long: SetToken(JsonToken.Integer, ReadInt64()); break; default: throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type); } }
private void ReadType(BsonType type) { BsonBinaryType bsonBinaryType; DateTime dateTime; object guid; switch (type) { case BsonType.Number: { double num = this.ReadDouble(); if (this._floatParseHandling != Newtonsoft.Json.FloatParseHandling.Decimal) { base.SetToken(JsonToken.Float, num); return; } base.SetToken(JsonToken.Float, Convert.ToDecimal(num, CultureInfo.InvariantCulture)); return; } case BsonType.String: case BsonType.Symbol: { base.SetToken(JsonToken.String, this.ReadLengthString()); return; } case BsonType.Object: { base.SetToken(JsonToken.StartObject); BsonReader.ContainerContext containerContext = new BsonReader.ContainerContext(BsonType.Object); this.PushContext(containerContext); containerContext.Length = this.ReadInt32(); return; } case BsonType.Array: { base.SetToken(JsonToken.StartArray); BsonReader.ContainerContext containerContext1 = new BsonReader.ContainerContext(BsonType.Array); this.PushContext(containerContext1); containerContext1.Length = this.ReadInt32(); return; } case BsonType.Binary: { byte[] numArray = this.ReadBinary(out bsonBinaryType); if (bsonBinaryType != BsonBinaryType.Uuid) { guid = numArray; } else { guid = new Guid(numArray); } base.SetToken(JsonToken.Bytes, guid); return; } case BsonType.Undefined: { base.SetToken(JsonToken.Undefined); return; } case BsonType.Oid: { base.SetToken(JsonToken.Bytes, this.ReadBytes(12)); return; } case BsonType.Boolean: { bool flag = Convert.ToBoolean(this.ReadByte()); base.SetToken(JsonToken.Boolean, flag); return; } case BsonType.Date: { DateTime dateTime1 = DateTimeUtils.ConvertJavaScriptTicksToDateTime(this.ReadInt64()); DateTimeKind dateTimeKindHandling = this.DateTimeKindHandling; if (dateTimeKindHandling == DateTimeKind.Unspecified) { dateTime = DateTime.SpecifyKind(dateTime1, DateTimeKind.Unspecified); } else { dateTime = (dateTimeKindHandling == DateTimeKind.Local ? dateTime1.ToLocalTime() : dateTime1); } base.SetToken(JsonToken.Date, dateTime); return; } case BsonType.Null: { base.SetToken(JsonToken.Null); return; } case BsonType.Regex: { string str = this.ReadString(); string str1 = this.ReadString(); string str2 = string.Concat("/", str, "/", str1); base.SetToken(JsonToken.String, str2); return; } case BsonType.Reference: { base.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReader.BsonReaderState.ReferenceStart; return; } case BsonType.Code: { base.SetToken(JsonToken.String, this.ReadLengthString()); return; } case BsonType.CodeWScope: { base.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeStart; return; } case BsonType.Integer: { base.SetToken(JsonToken.Integer, (long)this.ReadInt32()); return; } case BsonType.TimeStamp: case BsonType.Long: { base.SetToken(JsonToken.Integer, this.ReadInt64()); return; } } throw new ArgumentOutOfRangeException("type", string.Concat("Unexpected BsonType value: ", type)); }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: { double num = this.ReadDouble(); if (this._floatParseHandling == FloatParseHandling.Decimal) { base.SetToken(JsonToken.Float, Convert.ToDecimal(num, CultureInfo.InvariantCulture)); return; } base.SetToken(JsonToken.Float, num); return; } case BsonType.String: case BsonType.Symbol: base.SetToken(JsonToken.String, this.ReadLengthString()); return; case BsonType.Object: { base.SetToken(JsonToken.StartObject); BsonReader.ContainerContext containerContext = new BsonReader.ContainerContext(BsonType.Object); this.PushContext(containerContext); containerContext.Length = this.ReadInt32(); return; } case BsonType.Array: { base.SetToken(JsonToken.StartArray); BsonReader.ContainerContext containerContext2 = new BsonReader.ContainerContext(BsonType.Array); this.PushContext(containerContext2); containerContext2.Length = this.ReadInt32(); return; } case BsonType.Binary: base.SetToken(JsonToken.Bytes, this.ReadBinary()); return; case BsonType.Undefined: base.SetToken(JsonToken.Undefined); return; case BsonType.Oid: { byte[] value = this.ReadBytes(12); base.SetToken(JsonToken.Bytes, value); return; } case BsonType.Boolean: { bool flag = Convert.ToBoolean(this.ReadByte()); base.SetToken(JsonToken.Boolean, flag); return; } case BsonType.Date: { long javaScriptTicks = this.ReadInt64(); DateTime dateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(javaScriptTicks); DateTime dateTime2; switch (this.DateTimeKindHandling) { case DateTimeKind.Unspecified: dateTime2 = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified); goto IL_184; case DateTimeKind.Local: dateTime2 = dateTime.ToLocalTime(); goto IL_184; } dateTime2 = dateTime; IL_184: base.SetToken(JsonToken.Date, dateTime2); return; } case BsonType.Null: base.SetToken(JsonToken.Null); return; case BsonType.Regex: { string str = this.ReadString(); string str2 = this.ReadString(); string value2 = "/" + str + "/" + str2; base.SetToken(JsonToken.String, value2); return; } case BsonType.Reference: base.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReader.BsonReaderState.ReferenceStart; return; case BsonType.Code: base.SetToken(JsonToken.String, this.ReadLengthString()); return; case BsonType.CodeWScope: base.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeStart; return; case BsonType.Integer: base.SetToken(JsonToken.Integer, (long)this.ReadInt32()); return; case BsonType.TimeStamp: case BsonType.Long: base.SetToken(JsonToken.Integer, this.ReadInt64()); return; default: throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type); } }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: double num = this.ReadDouble(); if (this._floatParseHandling == FloatParseHandling.Decimal) { this.SetToken(JsonToken.Float, (object)Convert.ToDecimal((object)num, (IFormatProvider)CultureInfo.InvariantCulture)); break; } this.SetToken(JsonToken.Float, (object)num); break; case BsonType.String: case BsonType.Symbol: this.SetToken(JsonToken.String, (object)this.ReadLengthString()); break; case BsonType.Object: this.SetToken(JsonToken.StartObject); BsonReader.ContainerContext newContext1 = new BsonReader.ContainerContext(BsonType.Object); this.PushContext(newContext1); newContext1.Length = this.ReadInt32(); break; case BsonType.Array: this.SetToken(JsonToken.StartArray); BsonReader.ContainerContext newContext2 = new BsonReader.ContainerContext(BsonType.Array); this.PushContext(newContext2); newContext2.Length = this.ReadInt32(); break; case BsonType.Binary: BsonBinaryType binaryType; byte[] b = this.ReadBinary(out binaryType); this.SetToken(JsonToken.Bytes, binaryType != BsonBinaryType.Uuid ? (object)b : (object)new Guid(b)); break; case BsonType.Undefined: this.SetToken(JsonToken.Undefined); break; case BsonType.Oid: this.SetToken(JsonToken.Bytes, (object)this.ReadBytes(12)); break; case BsonType.Boolean: this.SetToken(JsonToken.Boolean, (object)Convert.ToBoolean(this.ReadByte())); break; case BsonType.Date: DateTime dateTime1 = DateTimeUtils.ConvertJavaScriptTicksToDateTime(this.ReadInt64()); DateTime dateTime2; switch (this.DateTimeKindHandling) { case DateTimeKind.Unspecified: dateTime2 = DateTime.SpecifyKind(dateTime1, DateTimeKind.Unspecified); break; case DateTimeKind.Local: dateTime2 = dateTime1.ToLocalTime(); break; default: dateTime2 = dateTime1; break; } this.SetToken(JsonToken.Date, (object)dateTime2); break; case BsonType.Null: this.SetToken(JsonToken.Null); break; case BsonType.Regex: this.SetToken(JsonToken.String, (object)("/" + this.ReadString() + "/" + this.ReadString())); break; case BsonType.Reference: this.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReader.BsonReaderState.ReferenceStart; break; case BsonType.Code: this.SetToken(JsonToken.String, (object)this.ReadLengthString()); break; case BsonType.CodeWScope: this.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeStart; break; case BsonType.Integer: this.SetToken(JsonToken.Integer, (object)(long)this.ReadInt32()); break; case BsonType.TimeStamp: case BsonType.Long: this.SetToken(JsonToken.Integer, (object)this.ReadInt64()); break; default: throw new ArgumentOutOfRangeException(nameof(type), "Unexpected BsonType value: " + (object)type); } }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: double d = ReadDouble(); SetFloatToken(d); break; case BsonType.String: case BsonType.Symbol: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.Object: { SetToken(JsonToken.StartObject); ContainerContext newContext = new ContainerContext(BsonType.Object); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Array: { SetToken(JsonToken.StartArray); ContainerContext newContext = new ContainerContext(BsonType.Array); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Binary: BsonBinaryType binaryType; byte[] data = ReadBinary(out binaryType); object value = (binaryType != BsonBinaryType.Uuid) ? data : (object)new Guid(data); SetToken(JsonToken.Bytes, value); break; case BsonType.Undefined: SetToken(JsonToken.Undefined); break; case BsonType.Oid: byte[] oid = ReadBytes(12); SetToken(JsonToken.Bytes, oid); break; case BsonType.Boolean: bool b = Convert.ToBoolean(ReadByte()); SetBoolToken(b); break; case BsonType.Date: long ticks = ReadInt64(); DateTime utcDateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks); DateTime dateTime; switch (DateTimeKindHandling) { case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified); break; case DateTimeKind.Local: dateTime = utcDateTime.ToLocalTime(); break; default: dateTime = utcDateTime; break; } SetToken(JsonToken.Date, dateTime); break; case BsonType.Null: SetToken(JsonToken.Null); break; case BsonType.Regex: string expression = ReadString(); string modifiers = ReadString(); string regex = @"/" + expression + @"/" + modifiers; SetToken(JsonToken.String, regex); break; case BsonType.Reference: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.ReferenceStart; break; case BsonType.Code: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.CodeWScope: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.CodeWScopeStart; break; case BsonType.Integer: SetIntToken((long)ReadInt32()); break; case BsonType.TimeStamp: case BsonType.Long: SetIntToken(ReadInt64()); break; default: throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type); } }
private void ReadType(BsonType type) { DateTime time2; switch (type) { case BsonType.Number: { double num = this.ReadDouble(); if (base._floatParseHandling != FloatParseHandling.Decimal) { base.SetToken(JsonToken.Float, num); return; } base.SetToken(JsonToken.Float, Convert.ToDecimal(num, CultureInfo.InvariantCulture)); return; } case BsonType.String: case BsonType.Symbol: base.SetToken(JsonToken.String, this.ReadLengthString()); return; case BsonType.Object: { base.SetToken(JsonToken.StartObject); ContainerContext newContext = new ContainerContext(BsonType.Object); this.PushContext(newContext); newContext.Length = this.ReadInt32(); return; } case BsonType.Array: { base.SetToken(JsonToken.StartArray); ContainerContext newContext = new ContainerContext(BsonType.Array); this.PushContext(newContext); newContext.Length = this.ReadInt32(); return; } case BsonType.Binary: { byte[] b = this.ReadBinary(out BsonBinaryType type2); object obj2 = (type2 != BsonBinaryType.Uuid) ? ((object)b) : ((object)new Guid(b)); base.SetToken(JsonToken.Bytes, obj2); return; } case BsonType.Undefined: base.SetToken(JsonToken.Undefined); return; case BsonType.Oid: { byte[] buffer2 = this.ReadBytes(12); base.SetToken(JsonToken.Bytes, buffer2); return; } case BsonType.Boolean: { bool flag = Convert.ToBoolean(this.ReadByte()); base.SetToken(JsonToken.Boolean, flag); return; } case BsonType.Date: { DateTime time = DateTimeUtils.ConvertJavaScriptTicksToDateTime(this.ReadInt64()); switch (this.DateTimeKindHandling) { case DateTimeKind.Unspecified: time2 = DateTime.SpecifyKind(time, DateTimeKind.Unspecified); goto Label_019C; case DateTimeKind.Local: time2 = time.ToLocalTime(); goto Label_019C; } time2 = time; break; } case BsonType.Null: base.SetToken(JsonToken.Null); return; case BsonType.Regex: { string str = this.ReadString(); string str2 = this.ReadString(); string str3 = "/" + str + "/" + str2; base.SetToken(JsonToken.String, str3); return; } case BsonType.Reference: base.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReaderState.ReferenceStart; return; case BsonType.Code: base.SetToken(JsonToken.String, this.ReadLengthString()); return; case BsonType.CodeWScope: base.SetToken(JsonToken.StartObject); this._bsonReaderState = BsonReaderState.CodeWScopeStart; return; case BsonType.Integer: base.SetToken(JsonToken.Integer, (long)this.ReadInt32()); return; case BsonType.TimeStamp: case BsonType.Long: base.SetToken(JsonToken.Integer, this.ReadInt64()); return; default: throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type); } Label_019C: base.SetToken(JsonToken.Date, time2); }