ISerializationExpression.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 System;
  16. using System.Collections;
  17. using System.Reflection;
  18. using MongoDB.Bson;
  19. using MongoDB.Bson.IO;
  20. using MongoDB.Bson.Serialization;
  21. using MongoDB.Driver.Core.Misc;
  22. using MongoDB.Driver.Support;
  23. namespace MongoDB.Driver.Linq.Expressions
  24. {
  25. internal interface ISerializationExpression
  26. {
  27. IBsonSerializer Serializer { get; }
  28. Type Type { get; }
  29. }
  30. internal static class ISerializationExpressionExtensions
  31. {
  32. public static string AppendFieldName(this ISerializationExpression node, string suffix)
  33. {
  34. var field = node as IFieldExpression;
  35. return CombineFieldNames(field == null ? null : field.FieldName, suffix);
  36. }
  37. public static string PrependFieldName(this ISerializationExpression node, string prefix)
  38. {
  39. var field = node as IFieldExpression;
  40. return CombineFieldNames(prefix, field == null ? null : field.FieldName);
  41. }
  42. public static BsonValue SerializeValue(this ISerializationExpression field, Type valueType, object value)
  43. {
  44. Ensure.IsNotNull(field, nameof(field));
  45. var valueSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.SerializerRegistry, valueType, value);
  46. var tempDocument = new BsonDocument();
  47. using (var bsonWriter = new BsonDocumentWriter(tempDocument))
  48. {
  49. var context = BsonSerializationContext.CreateRoot(bsonWriter);
  50. bsonWriter.WriteStartDocument();
  51. bsonWriter.WriteName("value");
  52. valueSerializer.Serialize(context, value);
  53. bsonWriter.WriteEndDocument();
  54. return tempDocument[0];
  55. }
  56. }
  57. public static BsonArray SerializeValues(this ISerializationExpression field, Type itemType, IEnumerable values)
  58. {
  59. Ensure.IsNotNull(field, nameof(field));
  60. Ensure.IsNotNull(itemType, nameof(itemType));
  61. Ensure.IsNotNull(values, nameof(values));
  62. var itemSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.SerializerRegistry, itemType);
  63. var tempDocument = new BsonDocument();
  64. using (var bsonWriter = new BsonDocumentWriter(tempDocument))
  65. {
  66. var context = BsonSerializationContext.CreateRoot(bsonWriter);
  67. bsonWriter.WriteStartDocument();
  68. bsonWriter.WriteName("values");
  69. bsonWriter.WriteStartArray();
  70. foreach (var value in values)
  71. {
  72. itemSerializer.Serialize(context, value);
  73. }
  74. bsonWriter.WriteEndArray();
  75. bsonWriter.WriteEndDocument();
  76. return (BsonArray)tempDocument[0];
  77. }
  78. }
  79. private static string CombineFieldNames(string prefix, string suffix)
  80. {
  81. if (prefix == null)
  82. {
  83. return suffix;
  84. }
  85. if (suffix == null)
  86. {
  87. return prefix;
  88. }
  89. return prefix + "." + suffix;
  90. }
  91. }
  92. }