public void SerializeAndDeserializeToTheSameObject(decimal originalValue) { Serializer <decimal> serializer = new DecimalSerializer(); byte[] serializedValue = serializer.Serialize(originalValue); decimal deserializedValue = serializer.Deserialize(serializedValue); Assert.Equal(originalValue, deserializedValue); }
/// <inheritdoc/> public override Money Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var reader = context.Reader; var bsonType = reader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { reader.ReadNull(); return(Money.Zero); } if (bsonType != BsonType.Document) { throw new FormatException($"Cannot deserialize Money from BsonType {bsonType}."); } reader.ReadStartDocument(); decimal? amount = null; ICurrency currency = null; while (reader.ReadBsonType() != BsonType.EndOfDocument) { var name = reader.ReadName(); if (name == _amountFieldName) { amount = _amountSerializer.Deserialize(context, args); } else if (name == _currencyFieldName) { currency = _currencySerializer.Deserialize(context, args); } else { throw new BsonSerializationException($"Invalid element: '{name}'."); } } reader.ReadEndDocument(); if (currency is null) { throw new BsonSerializationException($"Document does not contains field {_currencyFieldName}"); } if (amount is null) { throw new BsonSerializationException($"Document does not contains field {_amountFieldName}"); } return(new Money(amount.Value, currency)); }
public void ShouldThrowIfDeserializingLessThanSixteenBytes(byte[] data) { Serializer <decimal> serializer = new DecimalSerializer(); Assert.Throws <ArgumentOutOfRangeException>(() => serializer.Deserialize(data)); }
public void ShouldThrowIfDeserializingNull() { Serializer <decimal> serializer = new DecimalSerializer(); Assert.Throws <ArgumentNullException>(() => serializer.Deserialize(null)); }