GeoJsonFeatureCollectionSerializer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System.Collections.Generic;
  16. using MongoDB.Bson;
  17. using MongoDB.Bson.Serialization;
  18. using MongoDB.Bson.Serialization.Serializers;
  19. namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
  20. {
  21. /// <summary>
  22. /// Represents a serializer for a GeoJsonFeatureCollection value.
  23. /// </summary>
  24. /// <typeparam name="TCoordinates">The type of the coordinates.</typeparam>
  25. public class GeoJsonFeatureCollectionSerializer<TCoordinates> : ClassSerializerBase<GeoJsonFeatureCollection<TCoordinates>> where TCoordinates : GeoJsonCoordinates
  26. {
  27. // private constants
  28. private static class Flags
  29. {
  30. public const long Features = 16;
  31. }
  32. // private fields
  33. private readonly IBsonSerializer<GeoJsonFeature<TCoordinates>> _featureSerializer = BsonSerializer.LookupSerializer<GeoJsonFeature<TCoordinates>>();
  34. private readonly GeoJsonObjectSerializerHelper<TCoordinates> _helper;
  35. // constructors
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="GeoJsonFeatureCollectionSerializer{TCoordinates}"/> class.
  38. /// </summary>
  39. public GeoJsonFeatureCollectionSerializer()
  40. {
  41. _helper = new GeoJsonObjectSerializerHelper<TCoordinates>
  42. (
  43. "FeatureCollection",
  44. new SerializerHelper.Member("features", Flags.Features)
  45. );
  46. }
  47. // protected methods
  48. /// <summary>
  49. /// Deserializes a value.
  50. /// </summary>
  51. /// <param name="context">The deserialization context.</param>
  52. /// <param name="args">The deserialization args.</param>
  53. /// <returns>The value.</returns>
  54. protected override GeoJsonFeatureCollection<TCoordinates> DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  55. {
  56. var geoJsonObjectArgs = new GeoJsonObjectArgs<TCoordinates>();
  57. List<GeoJsonFeature<TCoordinates>> features = null;
  58. _helper.DeserializeMembers(context, (elementName, flag) =>
  59. {
  60. switch (flag)
  61. {
  62. case Flags.Features: features = DeserializeFeatures(context); break;
  63. default: _helper.DeserializeBaseMember(context, elementName, flag, geoJsonObjectArgs); break;
  64. }
  65. });
  66. return new GeoJsonFeatureCollection<TCoordinates>(geoJsonObjectArgs, features);
  67. }
  68. /// <summary>
  69. /// Serializes a value.
  70. /// </summary>
  71. /// <param name="context">The serialization context.</param>
  72. /// <param name="args">The serialization args.</param>
  73. /// <param name="value">The value.</param>
  74. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, GeoJsonFeatureCollection<TCoordinates> value)
  75. {
  76. _helper.SerializeMembers(context, value, SerializeDerivedMembers);
  77. }
  78. // private methods
  79. private List<GeoJsonFeature<TCoordinates>> DeserializeFeatures(BsonDeserializationContext context)
  80. {
  81. var bsonReader = context.Reader;
  82. bsonReader.ReadStartArray();
  83. var features = new List<GeoJsonFeature<TCoordinates>>();
  84. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  85. {
  86. var feature = _featureSerializer.Deserialize(context);
  87. features.Add(feature);
  88. }
  89. bsonReader.ReadEndArray();
  90. return features;
  91. }
  92. private void SerializeDerivedMembers(BsonSerializationContext context, GeoJsonFeatureCollection<TCoordinates> value)
  93. {
  94. SerializeFeatures(context, value.Features);
  95. }
  96. private void SerializeFeatures(BsonSerializationContext context, IEnumerable<GeoJsonFeature<TCoordinates>> features)
  97. {
  98. var bsonWriter = context.Writer;
  99. bsonWriter.WriteName("features");
  100. bsonWriter.WriteStartArray();
  101. foreach (var feature in features)
  102. {
  103. _featureSerializer.Serialize(context, feature);
  104. }
  105. bsonWriter.WriteEndArray();
  106. }
  107. }
  108. }