public GetSerializer ( |
||
actualType | The actual type of the member's value. | |
return | IBsonSerializer |
private object DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); return(null); } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); return(serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions)); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message); throw new FileFormatException(message, ex); } }
private void SerializeMember( BsonWriter bsonWriter, object obj, BsonMemberMap memberMap ) { var value = memberMap.Getter(obj); if (value == null && memberMap.IgnoreIfNull) { return; // don't serialize null value } if (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && object.Equals(value, memberMap.DefaultValue)) { return; // don't serialize default value } if (!memberMap.ShouldSerializeMethod(obj)) { return; // the ShouldSerializeMethod determined that the member shouldn't be serialized } bsonWriter.WriteName(memberMap.ElementName); var nominalType = memberMap.MemberType; var actualType = (value == null) ? nominalType : value.GetType(); var serializer = memberMap.GetSerializer(actualType); serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions); }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; Type actualType; if (bsonReader.CurrentBsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); var value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }
private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap) { var value = memberMap.Getter(obj); if (!memberMap.ShouldSerialize(obj, value)) { return; // don't serialize member } bsonWriter.WriteName(memberMap.ElementName); var nominalType = memberMap.MemberType; if (value == null && nominalType.IsInterface) { bsonWriter.WriteNull(); } else if (value == null && memberMap.MemberTypeIsBsonValue) { bsonWriter.WriteStartDocument(); bsonWriter.WriteBoolean("_csharpnull", true); bsonWriter.WriteEndDocument(); } else { var actualType = (value == null) ? nominalType : value.GetType(); var serializer = memberMap.GetSerializer(actualType); serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions); } }
public void Apply(BsonMemberMap memberMap) { if (memberMap.MemberType.IsEnum) { var serializer = memberMap.GetSerializer(); var camelCaseValueSerializer = new CamelCaseValueSerializer(serializer); memberMap.SetSerializer(camelCaseValueSerializer); } }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; object value = null; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); goto setvalue; } else if (memberMap.MemberTypeIsBsonValue) { if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader)) { // if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null goto setvalue; } // handle BSON null for backward compatibility with existing data (new data would have _csharpnull) if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull))) { bsonReader.ReadNull(); goto setvalue; } } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); setvalue: memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }
private void SerializeNormalMember(BsonSerializationContext context, object obj, BsonMemberMap memberMap) { var bsonWriter = context.Writer; var value = memberMap.Getter(obj); if (!memberMap.ShouldSerialize(obj, value)) { return; // don't serialize member } bsonWriter.WriteName(memberMap.ElementName); memberMap.GetSerializer().Serialize(context, value); }
private object DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap) { var bsonReader = context.Reader; try { return(memberMap.GetSerializer().Deserialize(context)); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo is FieldInfo) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message); throw new FormatException(message, ex); } }
private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap) { var value = memberMap.Getter(obj); if (!memberMap.ShouldSerialize(obj, value)) { return; // don't serialize member } bsonWriter.WriteName(memberMap.ElementName); var nominalType = memberMap.MemberType; var actualType = (value == null) ? nominalType : value.GetType(); var serializer = memberMap.GetSerializer(actualType); serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions); }
// public methods /// <summary> /// Applies a modification to the member map. /// </summary> /// <param name="memberMap">The member map.</param> public virtual void Apply(BsonMemberMap memberMap) { var memberSerializer = memberMap.GetSerializer(memberMap.MemberType); var memberSerializationOptions = memberMap.SerializationOptions; if (memberSerializationOptions == null) { var memberDefaultSerializationOptions = memberSerializer.GetDefaultSerializationOptions(); if (memberDefaultSerializationOptions == null) { var message = string.Format( "A serialization options attribute of type {0} cannot be used when the serializer is of type {1}.", BsonUtils.GetFriendlyTypeName(this.GetType()), BsonUtils.GetFriendlyTypeName(memberSerializer.GetType())); throw new NotSupportedException(message); } memberSerializationOptions = memberDefaultSerializationOptions.Clone(); memberMap.SetSerializationOptions(memberSerializationOptions); } memberSerializationOptions.ApplyAttribute(memberSerializer, this); }
private object SerializeValue(object value, BsonMemberMap memberMap) { const string tmpField = "tmp"; var type = value.GetType(); var serializer = memberMap.GetSerializer(type); var document = new BsonDocument(); using (var writer = BsonWriter.Create(document)) { // serialize the value inside a document using the provided serializer writer.WriteStartDocument(); writer.WriteName(tmpField); serializer.Serialize(writer, type, value, memberMap.SerializationOptions); writer.WriteEndDocument(); } // extract the serialized value from the document return document[tmpField]; }
private void DeserializeMember( BsonReader bsonReader, object obj, BsonMemberMap memberMap ) { var nominalType = memberMap.MemberType; Type actualType; if (bsonReader.CurrentBsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); var value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); memberMap.Setter(obj, value); }
private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap) { var value = memberMap.Getter(obj); if (!memberMap.ShouldSerialize(obj, value)) { return; // don't serialize member } bsonWriter.WriteName(memberMap.ElementName); var nominalType = memberMap.MemberType; var actualType = (value == null) ? nominalType : value.GetType(); var serializer = memberMap.GetSerializer(actualType); serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions); }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; Type actualType; if (bsonReader.CurrentBsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); var value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { Type memberType = memberMap.MemberType; Type actualType = bsonReader.CurrentBsonType != BsonType.Null ? BsonDefaultSerializer.LookupDiscriminatorConvention(memberType).GetActualType(bsonReader, memberType) : memberType; object obj1 = memberMap.GetSerializer(actualType).Deserialize(bsonReader, memberType, actualType, memberMap.SerializationOptions); // --- Added if (IsRelation(memberMap)) { ValidateVirtualRelation(memberMap); if (obj1 != null) if (IsReferencesManyRelation(memberMap)) ((IDocument)obj).ReferencesManyIDs[memberMap.MemberName] = ((IEnumerable)obj1).Cast<ObjectId>().ToList(); else ((IDocument) obj).ReferencesOneIDs[memberMap.MemberName] = (ObjectId) obj1; } // --- Added else memberMap.Setter(obj, obj1); } catch (Exception ex) { throw new FileFormatException(string.Format("An error occurred while deserializing the {0} {1} of class {2}: {3}", (object)memberMap.MemberName, memberMap.MemberInfo.MemberType == MemberTypes.Field ? (object)"field" : (object)"property", (object)obj.GetType().FullName, (object)ex.Message), ex); } }
private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap) { object objA = memberMap.Getter(obj); if (objA == null && memberMap.IgnoreIfNull || (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && object.Equals(objA, memberMap.DefaultValue) || !memberMap.ShouldSerializeMethod(obj))) return; bsonWriter.WriteName(memberMap.ElementName); Type memberType = memberMap.MemberType; Type actualType = objA == null ? memberType : objA.GetType(); if (IsRelation(memberMap)) ValidateVirtualRelation(memberMap); memberMap.GetSerializer(actualType).Serialize(bsonWriter, memberType, objA, memberMap.SerializationOptions); }
private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap) { var value = memberMap.Getter(obj); if (!memberMap.ShouldSerialize(obj, value)) { return; // don't serialize member } bsonWriter.WriteName(memberMap.ElementName); var nominalType = memberMap.MemberType; if (value == null && nominalType.IsInterface) { bsonWriter.WriteNull(); } else if (value == null && memberMap.MemberTypeIsBsonValue) { bsonWriter.WriteStartDocument(); bsonWriter.WriteBoolean("_csharpnull", true); bsonWriter.WriteEndDocument(); } else { var actualType = (value == null) ? nominalType : value.GetType(); var serializer = memberMap.GetSerializer(actualType); serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions); } }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; object value = null; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); goto setvalue; } else if (memberMap.MemberTypeIsBsonValue) { if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader)) { // if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null goto setvalue; } // handle BSON null for backward compatibility with existing data (new data would have _csharpnull) if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull))) { bsonReader.ReadNull(); goto setvalue; } } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); setvalue: memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }
private void SerializeMember( BsonWriter bsonWriter, object obj, BsonMemberMap memberMap ) { var value = memberMap.Getter(obj); if (value == null && memberMap.IgnoreIfNull) { return; // don't serialize null value } if (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && object.Equals(value, memberMap.DefaultValue)) { return; // don't serialize default value } if (!memberMap.ShouldSerializeMethod(obj)) { return; // the ShouldSerializeMethod determined that the member shouldn't be serialized } bsonWriter.WriteName(memberMap.ElementName); var nominalType = memberMap.MemberType; var actualType = (value == null) ? nominalType : value.GetType(); var serializer = memberMap.GetSerializer(actualType); serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions); }
private void DeserializeMember( BsonReader bsonReader, object obj, BsonMemberMap memberMap ) { var nominalType = memberMap.MemberType; Type actualType; if (bsonReader.CurrentBsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); var value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); memberMap.Setter(obj, value); }
private object DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); return null; } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); return serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message); throw new Exception(message, ex); } }
public void Apply(BsonMemberMap memberMap) { memberMap.SetSerializer(ConfigureSerializer(memberMap.GetSerializer())); }