GridFSFileInfoSerializer.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright 2015-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. using MongoDB.Driver.Core.Misc;
  19. namespace MongoDB.Driver.GridFS
  20. {
  21. /// <summary>
  22. /// Represents a serializer for GridFSFileInfo.
  23. /// </summary>
  24. /// <typeparam name="TFileId">The type of the file identifier.</typeparam>
  25. public class GridFSFileInfoSerializer<TFileId> : BsonDocumentBackedClassSerializer<GridFSFileInfo<TFileId>>, IGridFSFileInfoSerializer<TFileId>
  26. {
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="GridFSFileInfoSerializer" /> class.
  30. /// </summary>
  31. public GridFSFileInfoSerializer()
  32. : this(BsonSerializer.LookupSerializer<TFileId>())
  33. {
  34. }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="GridFSFileInfoSerializer" /> class.
  37. /// </summary>
  38. /// <param name="idSerializer">The id serializer.</param>
  39. public GridFSFileInfoSerializer(IBsonSerializer<TFileId> idSerializer)
  40. {
  41. Ensure.IsNotNull(idSerializer, nameof(idSerializer));
  42. RegisterMember("Aliases", "aliases", new ArraySerializer<string>());
  43. RegisterMember("ChunkSizeBytes", "chunkSize", new Int32Serializer());
  44. RegisterMember("ContentType", "contentType", new StringSerializer());
  45. RegisterMember("Filename", "filename", new StringSerializer());
  46. RegisterMember("Id", "_id", idSerializer);
  47. RegisterMember("Length", "length", new Int64Serializer());
  48. RegisterMember("MD5", "md5", new StringSerializer());
  49. RegisterMember("Metadata", "metadata", BsonDocumentSerializer.Instance);
  50. RegisterMember("UploadDateTime", "uploadDate", new DateTimeSerializer());
  51. }
  52. // protected methods
  53. /// <inheritdoc/>
  54. protected override GridFSFileInfo<TFileId> CreateInstance(BsonDocument backingDocument)
  55. {
  56. return new GridFSFileInfo<TFileId>(backingDocument, this);
  57. }
  58. }
  59. }