public override Voucher Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var voucher = new Voucher { ID = bsonReader.ReadObjectId("_id", ref read), Date = bsonReader.ReadDateTime("date", ref read), Type = VoucherType.Ordinary, }; voucher.Type = bsonReader.ReadString("special", ref read) switch { "amorz" => VoucherType.Amortization, "acarry" => VoucherType.AnnualCarry, "carry" => VoucherType.Carry, "dep" => VoucherType.Depreciation, "dev" => VoucherType.Devalue, "unc" => VoucherType.Uncertain, _ => VoucherType.Ordinary, }; voucher.Details = bsonReader.ReadArray("detail", ref read, new VoucherDetailSerializer().Deserialize); voucher.Remark = bsonReader.ReadString("remark", ref read); bsonReader.ReadEndDocument(); return(voucher); }
/***************************************************/ public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { IBsonReader reader = context.Reader; BsonType currentBsonType = reader.GetCurrentBsonType(); switch (currentBsonType) { case BsonType.Array: if (context.DynamicArraySerializer != null) { return(context.DynamicArraySerializer.Deserialize(context)); } break; case BsonType.Binary: { BsonBinaryData bsonBinaryData = reader.ReadBinaryData(); BsonBinarySubType subType = bsonBinaryData.SubType; if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { return(bsonBinaryData.ToGuid()); } break; } case BsonType.Boolean: return(reader.ReadBoolean()); case BsonType.DateTime: return(new BsonDateTime(reader.ReadDateTime()).ToUniversalTime()); case BsonType.Decimal128: return(reader.ReadDecimal128()); case BsonType.Document: return(DeserializeDiscriminatedValue(context, args)); case BsonType.Double: return(reader.ReadDouble()); case BsonType.Int32: return(reader.ReadInt32()); case BsonType.Int64: return(reader.ReadInt64()); case BsonType.Null: reader.ReadNull(); return(null); case BsonType.ObjectId: return(reader.ReadObjectId()); case BsonType.String: return(reader.ReadString()); } Engine.Reflection.Compute.RecordError($"ObjectSerializer does not support BSON type '{currentBsonType}'."); return(null); }
public override Asset Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var asset = new Asset { ID = bsonReader.ReadGuid("_id", ref read), User = bsonReader.ReadString("user", ref read), Name = bsonReader.ReadString("name", ref read), Date = bsonReader.ReadDateTime("date", ref read), Currency = bsonReader.ReadString("currency", ref read), Value = bsonReader.ReadDouble("value", ref read), Salvge = bsonReader.ReadDouble("salvge", ref read), Life = bsonReader.ReadInt32("life", ref read), Title = bsonReader.ReadInt32("title", ref read), DepreciationTitle = bsonReader.ReadInt32("deptitle", ref read), DevaluationTitle = bsonReader.ReadInt32("devtitle", ref read), DepreciationExpenseTitle = bsonReader.ReadInt32("exptitle", ref read), DevaluationExpenseTitle = bsonReader.ReadInt32("exvtitle", ref read) }; switch (bsonReader.ReadString("method", ref read)) { case "sl": asset.Method = DepreciationMethod.StraightLine; break; case "sy": asset.Method = DepreciationMethod.SumOfTheYear; break; case "dd": asset.Method = DepreciationMethod.DoubleDeclineMethod; break; default: asset.Method = DepreciationMethod.None; break; } if (asset.DepreciationExpenseTitle > 100) { asset.DepreciationExpenseSubTitle = asset.DepreciationExpenseTitle % 100; asset.DepreciationExpenseTitle /= 100; } if (asset.DevaluationExpenseTitle > 100) { asset.DevaluationExpenseSubTitle = asset.DevaluationExpenseTitle % 100; asset.DevaluationExpenseTitle /= 100; } asset.Schedule = bsonReader.ReadArray("schedule", ref read, ItemSerializer.Deserialize); asset.Remark = bsonReader.ReadString("remark", ref read); bsonReader.ReadEndDocument(); return(asset); }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.DateTime: return((DateTimeOffset?)ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime())); case BsonType.String: return((DateTimeOffset?)DateTimeOffset.Parse(Reader.ReadString())); case BsonType.Document: DateTime TP = DateTime.MinValue; TimeSpan TZ = TimeSpan.Zero; Reader.ReadStartDocument(); while (Reader.State == BsonReaderState.Type) { BsonType BsonType = Reader.ReadBsonType(); if (BsonType == BsonType.EndOfDocument) { break; } string FieldName = Reader.ReadName(); switch (FieldName) { case "tp": TP = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()); break; case "tz": TZ = TimeSpan.Parse(Reader.ReadString()); break; } } Reader.ReadEndDocument(); return((DateTimeOffset?)new DateTimeOffset(TP, TZ)); case BsonType.MinKey: Reader.ReadMinKey(); return((DateTimeOffset?)DateTimeOffset.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return((DateTimeOffset?)DateTimeOffset.MaxValue); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Expected a nullable DateTimeOffset value."); } }
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()); }
public override Amortization Deserialize(IBsonReader bsonReader) { string read = null; bsonReader.ReadStartDocument(); var amort = new Amortization { ID = bsonReader.ReadGuid("_id", ref read), User = bsonReader.ReadString("user", ref read), Name = bsonReader.ReadString("name", ref read), Value = bsonReader.ReadDouble("value", ref read), Date = bsonReader.ReadDateTime("date", ref read), TotalDays = bsonReader.ReadInt32("tday", ref read) }; switch (bsonReader.ReadString("interval", ref read)) { case "d": amort.Interval = AmortizeInterval.EveryDay; break; case "w": amort.Interval = AmortizeInterval.SameDayOfWeek; break; case "W": amort.Interval = AmortizeInterval.LastDayOfWeek; break; case "m": amort.Interval = AmortizeInterval.SameDayOfMonth; break; case "M": amort.Interval = AmortizeInterval.LastDayOfMonth; break; case "y": amort.Interval = AmortizeInterval.SameDayOfYear; break; case "Y": amort.Interval = AmortizeInterval.LastDayOfYear; break; } amort.Template = bsonReader.ReadDocument("template", ref read, VoucherSerializer.Deserialize); amort.Schedule = bsonReader.ReadArray("schedule", ref read, ItemSerializer.Deserialize); amort.Remark = bsonReader.ReadString("remark", ref read); bsonReader.ReadEndDocument(); return(amort); }
/// <summary> /// Reads a date & time value with offset. /// </summary> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>DateTimeOffset value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static DateTimeOffset ReadDateTimeOffset(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.DateTime: return((DateTimeOffset)ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime())); case BsonType.String: return(DateTimeOffset.Parse(Reader.ReadString())); case BsonType.Document: DateTime TP = DateTime.MinValue; TimeSpan TZ = TimeSpan.Zero; Reader.ReadStartDocument(); while (Reader.State == BsonReaderState.Type) { BsonType BsonType = Reader.ReadBsonType(); if (BsonType == BsonType.EndOfDocument) { break; } string FieldName = Reader.ReadName(); switch (FieldName) { case "tp": TP = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()); break; case "tz": TZ = TimeSpan.Parse(Reader.ReadString()); break; } } Reader.ReadEndDocument(); return(new DateTimeOffset(TP, TZ)); case BsonType.MinKey: Reader.ReadMinKey(); return(DateTimeOffset.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return(DateTimeOffset.MaxValue); default: throw new ArgumentException("Expected a date & time value with offset, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }
public void TestDateTimeShell() { var json = "ISODate(\"1970-01-01T00:00:00Z\")"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(0, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Initial, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; Assert.AreEqual(json, BsonSerializer.Deserialize <DateTime>(json).ToJson(jsonSettings)); }
public void DeserializeAttribute(string attr, IBsonReader reader, ref Measurement m) { long ticks; switch (attr) { case "CreatedAt": ticks = reader.ReadDateTime(); m.Timestamp = DateTimeOffset.FromUnixTimeMilliseconds(ticks).UtcDateTime; break; case "Data": this.DeserializeDataPoints(reader, ref m); break; default: throw new DatabaseException("Unknown document attribute", "Measurements"); } }
internal static object Deserialize(IBsonReader reader) { switch (reader.GetCurrentBsonType()) { case BsonType.ObjectId: return(reader.ReadObjectId()); case BsonType.Boolean: return(reader.ReadBoolean()); case BsonType.DateTime: return(UnixTime.ToDateTime(reader.ReadDateTime())); case BsonType.Int32: return(reader.ReadInt32()); case BsonType.Binary: return(reader.ReadBytes()); case BsonType.Int64: return(reader.ReadInt64()); case BsonType.Double: return(reader.ReadDouble()); case BsonType.Null: reader.ReadNull(); return(null); case BsonType.String: return(reader.ReadString()); default: throw new InvalidOperationException( $"Cannot deserialize {reader.GetCurrentBsonType()} to native value."); } }
/// <summary> /// Reads a string value. /// </summary> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>String value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static string ReadString(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.Boolean: return(Reader.ReadBoolean().ToString()); case BsonType.DateTime: return(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()).ToString()); case BsonType.Decimal128: return(Reader.ReadDecimal128().ToString()); case BsonType.Double: return(Reader.ReadDouble().ToString()); case BsonType.Int32: return(Reader.ReadInt32().ToString()); case BsonType.Int64: return(Reader.ReadInt64().ToString()); case BsonType.JavaScript: return(Reader.ReadJavaScript()); case BsonType.JavaScriptWithScope: return(Reader.ReadJavaScriptWithScope()); case BsonType.Null: Reader.ReadNull(); return(null); case BsonType.ObjectId: return(Reader.ReadObjectId().ToString()); case BsonType.String: return(Reader.ReadString()); case BsonType.Symbol: return(Reader.ReadSymbol()); default: throw new ArgumentException("Expected a char value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }
static object ReadObject(IBsonReader bsonReader) //_120509_173140 keep consistent { 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)); } }
public void TestDateTimeStrict() { var json = "{ \"$date\" : 0 }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(0, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.Equal(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.Boolean: return(new CaseInsensitiveString(Reader.ReadBoolean().ToString())); case BsonType.DateTime: return(new CaseInsensitiveString(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()).ToString())); case BsonType.Decimal128: return(new CaseInsensitiveString(Reader.ReadDecimal128().ToString())); case BsonType.Double: return(new CaseInsensitiveString(Reader.ReadDouble().ToString())); case BsonType.Int32: return(new CaseInsensitiveString(Reader.ReadInt32().ToString())); case BsonType.Int64: return(new CaseInsensitiveString(Reader.ReadInt64().ToString())); case BsonType.JavaScript: return(new CaseInsensitiveString(Reader.ReadJavaScript())); case BsonType.JavaScriptWithScope: return(new CaseInsensitiveString(Reader.ReadJavaScriptWithScope())); case BsonType.Null: Reader.ReadNull(); return(null); case BsonType.ObjectId: return(new CaseInsensitiveString(Reader.ReadObjectId().ToString())); case BsonType.String: return(new CaseInsensitiveString(Reader.ReadString())); case BsonType.Symbol: return(new CaseInsensitiveString(Reader.ReadSymbol())); default: throw new Exception("Expected a case-insensitive string value."); } }
public void TestDateTimeMaxBson() { var json = "new Date(9223372036854775807)"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(9223372036854775807, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson()); }
public void TestDateTimeShell(string json, long expectedResult, string canonicalJson) { using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(expectedResult, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; Assert.Equal(canonicalJson, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
public void TestDateTimeShell() { var json = "ISODate(\"1970-01-01T00:00:00Z\")"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(0, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Initial, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
public void TestDateTimeStrictIso8601() { var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }"; using (_bsonReader = new JsonReader(json)) { Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.Equal(0, _bsonReader.ReadDateTime()); Assert.Equal(BsonReaderState.Initial, _bsonReader.State); } var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.Equal(expected, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
/// <summary> /// Reads a date & time value. /// </summary> /// <param name="Reader">Binary reader.</param> /// <param name="FieldDataType">Field data type.</param> /// <returns>DateTime value.</returns> /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception> public static DateTime ReadDateTime(IBsonReader Reader, BsonType FieldDataType) { switch (FieldDataType) { case BsonType.DateTime: return(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime())); case BsonType.String: return(DateTime.Parse(Reader.ReadString())); case BsonType.MinKey: Reader.ReadMinKey(); return(DateTime.MinValue); case BsonType.MaxKey: Reader.ReadMaxKey(); return(DateTime.MaxValue); default: throw new ArgumentException("Expected a date & time value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType)); } }