// 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 Decimal128 Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var bsonReader = context.Reader; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Decimal128: return(bsonReader.ReadDecimal128()); case BsonType.Double: return(_converter.ToDecimal128(bsonReader.ReadDouble())); case BsonType.Int32: return(_converter.ToDecimal128(bsonReader.ReadInt32())); case BsonType.Int64: return(_converter.ToDecimal128(bsonReader.ReadInt64())); case BsonType.String: return(JsonConvert.ToDecimal128(bsonReader.ReadString())); default: throw CreateCannotDeserializeFromBsonTypeException(bsonType); } }
/// <summary> /// Serializes a value. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="args">The serialization args.</param> /// <param name="value">The object.</param> public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, ushort value) { var bsonWriter = context.Writer; switch (_representation) { case BsonType.Decimal128: bsonWriter.WriteDecimal128(_converter.ToDecimal128(value)); break; case BsonType.Double: bsonWriter.WriteDouble(_converter.ToDouble(value)); break; case BsonType.Int32: bsonWriter.WriteInt32(_converter.ToInt32(value)); break; case BsonType.Int64: bsonWriter.WriteInt64(_converter.ToInt64(value)); break; case BsonType.String: bsonWriter.WriteString(JsonConvert.ToString(value)); break; default: var message = string.Format("'{0}' is not a valid representation for type 'UInt16'", _representation); throw new BsonSerializationException(message); } }
/// <summary> /// Serializes a value. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="args">The serialization args.</param> /// <param name="value">The object.</param> public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, double value) { var bsonWriter = context.Writer; switch (_representation) { case BsonType.Decimal128: bsonWriter.WriteDecimal128(_converter.ToDecimal128(value)); break; case BsonType.Double: bsonWriter.WriteDouble(value); break; case BsonType.Int32: bsonWriter.WriteInt32(_converter.ToInt32(value)); break; case BsonType.Int64: bsonWriter.WriteInt64(_converter.ToInt64(value)); break; case BsonType.String: bsonWriter.WriteString(value.ToString("R", NumberFormatInfo.InvariantInfo)); break; default: var message = string.Format("'{0}' is not a valid Double representation.", _representation); throw new BsonSerializationException(message); } }
/// <summary> /// Serializes a value. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="args">The serialization args.</param> /// <param name="value">The object.</param> public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, decimal value) { var bsonWriter = context.Writer; switch (_representation) { case BsonType.Array: bsonWriter.WriteStartArray(); var bits = Decimal.GetBits(value); bsonWriter.WriteInt32(bits[0]); bsonWriter.WriteInt32(bits[1]); bsonWriter.WriteInt32(bits[2]); bsonWriter.WriteInt32(bits[3]); bsonWriter.WriteEndArray(); break; case BsonType.Decimal128: bsonWriter.WriteDecimal128(_converter.ToDecimal128(value)); break; case BsonType.Double: bsonWriter.WriteDouble(_converter.ToDouble(value)); break; case BsonType.Int32: bsonWriter.WriteInt32(_converter.ToInt32(value)); break; case BsonType.Int64: bsonWriter.WriteInt64(_converter.ToInt64(value)); break; case BsonType.String: bsonWriter.WriteString(JsonConvert.ToString(value)); break; default: var message = string.Format("'{0}' is not a valid Decimal representation.", _representation); throw new BsonSerializationException(message); } }