// public methods /// <summary> /// Deserializes a value. /// </summary> /// <param name="context">The deserialization context.</param> /// <param name="args">The deserialization args.</param> /// <returns>An object.</returns> public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var bsonReader = context.Reader; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: if (context.DynamicArraySerializer != null) { return(context.DynamicArraySerializer.Deserialize(context)); } goto default; case BsonType.Binary: var binaryData = bsonReader.ReadBinaryData(); var subType = binaryData.SubType; if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { return(binaryData.ToGuid()); } goto default; case BsonType.Boolean: return(bsonReader.ReadBoolean()); case BsonType.DateTime: var millisecondsSinceEpoch = bsonReader.ReadDateTime(); var bsonDateTime = new BsonDateTime(millisecondsSinceEpoch); return(bsonDateTime.ToUniversalTime()); case BsonType.Document: return(DeserializeDiscriminatedValue(context, args)); case BsonType.Double: return(bsonReader.ReadDouble()); case BsonType.Int32: return(bsonReader.ReadInt32()); case BsonType.Int64: return(bsonReader.ReadInt64()); case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.ObjectId: return(bsonReader.ReadObjectId()); case BsonType.String: return(bsonReader.ReadString()); default: var message = string.Format("ObjectSerializer does not support BSON type '{0}'.", bsonType); throw new FormatException(message); } }
public void TestAsUniversalTime() { var utcNow = DateTime.UtcNow; var utcNowTruncated = utcNow.AddTicks(-(utcNow.Ticks % 10000)); var bsonDateTime = new BsonDateTime(utcNow); var utcDateTime = bsonDateTime.ToUniversalTime(); Assert.AreEqual(DateTimeKind.Utc, utcDateTime.Kind); Assert.AreEqual(utcNowTruncated, utcDateTime); }
public IEnumerable <IEvent> GetByDay(BsonDateTime date) { try { var dateIntervalStart = date.ToUniversalTime().Date.AddDays(1); var dateIntervalEnd = date.ToUniversalTime().Date.AddDays(2); var builder = Builders <Models.Event.Event> .Filter; var filter = builder.Gte(e => e.StartDateTime, dateIntervalStart) & builder.Lte(e => e.StartDateTime, dateIntervalEnd); return(MongoContext.Collection.Find(filter).ToList()); } catch (Exception ex) { throw ex; } }
public void TestValues() { var maxValueTruncated = new DateTime(9999, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc); var tests = new object[][] { new object[] { long.MinValue, false, DateTime.MinValue }, new object[] { long.MinValue + 1, false, DateTime.MinValue }, new object[] { BsonConstants.DateTimeMinValueMillisecondsSinceEpoch - 1, false, DateTime.MinValue }, new object[] { BsonConstants.DateTimeMinValueMillisecondsSinceEpoch, true, DateTime.MinValue }, new object[] { BsonConstants.DateTimeMinValueMillisecondsSinceEpoch + 1, true, DateTime.MinValue.AddMilliseconds(1) }, new object[] { -1L, true, BsonConstants.UnixEpoch.AddMilliseconds(-1) }, new object[] { 0L, true, BsonConstants.UnixEpoch }, new object[] { 1L, true, BsonConstants.UnixEpoch.AddMilliseconds(1) }, new object[] { BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch - 1, true, maxValueTruncated.AddMilliseconds(-1) }, new object[] { BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch, true, DateTime.MaxValue }, new object[] { BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch + 1, false, DateTime.MaxValue }, new object[] { long.MaxValue - 1, false, DateTime.MaxValue }, new object[] { long.MaxValue, false, DateTime.MaxValue }, }; foreach (var test in tests) { var millisecondsSinceEpoch = (long)test[0]; var expectedIsValidDateTime = (bool)test[1]; var expectedDateTime = (DateTime)test[2]; var bsonDateTime = new BsonDateTime(millisecondsSinceEpoch); if (expectedIsValidDateTime) { Assert.IsTrue(bsonDateTime.IsValidDateTime); var value = bsonDateTime.ToUniversalTime(); Assert.AreEqual(DateTimeKind.Utc, value.Kind); Assert.AreEqual(expectedDateTime, value); } else { Assert.IsFalse(bsonDateTime.IsValidDateTime); Assert.Throws <ArgumentOutOfRangeException>(() => bsonDateTime.ToUniversalTime()); } } }
public void TestDateTimeMinValue() { foreach (var kind in new[] { DateTimeKind.Local, DateTimeKind.Unspecified, DateTimeKind.Utc }) { var bsonDateTime = new BsonDateTime(DateTime.SpecifyKind(DateTime.MinValue, kind)); Assert.AreEqual(BsonConstants.DateTimeMinValueMillisecondsSinceEpoch, bsonDateTime.MillisecondsSinceEpoch); var utcDateTime = bsonDateTime.ToUniversalTime(); Assert.AreEqual(DateTimeKind.Utc, utcDateTime.Kind); Assert.AreEqual(DateTime.MinValue, utcDateTime); var localDateTime = bsonDateTime.ToLocalTime(); Assert.AreEqual(DateTimeKind.Local, localDateTime.Kind); Assert.AreEqual(DateTime.MinValue, localDateTime); } }
public IEnumerable <ObjectId> GetEventSpots(string city, BsonDateTime startDateTime, BsonDateTime endDateTime) { try { var builder = Builders <Models.Event.Event> .Filter; var filter = builder.Gte(e => e.StartDateTime, startDateTime.ToUniversalTime()) & builder.Lte(e => e.StartDateTime, endDateTime.ToUniversalTime()) & builder.Eq(e => e.City, city); return(MongoContext.Collection.Find(filter).Project(p => p.SpotId).ToList()); } catch (Exception ex) { throw ex; } }
// private methods private void ReadValue() { object jsonDotNetValue; switch (_wrappedReader.GetCurrentBsonType()) { case BsonType.Array: _wrappedReader.ReadStartArray(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartArray); return; case BsonType.Binary: var bsonBinaryData = _wrappedReader.ReadBinaryData(); switch (bsonBinaryData.SubType) { case BsonBinarySubType.UuidLegacy: var guidRepresentation = GuidRepresentation.Unspecified; var bsonReader = _wrappedReader as BsonReader; if (bsonReader != null) { guidRepresentation = bsonReader.Settings.GuidRepresentation; } jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, guidRepresentation); break; case BsonBinarySubType.UuidStandard: jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, GuidRepresentation.Standard); break; default: jsonDotNetValue = bsonBinaryData.Bytes; break; } SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, jsonDotNetValue, bsonBinaryData); return; case BsonType.Boolean: var booleanValue = _wrappedReader.ReadBoolean(); SetCurrentToken(Newtonsoft.Json.JsonToken.Boolean, booleanValue, (BsonBoolean)booleanValue); return; case BsonType.DateTime: var bsonDateTime = new BsonDateTime(_wrappedReader.ReadDateTime()); if (bsonDateTime.IsValidDateTime) { jsonDotNetValue = bsonDateTime.ToUniversalTime(); } else { jsonDotNetValue = bsonDateTime.MillisecondsSinceEpoch; } SetCurrentToken(Newtonsoft.Json.JsonToken.Date, jsonDotNetValue, bsonDateTime); return; case BsonType.Document: _wrappedReader.ReadStartDocument(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartObject); return; case BsonType.Double: var bsonDouble = new BsonDouble(_wrappedReader.ReadDouble()); switch (FloatParseHandling) { case Newtonsoft.Json.FloatParseHandling.Decimal: jsonDotNetValue = Convert.ToDecimal(bsonDouble); break; case Newtonsoft.Json.FloatParseHandling.Double: jsonDotNetValue = bsonDouble.Value; break; default: throw new NotSupportedException(string.Format("Unexpected FloatParseHandling value: {0}.", FloatParseHandling)); } SetCurrentToken(Newtonsoft.Json.JsonToken.Float, jsonDotNetValue, bsonDouble); return; case BsonType.Int32: var bsonInt32 = (BsonInt32)_wrappedReader.ReadInt32(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, (long)bsonInt32.Value, bsonInt32); return; case BsonType.Int64: var bsonInt64 = (BsonInt64)_wrappedReader.ReadInt64(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonInt64.Value, bsonInt64); return; case BsonType.JavaScript: { var code = _wrappedReader.ReadJavaScript(); var bsonJavaScript = new BsonJavaScript(code); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScript); } return; case BsonType.JavaScriptWithScope: { var code = _wrappedReader.ReadJavaScriptWithScope(); var context = BsonDeserializationContext.CreateRoot(_wrappedReader); var scope = BsonDocumentSerializer.Instance.Deserialize <BsonDocument>(context); var bsonJavaScriptWithScope = new BsonJavaScriptWithScope(code, scope); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScriptWithScope); } return; case BsonType.MaxKey: _wrappedReader.ReadMaxKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMaxKey.Value); return; case BsonType.MinKey: _wrappedReader.ReadMinKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMinKey.Value); return; case BsonType.Null: _wrappedReader.ReadNull(); SetCurrentToken(Newtonsoft.Json.JsonToken.Null, null, BsonNull.Value); return; case BsonType.ObjectId: var bsonObjectId = new BsonObjectId(_wrappedReader.ReadObjectId()); SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, bsonObjectId.Value.ToByteArray(), bsonObjectId); return; case BsonType.RegularExpression: var bsonRegularExpression = _wrappedReader.ReadRegularExpression(); var pattern = bsonRegularExpression.Pattern; var options = bsonRegularExpression.Options; jsonDotNetValue = "/" + pattern.Replace("/", "\\/") + "/" + options; SetCurrentToken(Newtonsoft.Json.JsonToken.String, jsonDotNetValue, bsonRegularExpression); return; case BsonType.String: var stringValue = _wrappedReader.ReadString(); SetCurrentToken(Newtonsoft.Json.JsonToken.String, stringValue, (BsonString)stringValue); return; case BsonType.Symbol: var bsonSymbol = BsonSymbolTable.Lookup(_wrappedReader.ReadSymbol()); SetCurrentToken(Newtonsoft.Json.JsonToken.String, bsonSymbol.Name, bsonSymbol); return; case BsonType.Timestamp: var bsonTimestamp = new BsonTimestamp(_wrappedReader.ReadTimestamp()); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonTimestamp.Value, bsonTimestamp); return; case BsonType.Undefined: _wrappedReader.ReadUndefined(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonUndefined.Value); return; default: var message = string.Format("Unexpected BsonType: {0}.", _wrappedReader.GetCurrentBsonType()); throw new Newtonsoft.Json.JsonReaderException(message); } }
// private methods private void ReadValue() { object jsonDotNetValue; switch (_wrappedReader.GetCurrentBsonType()) { case BsonType.Array: _wrappedReader.ReadStartArray(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartArray); return; case BsonType.Binary: var bsonBinaryData = _wrappedReader.ReadBinaryData(); switch (bsonBinaryData.SubType) { case BsonBinarySubType.UuidLegacy: var guidRepresentation = GuidRepresentation.Unspecified; var bsonReader = _wrappedReader as BsonReader; if (bsonReader != null) { guidRepresentation = bsonReader.Settings.GuidRepresentation; } jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, guidRepresentation); break; case BsonBinarySubType.UuidStandard: jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, GuidRepresentation.Standard); break; default: jsonDotNetValue = bsonBinaryData.Bytes; break; } SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, jsonDotNetValue, bsonBinaryData); return; case BsonType.Boolean: var booleanValue = _wrappedReader.ReadBoolean(); SetCurrentToken(Newtonsoft.Json.JsonToken.Boolean, booleanValue, (BsonBoolean)booleanValue); return; case BsonType.DateTime: var bsonDateTime = new BsonDateTime(_wrappedReader.ReadDateTime()); if (bsonDateTime.IsValidDateTime) { jsonDotNetValue = bsonDateTime.ToUniversalTime(); } else { jsonDotNetValue = bsonDateTime.MillisecondsSinceEpoch; } SetCurrentToken(Newtonsoft.Json.JsonToken.Date, jsonDotNetValue, bsonDateTime); return; case BsonType.Document: _wrappedReader.ReadStartDocument(); SetCurrentToken(Newtonsoft.Json.JsonToken.StartObject); return; case BsonType.Double: var bsonDouble = new BsonDouble(_wrappedReader.ReadDouble()); switch (FloatParseHandling) { case Newtonsoft.Json.FloatParseHandling.Decimal: jsonDotNetValue = Convert.ToDecimal(bsonDouble); break; case Newtonsoft.Json.FloatParseHandling.Double: jsonDotNetValue = bsonDouble.Value; break; default: throw new NotSupportedException(string.Format("Unexpected FloatParseHandling value: {0}.", FloatParseHandling)); } SetCurrentToken(Newtonsoft.Json.JsonToken.Float, jsonDotNetValue, bsonDouble); return; case BsonType.Int32: var bsonInt32 = (BsonInt32)_wrappedReader.ReadInt32(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, (long)bsonInt32.Value, bsonInt32); return; case BsonType.Int64: var bsonInt64 = (BsonInt64)_wrappedReader.ReadInt64(); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonInt64.Value, bsonInt64); return; case BsonType.JavaScript: { var code = _wrappedReader.ReadJavaScript(); var bsonJavaScript = new BsonJavaScript(code); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScript); } return; case BsonType.JavaScriptWithScope: { var code = _wrappedReader.ReadJavaScriptWithScope(); var context = BsonDeserializationContext.CreateRoot(_wrappedReader); var scope = BsonDocumentSerializer.Instance.Deserialize<BsonDocument>(context); var bsonJavaScriptWithScope = new BsonJavaScriptWithScope(code, scope); SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScriptWithScope); } return; case BsonType.MaxKey: _wrappedReader.ReadMaxKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMaxKey.Value); return; case BsonType.MinKey: _wrappedReader.ReadMinKey(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMinKey.Value); return; case BsonType.Null: _wrappedReader.ReadNull(); SetCurrentToken(Newtonsoft.Json.JsonToken.Null, null, BsonNull.Value); return; case BsonType.ObjectId: var bsonObjectId = new BsonObjectId(_wrappedReader.ReadObjectId()); SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, bsonObjectId.Value.ToByteArray(), bsonObjectId); return; case BsonType.RegularExpression: var bsonRegularExpression = _wrappedReader.ReadRegularExpression(); var pattern = bsonRegularExpression.Pattern; var options = bsonRegularExpression.Options; jsonDotNetValue = "/" + pattern.Replace("/", "\\/") + "/" + options; SetCurrentToken(Newtonsoft.Json.JsonToken.String, jsonDotNetValue, bsonRegularExpression); return; case BsonType.String: var stringValue = _wrappedReader.ReadString(); SetCurrentToken(Newtonsoft.Json.JsonToken.String, stringValue, (BsonString)stringValue); return; case BsonType.Symbol: var bsonSymbol = BsonSymbolTable.Lookup(_wrappedReader.ReadSymbol()); SetCurrentToken(Newtonsoft.Json.JsonToken.String, bsonSymbol.Name, bsonSymbol); return; case BsonType.Timestamp: var bsonTimestamp = new BsonTimestamp(_wrappedReader.ReadTimestamp()); SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonTimestamp.Value, bsonTimestamp); return; case BsonType.Undefined: _wrappedReader.ReadUndefined(); SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonUndefined.Value); return; default: var message = string.Format("Unexpected BsonType: {0}.", _wrappedReader.GetCurrentBsonType()); throw new Newtonsoft.Json.JsonReaderException(message); } }
public static Business.ReportDistribution.Model.ReportDistributionLogEntryCollection GetReportDistributionLogEntryCollectionGTETime(DateTime startTime) { Business.ReportDistribution.Model.ReportDistributionLogEntryCollection result = new ReportDistribution.Model.ReportDistributionLogEntryCollection(); YellowstonePathology.Business.Mongo.Server server = new Business.Mongo.TestServer(Business.Mongo.TestServer.LISDatabaseName); MongoCollection collection = server.Database.GetCollection <BsonDocument>("ReportDistributionLogEntry"); BsonDateTime bsonStartDate = BsonDateTime.Create(startTime); MongoCursor cursor = collection.FindAs <BsonDocument>(Query.GTE("Date", bsonStartDate.ToUniversalTime())); foreach (BsonDocument bsonDocument in cursor) { YellowstonePathology.Business.ReportDistribution.Model.ReportDistributionLogEntry reportDistributionLogEntry = new ReportDistribution.Model.ReportDistributionLogEntry(); YellowstonePathology.Business.Mongo.BSONPropertyWriter.Write(bsonDocument, reportDistributionLogEntry); result.Add(reportDistributionLogEntry); } return(result); }
public void TestValues() { var maxValueTruncated = new DateTime(9999, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc); var tests = new object[][] { new object[] { long.MinValue, false, DateTime.MinValue }, new object[] { long.MinValue + 1, false, DateTime.MinValue }, new object[] { BsonConstants.DateTimeMinValueMillisecondsSinceEpoch - 1, false, DateTime.MinValue }, new object[] { BsonConstants.DateTimeMinValueMillisecondsSinceEpoch, true, DateTime.MinValue }, new object[] { BsonConstants.DateTimeMinValueMillisecondsSinceEpoch + 1, true, DateTime.MinValue.AddMilliseconds(1) }, new object[] { -1L, true, BsonConstants.UnixEpoch.AddMilliseconds(-1) }, new object[] { 0L, true, BsonConstants.UnixEpoch }, new object[] { 1L, true, BsonConstants.UnixEpoch.AddMilliseconds(1) }, new object[] { BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch - 1, true, maxValueTruncated.AddMilliseconds(-1) }, new object[] { BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch, true, DateTime.MaxValue }, new object[] { BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch + 1, false, DateTime.MaxValue }, new object[] { long.MaxValue - 1, false, DateTime.MaxValue }, new object[] { long.MaxValue, false, DateTime.MaxValue }, }; foreach (var test in tests) { var millisecondsSinceEpoch = (long)test[0]; var expectedIsValidDateTime = (bool)test[1]; var expectedDateTime = (DateTime)test[2]; var bsonDateTime = new BsonDateTime(millisecondsSinceEpoch); if (expectedIsValidDateTime) { Assert.IsTrue(bsonDateTime.IsValidDateTime); var value = bsonDateTime.ToUniversalTime(); Assert.AreEqual(DateTimeKind.Utc, value.Kind); Assert.AreEqual(expectedDateTime, value); } else { Assert.IsFalse(bsonDateTime.IsValidDateTime); Assert.Throws<ArgumentOutOfRangeException>(() => { var value = bsonDateTime.ToUniversalTime(); }); } } }
public static DateTime GetUtcDate(BsonDateTime value) { return(value.ToUniversalTime()); }
// public methods /// <summary> /// Deserializes a value. /// </summary> /// <param name="context">The deserialization context.</param> /// <param name="args">The deserialization args.</param> /// <returns>A deserialized value.</returns> public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var bsonReader = context.Reader; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: if (context.DynamicArraySerializer != null) { return(context.DynamicArraySerializer.Deserialize(context)); } goto default; case BsonType.Binary: #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && _guidRepresentation == GuidRepresentation.Unspecified) { var binaryData = bsonReader.ReadBinaryData(); var subType = binaryData.SubType; if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { return(binaryData.ToGuid()); } } else { var binaryDataBookmark = bsonReader.GetBookmark(); var binaryData = bsonReader.ReadBinaryDataWithGuidRepresentationUnspecified(); var subType = binaryData.SubType; if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { bsonReader.ReturnToBookmark(binaryDataBookmark); return(_guidSerializer.Deserialize(context)); } } #pragma warning restore 618 goto default; case BsonType.Boolean: return(bsonReader.ReadBoolean()); case BsonType.DateTime: var millisecondsSinceEpoch = bsonReader.ReadDateTime(); var bsonDateTime = new BsonDateTime(millisecondsSinceEpoch); return(bsonDateTime.ToUniversalTime()); case BsonType.Decimal128: return(bsonReader.ReadDecimal128()); case BsonType.Document: return(DeserializeDiscriminatedValue(context, args)); case BsonType.Double: return(bsonReader.ReadDouble()); case BsonType.Int32: return(bsonReader.ReadInt32()); case BsonType.Int64: return(bsonReader.ReadInt64()); case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.ObjectId: return(bsonReader.ReadObjectId()); case BsonType.String: return(bsonReader.ReadString()); default: var message = string.Format("ObjectSerializer does not support BSON type '{0}'.", bsonType); throw new FormatException(message); } }