GeoJsonFeatureSerializer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 MongoDB.Bson;
  16. using MongoDB.Bson.Serialization;
  17. using MongoDB.Bson.Serialization.Serializers;
  18. namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
  19. {
  20. /// <summary>
  21. /// Represents a serializer for a GeoJsonFeature value.
  22. /// </summary>
  23. /// <typeparam name="TCoordinates">The type of the coordinates.</typeparam>
  24. public class GeoJsonFeatureSerializer<TCoordinates> : ClassSerializerBase<GeoJsonFeature<TCoordinates>> where TCoordinates : GeoJsonCoordinates
  25. {
  26. // private constants
  27. private static class Flags
  28. {
  29. public const long Geometry = 16;
  30. public const long Id = 32;
  31. public const long Properties = 64;
  32. }
  33. // private fields
  34. private readonly IBsonSerializer<GeoJsonGeometry<TCoordinates>> _geometrySerializer = BsonSerializer.LookupSerializer<GeoJsonGeometry<TCoordinates>>();
  35. private readonly GeoJsonObjectSerializerHelper<TCoordinates> _helper;
  36. // constructors
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="GeoJsonFeatureSerializer{TCoordinates}"/> class.
  39. /// </summary>
  40. public GeoJsonFeatureSerializer()
  41. {
  42. _helper = new GeoJsonObjectSerializerHelper<TCoordinates>
  43. (
  44. "Feature",
  45. new SerializerHelper.Member("geometry", Flags.Geometry),
  46. new SerializerHelper.Member("id", Flags.Id, isOptional: true),
  47. new SerializerHelper.Member("properties", Flags.Properties, isOptional: true)
  48. );
  49. }
  50. // protected methods
  51. /// <summary>
  52. /// Deserializes a value.
  53. /// </summary>
  54. /// <param name="context">The deserialization context.</param>
  55. /// <param name="args">The deserialization args.</param>
  56. /// <returns>The value.</returns>
  57. protected override GeoJsonFeature<TCoordinates> DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  58. {
  59. var geoJsonFeatureArgs = new GeoJsonFeatureArgs<TCoordinates>();
  60. GeoJsonGeometry<TCoordinates> geometry = null;
  61. _helper.DeserializeMembers(context, (elementName, flag) =>
  62. {
  63. switch (flag)
  64. {
  65. case Flags.Geometry: geometry = DeserializeGeometry(context); break;
  66. case Flags.Id: geoJsonFeatureArgs.Id = DeserializeId(context); break;
  67. case Flags.Properties: geoJsonFeatureArgs.Properties = DeserializeProperties(context); break;
  68. default: _helper.DeserializeBaseMember(context, elementName, flag, geoJsonFeatureArgs); break;
  69. }
  70. });
  71. return new GeoJsonFeature<TCoordinates>(geoJsonFeatureArgs, geometry);
  72. }
  73. /// <summary>
  74. /// Serializes a value.
  75. /// </summary>
  76. /// <param name="context">The serialization context.</param>
  77. /// <param name="args">The serialization args.</param>
  78. /// <param name="value">The value.</param>
  79. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, GeoJsonFeature<TCoordinates> value)
  80. {
  81. _helper.SerializeMembers(context, value, SerializeDerivedMembers);
  82. }
  83. // private methods
  84. private GeoJsonGeometry<TCoordinates> DeserializeGeometry(BsonDeserializationContext context)
  85. {
  86. return _geometrySerializer.Deserialize(context);
  87. }
  88. private BsonValue DeserializeId(BsonDeserializationContext context)
  89. {
  90. return BsonValueSerializer.Instance.Deserialize(context);
  91. }
  92. private BsonDocument DeserializeProperties(BsonDeserializationContext context)
  93. {
  94. return BsonDocumentSerializer.Instance.Deserialize(context);
  95. }
  96. private void SerializeDerivedMembers(BsonSerializationContext context, GeoJsonFeature<TCoordinates> value)
  97. {
  98. SerializeGeometry(context, value.Geometry);
  99. SerializeId(context, value.Id);
  100. SerializeProperties(context, value.Properties);
  101. }
  102. private void SerializeGeometry(BsonSerializationContext context, GeoJsonGeometry<TCoordinates> geometry)
  103. {
  104. context.Writer.WriteName("geometry");
  105. _geometrySerializer.Serialize(context, geometry);
  106. }
  107. private void SerializeId(BsonSerializationContext context, BsonValue id)
  108. {
  109. if (id != null)
  110. {
  111. context.Writer.WriteName("id");
  112. BsonValueSerializer.Instance.Serialize(context, id);
  113. }
  114. }
  115. private void SerializeProperties(BsonSerializationContext context, BsonDocument properties)
  116. {
  117. if (properties != null)
  118. {
  119. context.Writer.WriteName("properties");
  120. BsonDocumentSerializer.Instance.Serialize(context, properties);
  121. }
  122. }
  123. }
  124. }