| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- /* Copyright 2010-present MongoDB Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using System.Globalization;
- using System.IO;
- using MongoDB.Bson.IO;
- using MongoDB.Bson.Serialization.Attributes;
- using MongoDB.Bson.Serialization.Options;
- namespace MongoDB.Bson.Serialization.Serializers
- {
- /// <summary>
- /// Represents a serializer for Doubles.
- /// </summary>
- public class DoubleSerializer : StructSerializerBase<double>, IRepresentationConfigurable<DoubleSerializer>, IRepresentationConverterConfigurable<DoubleSerializer>
- {
- // private fields
- private readonly BsonType _representation;
- private readonly RepresentationConverter _converter;
- // constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="DoubleSerializer"/> class.
- /// </summary>
- public DoubleSerializer()
- : this(BsonType.Double)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="DoubleSerializer"/> class.
- /// </summary>
- /// <param name="representation">The representation.</param>
- public DoubleSerializer(BsonType representation)
- : this(representation, new RepresentationConverter(false, false))
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="DoubleSerializer"/> class.
- /// </summary>
- /// <param name="representation">The representation.</param>
- /// <param name="converter">The converter.</param>
- public DoubleSerializer(BsonType representation, RepresentationConverter converter)
- {
- switch (representation)
- {
- case BsonType.Decimal128:
- case BsonType.Double:
- case BsonType.Int32:
- case BsonType.Int64:
- case BsonType.String:
- break;
- default:
- var message = string.Format("{0} is not a valid representation for a DoubleSerializer.", representation);
- throw new ArgumentException(message);
- }
- _representation = representation;
- _converter = converter;
- }
- // public properties
- /// <summary>
- /// Gets the converter.
- /// </summary>
- /// <value>
- /// The converter.
- /// </value>
- public RepresentationConverter Converter
- {
- get { return _converter; }
- }
- /// <summary>
- /// Gets the representation.
- /// </summary>
- /// <value>
- /// The representation.
- /// </value>
- public BsonType Representation
- {
- get { return _representation; }
- }
- // 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 double Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
- {
- var bsonReader = context.Reader;
- var bsonType = bsonReader.GetCurrentBsonType();
- switch (bsonType)
- {
- case BsonType.Decimal128:
- return _converter.ToDouble(bsonReader.ReadDecimal128());
- case BsonType.Double:
- return bsonReader.ReadDouble();
- case BsonType.Int32:
- return _converter.ToDouble(bsonReader.ReadInt32());
- case BsonType.Int64:
- return _converter.ToDouble(bsonReader.ReadInt64());
- case BsonType.String:
- return JsonConvert.ToDouble(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, 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(JsonConvert.ToString(value));
- break;
- default:
- var message = string.Format("'{0}' is not a valid Double representation.", _representation);
- throw new BsonSerializationException(message);
- }
- }
- /// <summary>
- /// Returns a serializer that has been reconfigured with the specified item serializer.
- /// </summary>
- /// <param name="converter">The converter.</param>
- /// <returns>The reconfigured serializer.</returns>
- public DoubleSerializer WithConverter(RepresentationConverter converter)
- {
- if (converter == _converter)
- {
- return this;
- }
- else
- {
- return new DoubleSerializer(_representation, converter);
- }
- }
- /// <summary>
- /// Returns a serializer that has been reconfigured with the specified representation.
- /// </summary>
- /// <param name="representation">The representation.</param>
- /// <returns>The reconfigured serializer.</returns>
- public DoubleSerializer WithRepresentation(BsonType representation)
- {
- if (representation == _representation)
- {
- return this;
- }
- else
- {
- return new DoubleSerializer(representation, _converter);
- }
- }
- // explicit interface implementations
- IBsonSerializer IRepresentationConverterConfigurable.WithConverter(RepresentationConverter converter)
- {
- return WithConverter(converter);
- }
- IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
- {
- return WithRepresentation(representation);
- }
- }
- }
|