本文整理汇总了C#中BsonReader.ReadDateTime方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadDateTime方法的具体用法?C# BsonReader.ReadDateTime怎么用?C# BsonReader.ReadDateTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadDateTime方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(DateTimeOffset));
BsonType bsonType = bsonReader.GetCurrentBsonType();
long ticks;
TimeSpan offset;
switch (bsonType)
{
case BsonType.Array:
bsonReader.ReadStartArray();
ticks = bsonReader.ReadInt64();
offset = TimeSpan.FromMinutes(bsonReader.ReadInt32());
bsonReader.ReadEndArray();
return new DateTimeOffset(ticks, offset);
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadDateTime("DateTime"); // ignore value
ticks = bsonReader.ReadInt64("Ticks");
offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset"));
bsonReader.ReadEndDocument();
return new DateTimeOffset(ticks, offset);
case BsonType.String:
return XmlConvert.ToDateTimeOffset(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例2: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(DateTimeOffset));
var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
DateTimeOffset value;
switch (bsonType)
{
case BsonType.DateTime:
// use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly
value = (new BsonDateTime(bsonReader.ReadDateTime())).ToUniversalTime();
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadDateTime("DateTimeUTC"); // ignore value (use Ticks instead)
value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc);
bsonReader.ReadEndDocument();
break;
case BsonType.Int64:
value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc);
break;
case BsonType.String:
// note: we're not using XmlConvert because of bugs in Mono
if (dateTimeSerializationOptions.DateOnly)
{
value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
}
else
{
var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" };
value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
}
break;
default:
var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType);
throw new FormatException(message);
}
return value;
}
示例3: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(BsonDateTime));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.DateTime:
var millisecondsSinceEpoch = bsonReader.ReadDateTime();
return new BsonDateTime(millisecondsSinceEpoch);
default:
var message = string.Format("Cannot deserialize BsonDateTime from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例4: while
// explicit interface implementation
object IBsonSerializable.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
if (bsonReader.CurrentBsonType == Bson.BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
bsonReader.ReadStartDocument();
BsonType bsonType;
while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
switch (name)
{
case "abbreviated":
_abbreviated = bsonReader.ReadString();
break;
case "client":
_client = bsonReader.ReadString();
break;
case "command":
_command = BsonDocument.ReadFrom(bsonReader);
break;
case "cursorid":
_cursorId = BsonValue.ReadFrom(bsonReader).ToInt64();
break;
case "err":
_error = bsonReader.ReadString();
break;
case "exception":
_exception = bsonReader.ReadString();
break;
case "exceptionCode":
_exceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "exhaust":
_exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "fastmod":
_fastMod = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "fastmodinsert":
_fastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "idhack":
_idHack = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "info":
_info = bsonReader.ReadString();
break;
case "keyUpdates":
_keyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "millis":
_duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble());
break;
case "moved":
_moved = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "nreturned":
_numberReturned = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "ns":
_namespace = bsonReader.ReadString();
break;
case "nscanned":
_numberScanned = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "ntoreturn":
_numberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "ntoskip":
_numberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "op":
_op = bsonReader.ReadString();
break;
case "query":
_query = BsonDocument.ReadFrom(bsonReader);
break;
case "responseLength":
_responseLength = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "scanAndOrder":
_scanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "ts":
_timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
break;
case "updateobj":
_updateObject = BsonDocument.ReadFrom(bsonReader);
break;
case "upsert":
_upsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "user":
_user = bsonReader.ReadString();
//.........这里部分代码省略.........
示例5: TestDateTimeTengen
public void TestDateTimeTengen() {
var json = "new Date(0)";
using (bsonReader = BsonReader.Create(json)) {
Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType());
Assert.AreEqual(0, bsonReader.ReadDateTime());
Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
}
var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.TenGen };
Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(new StringReader(json)).ToJson(jsonSettings));
}
示例6: TestDateTimeShell
public void TestDateTimeShell() {
var json = "ISODate(\"1970-01-01T00:00:00Z\")";
using (bsonReader = BsonReader.Create(json)) {
Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType());
Assert.AreEqual(0, bsonReader.ReadDateTime());
Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
}
var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell };
Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(new StringReader(json)).ToJson(jsonSettings));
}
示例7: TestDateTimeMaxBson
public void TestDateTimeMaxBson() {
var json = "new Date(9223372036854775807)";
using (bsonReader = BsonReader.Create(json)) {
Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType());
Assert.AreEqual(9223372036854775807, bsonReader.ReadDateTime());
Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
}
Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDateTime>(new StringReader(json)).ToJson());
}
示例8: Deserialize
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
) {
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else {
var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions) options;
long? millisecondsSinceEpoch = null;
long? ticks = null;
switch (bsonType) {
case BsonType.DateTime:
millisecondsSinceEpoch = bsonReader.ReadDateTime();
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime");
bsonReader.ReadName("Ticks");
var ticksValue = BsonValue.ReadFrom(bsonReader);
if (!ticksValue.IsBsonUndefined) {
ticks = ticksValue.ToInt64();
}
bsonReader.ReadEndDocument();
break;
case BsonType.Int64:
ticks = bsonReader.ReadInt64();
break;
case BsonType.String:
// note: we're not using XmlConvert because of bugs in Mono
DateTime dateTime;
if (dateTimeOptions.DateOnly) {
dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
} else {
var formats = new string[] {
"yyyy-MM-ddK",
"yyyy-MM-ddTHH:mm:ssK",
"yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
};
dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
}
ticks = dateTime.Ticks;
break;
default:
var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
BsonDateTime bsonDateTime;
if (ticks.HasValue) {
bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc));
} else {
bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value);
}
if (dateTimeOptions.DateOnly) {
var dateTime = bsonDateTime.Value;
if (dateTime.TimeOfDay != TimeSpan.Zero) {
throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero.");
}
bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeOptions.Kind)); // not ToLocalTime or ToUniversalTime!
} else {
if (bsonDateTime.IsValidDateTime) {
var dateTime = bsonDateTime.Value;
switch (dateTimeOptions.Kind) {
case DateTimeKind.Local:
case DateTimeKind.Unspecified:
dateTime = BsonUtils.ToLocalTime(dateTime, dateTimeOptions.Kind);
break;
case DateTimeKind.Utc:
dateTime = BsonUtils.ToUniversalTime(dateTime);
break;
}
bsonDateTime = BsonDateTime.Create(dateTime);
} else {
if (dateTimeOptions.Kind != DateTimeKind.Utc) {
throw new FileFormatException("BsonDateTime is outside the range of .NET DateTime.");
}
}
}
return bsonDateTime;
}
}
示例9: TestDateTime
public void TestDateTime()
{
var json = "{ \"$date\" : 0 }";
using (bsonReader = BsonReader.Create(json)) {
Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType());
Assert.AreEqual(BsonConstants.UnixEpoch, bsonReader.ReadDateTime());
Assert.AreEqual(BsonReadState.Done, bsonReader.ReadState);
}
Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(new StringReader(json)).ToJson());
}
示例10: TestDateTimeStrictIso8601
public void TestDateTimeStrictIso8601()
{
var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }";
using (_bsonReader = new JsonReader(json))
{
Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType());
Assert.AreEqual(0, _bsonReader.ReadDateTime());
Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
}
var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out
var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
Assert.AreEqual(expected, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
}
示例11: TestDateTimeStrict
public void TestDateTimeStrict()
{
var json = "{ \"$date\" : 0 }";
using (_bsonReader = new JsonReader(json))
{
Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType());
Assert.AreEqual(0, _bsonReader.ReadDateTime());
Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
}
var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
}
示例12: TestDateTimeMinBson
public void TestDateTimeMinBson()
{
var json = "new Date(-9223372036854775808)";
using (_bsonReader = new JsonReader(json))
{
Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType());
Assert.AreEqual(-9223372036854775808, _bsonReader.ReadDateTime());
Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
}
Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson());
}
示例13: Deserialize
//.........这里部分代码省略.........
BsonType bsonType;
while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
switch (name)
{
case "abbreviated":
profileInfo.Abbreviated = bsonReader.ReadString();
break;
case "client":
profileInfo.Client = bsonReader.ReadString();
break;
case "command":
profileInfo.Command = BsonDocument.ReadFrom(bsonReader);
break;
case "cursorid":
profileInfo.CursorId = BsonValue.ReadFrom(bsonReader).ToInt64();
break;
case "err":
profileInfo.Error = bsonReader.ReadString();
break;
case "exception":
profileInfo.Exception = bsonReader.ReadString();
break;
case "exceptionCode":
profileInfo.ExceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "exhaust":
profileInfo.Exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "fastmod":
profileInfo.FastMod = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "fastmodinsert":
profileInfo.FastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "idhack":
profileInfo.IdHack = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "info":
profileInfo.Info = bsonReader.ReadString();
break;
case "keyUpdates":
profileInfo.KeyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "millis":
profileInfo.Duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble());
break;
case "moved":
profileInfo.Moved = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "nreturned":
profileInfo.NumberReturned = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "ns":
profileInfo.Namespace = bsonReader.ReadString();
break;
case "nscanned":
profileInfo.NumberScanned = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "ntoreturn":
profileInfo.NumberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "ntoskip":
profileInfo.NumberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "op":
profileInfo.Op = bsonReader.ReadString();
break;
case "query":
profileInfo.Query = BsonDocument.ReadFrom(bsonReader);
break;
case "responseLength":
profileInfo.ResponseLength = BsonValue.ReadFrom(bsonReader).ToInt32();
break;
case "scanAndOrder":
profileInfo.ScanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "ts":
profileInfo.Timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
break;
case "updateobj":
profileInfo.UpdateObject = BsonDocument.ReadFrom(bsonReader);
break;
case "upsert":
profileInfo.Upsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
break;
case "user":
profileInfo.User = bsonReader.ReadString();
break;
default:
bsonReader.SkipValue(); // ignore unknown elements
break;
}
}
bsonReader.ReadEndDocument();
return profileInfo;
}
}
示例14: Deserialize
public override object Deserialize(
BsonReader bsonReader,
Type nominalType
)
{
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else {
return BsonDateTime.Create(bsonReader.ReadDateTime());
}
}
示例15: ReadObject
//_120509_173140 keep consistent
static object ReadObject(BsonReader bsonReader)
{
switch (bsonReader.GetCurrentBsonType())
{
case BsonType.Array: return ReadArray(bsonReader); // replacement
case BsonType.Binary: var binary = BsonSerializer.Deserialize<BsonValue>(bsonReader); return BsonTypeMapper.MapToDotNetValue(binary) ?? binary; // byte[] or Guid else self
case BsonType.Boolean: return bsonReader.ReadBoolean();
case BsonType.DateTime: return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
case BsonType.Document: return ReadCustomObject(bsonReader); // replacement
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: return BsonSerializer.Deserialize<BsonValue>(bsonReader);
}
}