/* 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 MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; namespace MongoDB.Driver.GeoJsonObjectModel.Serializers { /// /// Represents a serializer for a GeoJsonMultiLineString value. /// /// The type of the coordinates. public class GeoJsonMultiLineStringSerializer : ClassSerializerBase> where TCoordinates : GeoJsonCoordinates { // private constants private static class Flags { public const long Coordinates = 16; } // private fields private readonly IBsonSerializer> _coordinatesSerializer = BsonSerializer.LookupSerializer>(); private readonly GeoJsonObjectSerializerHelper _helper; // constructors /// /// Initializes a new instance of the class. /// public GeoJsonMultiLineStringSerializer() { _helper = new GeoJsonObjectSerializerHelper ( "MultiLineString", new SerializerHelper.Member("coordinates", Flags.Coordinates) ); } // protected methods /// /// Deserializes a value. /// /// The deserialization context. /// The deserialization args. /// The value. protected override GeoJsonMultiLineString DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { var geoJsonObjectArgs = new GeoJsonObjectArgs(); GeoJsonMultiLineStringCoordinates coordinates = null; _helper.DeserializeMembers(context, (elementName, flag) => { switch (flag) { case Flags.Coordinates: coordinates = DeserializeCoordinates(context); break; default: _helper.DeserializeBaseMember(context, elementName, flag, geoJsonObjectArgs); break; } }); return new GeoJsonMultiLineString(geoJsonObjectArgs, coordinates); } /// /// Serializes a value. /// /// The serialization context. /// The serialization args. /// The value. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, GeoJsonMultiLineString value) { _helper.SerializeMembers(context, value, SerializeDerivedMembers); } // private methods private GeoJsonMultiLineStringCoordinates DeserializeCoordinates(BsonDeserializationContext context) { return _coordinatesSerializer.Deserialize(context); } private void SerializeCoordinates(BsonSerializationContext context, GeoJsonMultiLineStringCoordinates coordinates) { context.Writer.WriteName("coordinates"); _coordinatesSerializer.Serialize(context, coordinates); } private void SerializeDerivedMembers(BsonSerializationContext context, GeoJsonMultiLineString value) { SerializeCoordinates(context, value.Coordinates); } } }