|
| 1 | +/* Copyright 2016 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 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Reflection; |
| 19 | +using MongoDB.Bson; |
| 20 | +using MongoDB.Bson.Serialization; |
| 21 | +using MongoDB.Bson.Serialization.Serializers; |
| 22 | +using MongoDB.Driver.Support; |
| 23 | + |
| 24 | +namespace MongoDB.Driver |
| 25 | +{ |
| 26 | + internal static class FieldValueSerializerHelper |
| 27 | + { |
| 28 | + public static IBsonSerializer GetSerializerForValueType(IBsonSerializer fieldSerializer, Type valueType) |
| 29 | + { |
| 30 | + var fieldType = fieldSerializer.ValueType; |
| 31 | + |
| 32 | + // these will normally be equal unless we've removed some Convert(s) that the compiler put in |
| 33 | + if (fieldType == valueType) |
| 34 | + { |
| 35 | + return fieldSerializer; |
| 36 | + } |
| 37 | + |
| 38 | + // serialize numeric values without converting them |
| 39 | + if (fieldType.IsNumeric() && valueType.IsNumeric()) |
| 40 | + { |
| 41 | + var valueSerializer = BsonSerializer.SerializerRegistry.GetSerializer(valueType); |
| 42 | + if (HasStringRepresentation(fieldSerializer)) |
| 43 | + { |
| 44 | + valueSerializer = WithStringRepresentation(valueSerializer); |
| 45 | + } |
| 46 | + return valueSerializer; |
| 47 | + } |
| 48 | + |
| 49 | + var fieldTypeInfo = fieldType.GetTypeInfo(); |
| 50 | + var fieldSerializerInterfaceType = typeof(IBsonSerializer<>).MakeGenericType(fieldType); |
| 51 | + var valueTypeInfo = valueType.GetTypeInfo(); |
| 52 | + |
| 53 | + // synthesize a NullableSerializer using the field serializer |
| 54 | + if (valueType.IsNullable() && valueType.GetNullableUnderlyingType() == fieldType) |
| 55 | + { |
| 56 | + var nullableSerializerType = typeof(NullableSerializer<>).MakeGenericType(fieldType); |
| 57 | + var nullableSerializerConstructor = nullableSerializerType.GetTypeInfo().GetConstructor(new[] { fieldSerializerInterfaceType }); |
| 58 | + return (IBsonSerializer)nullableSerializerConstructor.Invoke(new object[] { fieldSerializer }); |
| 59 | + } |
| 60 | + |
| 61 | + // synthesize an EnumConvertingSerializer using the field serializer |
| 62 | + if (fieldTypeInfo.IsEnum) |
| 63 | + { |
| 64 | + var enumConvertingSerializerType = typeof(EnumConvertingSerializer<,>).MakeGenericType(valueType, fieldType); |
| 65 | + var enumConvertingSerializerConstructor = enumConvertingSerializerType.GetTypeInfo().GetConstructor(new[] { fieldSerializerInterfaceType }); |
| 66 | + return (IBsonSerializer)enumConvertingSerializerConstructor.Invoke(new object[] { fieldSerializer }); |
| 67 | + } |
| 68 | + |
| 69 | + // synthesize a NullableEnumConvertingSerializer using the field serializer |
| 70 | + if (fieldType.IsNullableEnum() && valueType.IsNullable()) |
| 71 | + { |
| 72 | + var nonNullableFieldType = fieldType.GetNullableUnderlyingType(); |
| 73 | + var nonNullableValueType = valueType.GetNullableUnderlyingType(); |
| 74 | + var nonNullableFieldSerializer = ((IChildSerializerConfigurable)fieldSerializer).ChildSerializer; |
| 75 | + var nonNullableFieldSerializerInterfaceType = typeof(IBsonSerializer<>).MakeGenericType(nonNullableFieldType); |
| 76 | + var nullableEnumConvertingSerializerType = typeof(NullableEnumConvertingSerializer<,>).MakeGenericType(nonNullableValueType, nonNullableFieldType); |
| 77 | + var nullableEnumConvertingSerializerConstructor = nullableEnumConvertingSerializerType.GetTypeInfo().GetConstructor(new[] { nonNullableFieldSerializerInterfaceType }); |
| 78 | + return (IBsonSerializer)nullableEnumConvertingSerializerConstructor.Invoke(new object[] { nonNullableFieldSerializer }); |
| 79 | + } |
| 80 | + |
| 81 | + // synthesize an IEnumerableSerializer serializer using the item serializer from the field serializer |
| 82 | + Type fieldIEnumerableInterfaceType; |
| 83 | + Type valueIEnumerableInterfaceType; |
| 84 | + Type itemType; |
| 85 | + if ( |
| 86 | + (fieldIEnumerableInterfaceType = fieldType.FindIEnumerable()) != null && |
| 87 | + (valueIEnumerableInterfaceType = valueType.FindIEnumerable()) != null && |
| 88 | + (itemType = fieldIEnumerableInterfaceType.GetSequenceElementType()) == valueIEnumerableInterfaceType.GetSequenceElementType() && |
| 89 | + fieldSerializer is IChildSerializerConfigurable) |
| 90 | + { |
| 91 | + var itemSerializer = ((IChildSerializerConfigurable)fieldSerializer).ChildSerializer; |
| 92 | + var itemSerializerInterfaceType = typeof(IBsonSerializer<>).MakeGenericType(itemType); |
| 93 | + var ienumerableSerializerType = typeof(IEnumerableSerializer<>).MakeGenericType(itemType); |
| 94 | + var ienumerableSerializerConstructor = ienumerableSerializerType.GetTypeInfo().GetConstructor(new[] { itemSerializerInterfaceType }); |
| 95 | + return (IBsonSerializer)ienumerableSerializerConstructor.Invoke(new object[] { itemSerializer }); |
| 96 | + } |
| 97 | + |
| 98 | + // otherwise assume that the value can be cast to the right type for the field serializer |
| 99 | + var castingSerializerType = typeof(CastingSerializer<,>).MakeGenericType(valueType, fieldType); |
| 100 | + var castingSerializerConstructor = castingSerializerType.GetTypeInfo().GetConstructor(new[] { fieldSerializerInterfaceType }); |
| 101 | + return (IBsonSerializer)castingSerializerConstructor.Invoke(new object[] { fieldSerializer }); |
| 102 | + } |
| 103 | + |
| 104 | + public static IBsonSerializer<TValue> GetSerializerForValueType<TField, TValue>(IBsonSerializer<TField> fieldSerializer) |
| 105 | + { |
| 106 | + return (IBsonSerializer<TValue>)GetSerializerForValueType(fieldSerializer, typeof(TValue)); |
| 107 | + } |
| 108 | + |
| 109 | + // private static methods |
| 110 | + private static bool HasStringRepresentation(IBsonSerializer serializer) |
| 111 | + { |
| 112 | + var configurableSerializer = serializer as IRepresentationConfigurable; |
| 113 | + if (configurableSerializer != null) |
| 114 | + { |
| 115 | + return configurableSerializer.Representation == BsonType.String; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static IBsonSerializer WithStringRepresentation(IBsonSerializer serializer) |
| 124 | + { |
| 125 | + var configurableSerializer = serializer as IRepresentationConfigurable; |
| 126 | + if (configurableSerializer != null) |
| 127 | + { |
| 128 | + return configurableSerializer.WithRepresentation(BsonType.String); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + return serializer; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // nested types |
| 137 | + private class CastingSerializer<TFrom, TTo> : SerializerBase<TFrom> |
| 138 | + { |
| 139 | + private readonly IBsonSerializer<TTo> _serializer; |
| 140 | + |
| 141 | + public CastingSerializer(IBsonSerializer<TTo> serializer) |
| 142 | + { |
| 143 | + _serializer = serializer; |
| 144 | + } |
| 145 | + |
| 146 | + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TFrom value) |
| 147 | + { |
| 148 | + _serializer.Serialize(context, args, (TTo)(object)value); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private class EnumConvertingSerializer<TFrom, TTo> : SerializerBase<TFrom> |
| 153 | + { |
| 154 | + private readonly IBsonSerializer<TTo> _serializer; |
| 155 | + |
| 156 | + public EnumConvertingSerializer(IBsonSerializer<TTo> serializer) |
| 157 | + { |
| 158 | + _serializer = serializer; |
| 159 | + } |
| 160 | + |
| 161 | + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TFrom value) |
| 162 | + { |
| 163 | + _serializer.Serialize(context, args, (TTo)Enum.ToObject(typeof(TTo), (object)value)); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private class IEnumerableSerializer<TItem> : SerializerBase<IEnumerable<TItem>> |
| 168 | + { |
| 169 | + private readonly IBsonSerializer<TItem> _itemSerializer; |
| 170 | + |
| 171 | + public IEnumerableSerializer(IBsonSerializer<TItem> itemSerializer) |
| 172 | + { |
| 173 | + _itemSerializer = itemSerializer; |
| 174 | + } |
| 175 | + |
| 176 | + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IEnumerable<TItem> value) |
| 177 | + { |
| 178 | + var bsonWriter = context.Writer; |
| 179 | + if (value == null) |
| 180 | + { |
| 181 | + bsonWriter.WriteNull(); |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + bsonWriter.WriteStartArray(); |
| 186 | + foreach (var item in value) |
| 187 | + { |
| 188 | + _itemSerializer.Serialize(context, item); |
| 189 | + } |
| 190 | + bsonWriter.WriteEndArray(); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private class NullableEnumConvertingSerializer<TFrom, TTo> : SerializerBase<Nullable<TFrom>> where TFrom : struct where TTo : struct |
| 196 | + { |
| 197 | + private readonly IBsonSerializer<TTo> _nonNullableEnumSerializer; |
| 198 | + |
| 199 | + public NullableEnumConvertingSerializer(IBsonSerializer<TTo> nonNullableEnumSerializer) |
| 200 | + { |
| 201 | + _nonNullableEnumSerializer = nonNullableEnumSerializer; |
| 202 | + } |
| 203 | + |
| 204 | + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Nullable<TFrom> value) |
| 205 | + { |
| 206 | + if (value == null) |
| 207 | + { |
| 208 | + context.Writer.WriteNull(); |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + _nonNullableEnumSerializer.Serialize(context, args, (TTo)Enum.ToObject(typeof(TTo), (object)value.Value)); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments