/* 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 MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Bson.Serialization.Serializers
{
///
/// Represents a serializer for nullable values.
///
/// The underlying type.
public class NullableSerializer :
SerializerBase>,
IChildSerializerConfigurable
where T : struct
{
// private fields
private Lazy> _lazySerializer;
// constructors
///
/// Initializes a new instance of the class.
///
public NullableSerializer()
: this(BsonSerializer.SerializerRegistry)
{
}
///
/// Initializes a new instance of the class.
///
/// The serializer.
public NullableSerializer(IBsonSerializer serializer)
{
if (serializer == null)
{
throw new ArgumentNullException("serializer");
}
_lazySerializer = new Lazy>(() => serializer);
}
///
/// Initializes a new instance of the class.
///
/// The serializer registry.
public NullableSerializer(IBsonSerializerRegistry serializerRegistry)
{
if (serializerRegistry == null)
{
throw new ArgumentNullException("serializerRegistry");
}
_lazySerializer = new Lazy>(() => serializerRegistry.GetSerializer());
}
// public methods
///
/// Deserializes a value.
///
/// The deserialization context.
/// The deserialization args.
/// A deserialized value.
public override T? Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonReader = context.Reader;
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
return _lazySerializer.Value.Deserialize(context);
}
}
///
/// Serializes a value.
///
/// The serialization context.
/// The serialization args.
/// The object.
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T? value)
{
var bsonWriter = context.Writer;
if (value == null)
{
bsonWriter.WriteNull();
}
else
{
_lazySerializer.Value.Serialize(context, value.Value);
}
}
///
/// Returns a serializer that has been reconfigured with the specified serializer.
///
/// The serializer.
///
/// The reconfigured serializer.
///
public NullableSerializer WithSerializer(IBsonSerializer serializer)
{
if (serializer == _lazySerializer.Value)
{
return this;
}
else
{
return new NullableSerializer(serializer);
}
}
// explicit interface implementations
IBsonSerializer IChildSerializerConfigurable.ChildSerializer
{
get { return _lazySerializer.Value; }
}
IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
{
return WithSerializer((IBsonSerializer)childSerializer);
}
}
}