public static void Test_Serialize_Dictionary_02(DictionaryRepresentation dictionaryRepresentation) { // json Dictionary // DictionaryRepresentation.Dynamic or DictionaryRepresentation.Document // { "toto1" : "tata1", "toto2" : "tata2" } // DictionaryRepresentation.ArrayOfArrays // [["toto1", "tata1"], ["toto2", "tata2"]] // DictionaryRepresentation.ArrayOfDocuments // [{ "k" : "toto1", "v" : "tata1" }, { "k" : "toto2", "v" : "tata2" }] Trace.WriteLine(); Trace.WriteLine("Test_Serialize_Dictionary_02"); Dictionary<string, string> dictionary = new Dictionary<string, string>(); dictionary.Add("toto1", "tata1"); dictionary.Add("toto2", "tata2"); //DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation.ArrayOfDocuments; Trace.WriteLine("DictionaryRepresentation : {0}", dictionaryRepresentation); Trace.WriteLine("Dictionary json :"); string json = dictionary.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json); Trace.WriteLine("Deserialize json :"); Dictionary<string, string> dictionary2 = BsonSerializer.Deserialize<Dictionary<string, string>>(json); string json2 = dictionary2.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json2); Trace.WriteLine("comparison of Dictionary json and Deserialize json : {0}", json == json2 ? "identical" : "different"); }
public void Apply(BsonMemberMap memberMap) { Type memberType = memberMap.MemberType; TypeInfo typeInfo = memberType.GetTypeInfo(); if (typeInfo.Name == "Dictionary`2") { Type keyType = typeInfo.GenericTypeArguments[0]; DictionaryRepresentation representation = DictionaryRepresentation.Document; if (keyType != typeof(string)) { representation = DictionaryRepresentation.ArrayOfDocuments; } var serializer = memberMap.GetSerializer(); var dictionaryRepresentationConfigurable = serializer as IDictionaryRepresentationConfigurable; if (dictionaryRepresentationConfigurable != null && dictionaryRepresentationConfigurable.DictionaryRepresentation != representation) { var reconfiguredSerializer = dictionaryRepresentationConfigurable.WithDictionaryRepresentation(representation); memberMap.SetSerializer(reconfiguredSerializer); } return; } }
public static void Test_Serialize_Dictionary_02(DictionaryRepresentation dictionaryRepresentation) { // json Dictionary // DictionaryRepresentation.Dynamic or DictionaryRepresentation.Document // { "toto1" : "tata1", "toto2" : "tata2" } // DictionaryRepresentation.ArrayOfArrays // [["toto1", "tata1"], ["toto2", "tata2"]] // DictionaryRepresentation.ArrayOfDocuments // [{ "k" : "toto1", "v" : "tata1" }, { "k" : "toto2", "v" : "tata2" }] Trace.WriteLine(); Trace.WriteLine("Test_Serialize_Dictionary_02"); Dictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("toto1", "tata1"); dictionary.Add("toto2", "tata2"); //DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation.ArrayOfDocuments; Trace.WriteLine("DictionaryRepresentation : {0}", dictionaryRepresentation); Trace.WriteLine("Dictionary json :"); string json = dictionary.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json); Trace.WriteLine("Deserialize json :"); Dictionary <string, string> dictionary2 = BsonSerializer.Deserialize <Dictionary <string, string> >(json); string json2 = dictionary2.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json2); Trace.WriteLine("comparison of Dictionary json and Deserialize json : {0}", json == json2 ? "identical" : "different"); }
// public methods /// <summary> /// Apply an attribute to these serialization options and modify the options accordingly. /// </summary> /// <param name="serializer">The serializer that these serialization options are for.</param> /// <param name="attribute">The serialization options attribute.</param> public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute) { EnsureNotFrozen(); var dictionaryOptionsAttribute = attribute as BsonDictionaryOptionsAttribute; if (dictionaryOptionsAttribute != null) { _representation = dictionaryOptionsAttribute.Representation; return; } // for backward compatibility reasons representations Array and Document apply to the Dictionary and not the items var representationAttribute = attribute as BsonRepresentationAttribute; if (representationAttribute != null) { switch (representationAttribute.Representation) { case BsonType.Array: _representation = DictionaryRepresentation.ArrayOfArrays; return; case BsonType.Document: _representation = DictionaryRepresentation.Document; return; } } var itemSerializer = serializer.GetItemSerializationInfo().Serializer; if (_itemSerializationOptions == null) { var itemDefaultSerializationOptions = itemSerializer.GetDefaultSerializationOptions(); // special case for legacy dictionaries: allow BsonRepresentation on object if (itemDefaultSerializationOptions == null && serializer.GetType() == typeof(DictionarySerializer) && attribute.GetType() == typeof(BsonRepresentationAttribute)) { itemDefaultSerializationOptions = new RepresentationSerializationOptions(BsonType.Null); // will be modified later by ApplyAttribute } if (itemDefaultSerializationOptions == null) { var message = string.Format( "A serialization options attribute of type {0} cannot be used when the serializer is of type {1} and the item serializer is of type {2}.", BsonUtils.GetFriendlyTypeName(attribute.GetType()), BsonUtils.GetFriendlyTypeName(serializer.GetType()), BsonUtils.GetFriendlyTypeName(itemSerializer.GetType())); throw new NotSupportedException(message); } _itemSerializationOptions = itemDefaultSerializationOptions.Clone(); } _itemSerializationOptions.ApplyAttribute(itemSerializer, attribute); }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> /// <param name="keyValuePairSerializationOptions">The serialization options for the key/value pairs in the dictionary.</param> public DictionarySerializationOptions(DictionaryRepresentation representation, KeyValuePairSerializationOptions keyValuePairSerializationOptions) { if (keyValuePairSerializationOptions == null) { throw new ArgumentNullException("keyValuePairSerializationOptions"); } _representation = representation; _keyValuePairSerializationOptions = keyValuePairSerializationOptions; }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> /// <param name="keyValuePairSerializationOptions">The serialization options for the key/value pairs in the dictionary.</param> public DictionarySerializationOptions(DictionaryRepresentation representation, KeyValuePairSerializationOptions keyValuePairSerializationOptions) { if (keyValuePairSerializationOptions == null) { throw new ArgumentNullException("keyValuePairSerializationOptions"); } _representation = representation; _keyValuePairSerializationOptions = keyValuePairSerializationOptions; }
/// <summary> /// Initializes a new instance of the <see cref="DictionaryBsonSerializer{TDictionary,TKey,TValue}"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> public DictionaryBsonSerializer( DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) { if (!typeof(TDictionary).IsClosedSystemDictionaryType()) { throw new ArgumentException("'typeof(TDictionary).IsSystemDictionaryType()' is false"); } this.underlyingSerializer = new DictionaryInterfaceImplementerSerializer <Dictionary <TKey, TValue> >(dictionaryRepresentation, keySerializer, valueSerializer); }
/// <summary> /// Initializes a new instance of the <see cref="DictionarySerializerBase{TDictionary}"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> public DictionarySerializerBase(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) { _dictionaryRepresentation = dictionaryRepresentation; _keySerializer = keySerializer; _valueSerializer = valueSerializer; _helper = new SerializerHelper ( new SerializerHelper.Member("k", Flags.Key), new SerializerHelper.Member("v", Flags.Value) ); }
public static void Test_Serialize_03(DictionaryRepresentation dictionaryRepresentation) { Trace.WriteLine("BsonPBSerializationProvider.RegisterProvider()"); BsonPBSerializationProvider.RegisterProvider(); PBDictionarySerializer.RegisterGenericDictionarySerializer(); try { PBDictionarySerializer.DefaultDictionaryRepresentation = dictionaryRepresentation; Test_Bson_Class_02 test = Create_Test_Bson_Class_02(); BsonDocument document = test.zToBsonDocument(); Trace.WriteLine(document.zToJson()); } finally { Trace.WriteLine("BsonPBSerializationProvider.UnregisterProvider()"); BsonPBSerializationProvider.UnregisterProvider(); PBDictionarySerializer.UnregisterGenericDictionarySerializer(); } }
public static void Test_Serialize_NameValueCollection_02(DictionaryRepresentation dictionaryRepresentation) { Trace.WriteLine(); Trace.WriteLine("Test_Serialize_NameValueCollection_02"); //if (BsonSerializer.zIsSerializerRegistered(typeof(NameValueCollection))) //{ // Trace.WriteLine("UnregisterSerializer(typeof(NameValueCollection)"); // BsonSerializer.zUnregisterSerializer(typeof(NameValueCollection)); //} //if (!BsonSerializer.zIsSerializerRegistered(typeof(NameValueCollection))) //{ // Trace.WriteLine("RegisterSerializer(typeof(NameValueCollection), new NameValueCollectionSerializer())"); // BsonSerializer.RegisterSerializer(typeof(NameValueCollection), new NameValueCollectionSerializer()); //} //RegisterNameValueCollectionSerializer(); //RegisterBsonPBSerializationProvider(); BsonPBSerializationProvider.RegisterProvider(); try { NameValueCollection nameValues = new NameValueCollection(); nameValues.Add("toto1", "tata1"); nameValues.Add("toto2", "tata2"); Trace.WriteLine("DictionaryRepresentation : {0}", dictionaryRepresentation); Trace.WriteLine("NameValueCollection json :"); string json = nameValues.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json); Trace.WriteLine("Deserialize json :"); NameValueCollection nameValues2 = BsonSerializer.Deserialize<NameValueCollection>(json); string json2 = nameValues2.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json2); Trace.WriteLine("comparison of NameValueCollection json and Deserialize json : {0}", json == json2 ? "identical" : "different"); } finally { //UnregisterBsonPBSerializationProvider(); BsonPBSerializationProvider.UnregisterProvider(); } }
public static void Test_Serialize_NameValueCollection_02(DictionaryRepresentation dictionaryRepresentation) { Trace.WriteLine(); Trace.WriteLine("Test_Serialize_NameValueCollection_02"); //if (BsonSerializer.zIsSerializerRegistered(typeof(NameValueCollection))) //{ // Trace.WriteLine("UnregisterSerializer(typeof(NameValueCollection)"); // BsonSerializer.zUnregisterSerializer(typeof(NameValueCollection)); //} //if (!BsonSerializer.zIsSerializerRegistered(typeof(NameValueCollection))) //{ // Trace.WriteLine("RegisterSerializer(typeof(NameValueCollection), new NameValueCollectionSerializer())"); // BsonSerializer.RegisterSerializer(typeof(NameValueCollection), new NameValueCollectionSerializer()); //} //RegisterNameValueCollectionSerializer(); //RegisterBsonPBSerializationProvider(); BsonPBSerializationProvider.RegisterProvider(); try { NameValueCollection nameValues = new NameValueCollection(); nameValues.Add("toto1", "tata1"); nameValues.Add("toto2", "tata2"); Trace.WriteLine("DictionaryRepresentation : {0}", dictionaryRepresentation); Trace.WriteLine("NameValueCollection json :"); string json = nameValues.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json); Trace.WriteLine("Deserialize json :"); NameValueCollection nameValues2 = BsonSerializer.Deserialize <NameValueCollection>(json); string json2 = nameValues2.ToJson(new DictionarySerializationOptions(dictionaryRepresentation)); Trace.WriteLine(json2); Trace.WriteLine("comparison of NameValueCollection json and Deserialize json : {0}", json == json2 ? "identical" : "different"); } finally { //UnregisterBsonPBSerializationProvider(); BsonPBSerializationProvider.UnregisterProvider(); } }
public static void Test_Serialize_04(DictionaryRepresentation dictionaryRepresentation) { Trace.WriteLine("BsonPBSerializationProvider.RegisterProvider()"); BsonPBSerializationProvider.RegisterProvider(); PBDictionarySerializer.RegisterGenericDictionarySerializer(); try { //PBDictionarySerializer.DefaultDictionaryRepresentation = dictionaryRepresentation; DictionarySerializationOptions options = new DictionarySerializationOptions(dictionaryRepresentation); Dictionary <string, ZValue> infos2 = Create_Dictionary_string_ZValue_01(); //BsonDocument document = infos2.zToBsonDocument(options); //Trace.WriteLine(document.zToJson()); Trace.WriteLine("dictionaryRepresentation : {0}", dictionaryRepresentation); string json = infos2.ToJson(options); Trace.WriteLine(json); } finally { Trace.WriteLine("BsonPBSerializationProvider.UnregisterProvider()"); BsonPBSerializationProvider.UnregisterProvider(); PBDictionarySerializer.UnregisterGenericDictionarySerializer(); } }
// public methods /// <summary> /// Returns a serializer that has been reconfigured with the specified dictionary representation. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <returns>The reconfigured serializer.</returns> public ReadOnlyDictionaryInterfaceImplementerSerializer <TDictionary, TKey, TValue> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation) { return(dictionaryRepresentation == DictionaryRepresentation ? this : new ReadOnlyDictionaryInterfaceImplementerSerializer <TDictionary, TKey, TValue>(dictionaryRepresentation, KeySerializer, ValueSerializer)); }
public BsonDictionaryOptionsAttribute(DictionaryRepresentation dictionaryRepresentation) { }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> public DictionarySerializationOptions(DictionaryRepresentation representation) { _representation = representation; _keyValuePairSerializationOptions = (KeyValuePairSerializationOptions)KeyValuePairSerializationOptions.Defaults.Clone(); }
public DictionaryRepresentationConvention(DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation.ArrayOfDocuments) { // see http://mongodb.github.io/mongo-csharp-driver/2.2/reference/bson/mapping/#dictionary-serialization-options _dictionaryRepresentation = dictionaryRepresentation; }
public DictionaryRepresentationConvention(DictionaryRepresentation dictionaryRepresentation) { _dictionaryRepresentation = dictionaryRepresentation; }
// public methods /// <summary> /// Returns a serializer that has been reconfigured with the specified dictionary representation. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <returns>The reconfigured serializer.</returns> public DictionaryInterfaceImplementerSerializer <TDictionary> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation) { if (dictionaryRepresentation == DictionaryRepresentation) { return(this); } else { return(new DictionaryInterfaceImplementerSerializer <TDictionary>(dictionaryRepresentation, KeySerializer, ValueSerializer)); } }
/// <summary> /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary}"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation) : base(dictionaryRepresentation) { }
// public methods /// <summary> /// Apply an attribute to these serialization options and modify the options accordingly. /// </summary> /// <param name="serializer">The serializer that these serialization options are for.</param> /// <param name="attribute">The serialization options attribute.</param> public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute) { EnsureNotFrozen(); var dictionaryOptionsAttribute = attribute as BsonDictionaryOptionsAttribute; if (dictionaryOptionsAttribute != null) { _representation = dictionaryOptionsAttribute.Representation; return; } // for backward compatibility reasons representations Array and Document apply to the Dictionary and not the items var representationAttribute = attribute as BsonRepresentationAttribute; if (representationAttribute != null) { switch (representationAttribute.Representation) { case BsonType.Array: _representation = DictionaryRepresentation.ArrayOfArrays; return; case BsonType.Document: _representation = DictionaryRepresentation.Document; return; } } var itemSerializer = serializer.GetItemSerializationInfo().Serializer; if (_itemSerializationOptions == null) { var itemDefaultSerializationOptions = itemSerializer.GetDefaultSerializationOptions(); // special case for legacy dictionaries: allow BsonRepresentation on object if (itemDefaultSerializationOptions == null && serializer.GetType() == typeof(DictionarySerializer) && attribute.GetType() == typeof(BsonRepresentationAttribute)) { itemDefaultSerializationOptions = new RepresentationSerializationOptions(BsonType.Null); // will be modified later by ApplyAttribute } if (itemDefaultSerializationOptions == null) { var message = string.Format( "A serialization options attribute of type {0} cannot be used when the serializer is of type {1} and the item serializer is of type {2}.", BsonUtils.GetFriendlyTypeName(attribute.GetType()), BsonUtils.GetFriendlyTypeName(serializer.GetType()), BsonUtils.GetFriendlyTypeName(itemSerializer.GetType())); throw new NotSupportedException(message); } _itemSerializationOptions = itemDefaultSerializationOptions.Clone(); } _itemSerializationOptions.ApplyAttribute(itemSerializer, attribute); }
/// <summary> /// Initializes a new instance of the BsonDictionaryOptionsAttribute class. /// </summary> /// <param name="representation">The representation to use for the Dictionary.</param> public BsonDictionaryOptionsAttribute(DictionaryRepresentation representation) { this.representation = representation; }
/// <summary> /// Initializes a new instance of the <see cref="NullNaosDictionarySerializer"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> // ReSharper disable once InheritdocConsiderUsage public NullNaosDictionarySerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) : base(dictionaryRepresentation, keySerializer, valueSerializer) { throw new NotSupportedException("The null dictionary serializer is not intended for use."); }
/// <summary> /// Initializes a new instance of the <see cref="NaosDictionarySerializer{T, TKey,TValue}"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> public NaosDictionarySerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) { DeserializationConverterFuncBySerializedType.ContainsKey(typeof(TDictionary)).Named(Invariant($"{typeof(TDictionary)}-mustBeSupportedDictionaryType")).Must().BeTrue(); this.underlyingSerializer = new DictionaryInterfaceImplementerSerializer <Dictionary <TKey, TValue> >(dictionaryRepresentation, keySerializer, valueSerializer); }
/// <summary> /// Returns a serializer that has been reconfigured with the specified dictionary representation and key value serializers. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> /// <returns>The reconfigured serializer.</returns> public ReadOnlyDictionaryInterfaceImplementerSerializer <TDictionary, TKey, TValue> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer <TKey> keySerializer, IBsonSerializer <TValue> valueSerializer) { return(dictionaryRepresentation == DictionaryRepresentation && keySerializer == KeySerializer && valueSerializer == ValueSerializer ? this : new ReadOnlyDictionaryInterfaceImplementerSerializer <TDictionary, TKey, TValue>(dictionaryRepresentation, keySerializer, valueSerializer)); }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> /// <param name="itemSerializationOptions">The serialization options for the items in the dictionary.</param> public DictionarySerializationOptions(DictionaryRepresentation representation, IBsonSerializationOptions itemSerializationOptions) { _representation = representation; _itemSerializationOptions = itemSerializationOptions; }
IBsonSerializer IDictionaryRepresentationConfigurable.WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation) { return(WithDictionaryRepresentation(dictionaryRepresentation)); }
/// <summary> /// Initializes a new instance of the <see cref="DictionarySerializerBase{TDictionary}"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> public DictionarySerializerBase(DictionaryRepresentation dictionaryRepresentation) : this(dictionaryRepresentation, new ObjectSerializer(), new ObjectSerializer()) { }
/// <summary> /// Initializes a new instance of the <see cref="DictionaryInterfaceImplementerSerializer{TDictionary}"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) : base(dictionaryRepresentation, keySerializer, valueSerializer) { }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> /// <param name="itemSerializationOptions">The serialization options for the items in the dictionary.</param> public DictionarySerializationOptions(DictionaryRepresentation representation, IBsonSerializationOptions itemSerializationOptions) { _representation = representation; _itemSerializationOptions = itemSerializationOptions; }
/// <summary> /// Returns a serializer that has been reconfigured with the specified dictionary representation and key value serializers. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> /// <returns>The reconfigured serializer.</returns> public DictionaryInterfaceImplementerSerializer <TDictionary> WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) { if (dictionaryRepresentation == DictionaryRepresentation && keySerializer == KeySerializer && valueSerializer == ValueSerializer) { return(this); } else { return(new DictionaryInterfaceImplementerSerializer <TDictionary>(dictionaryRepresentation, keySerializer, valueSerializer)); } }
public SafeDictionaryKeyConvention(DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation.ArrayOfDocuments) { _dictionaryRepresentation = dictionaryRepresentation; }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> public DictionarySerializationOptions( DictionaryRepresentation representation ) { this.representation = representation; }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> public DictionarySerializationOptions(DictionaryRepresentation representation) { this.representation = representation; }
// public methods /// <summary> /// Apply an attribute to these serialization options and modify the options accordingly. /// </summary> /// <param name="serializer">The serializer that these serialization options are for.</param> /// <param name="attribute">The serialization options attribute.</param> public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute) { EnsureNotFrozen(); var dictionaryOptionsAttribute = attribute as BsonDictionaryOptionsAttribute; if (dictionaryOptionsAttribute != null) { _representation = dictionaryOptionsAttribute.Representation; return; } // for backward compatibility reasons representations Array and Document apply to the Dictionary and not the values var representationAttribute = attribute as BsonRepresentationAttribute; if (representationAttribute != null) { switch (representationAttribute.Representation) { case BsonType.Array: _representation = DictionaryRepresentation.ArrayOfArrays; return; case BsonType.Document: _representation = DictionaryRepresentation.Document; return; } } // any other attributes are applied to the values var valueType = typeof(object); if (serializer.GetType().IsGenericType) { valueType = serializer.GetType().GetGenericArguments()[1]; // TValue } var valueSerializer = BsonSerializer.LookupSerializer(valueType); var valueSerializationOptions = _keyValuePairSerializationOptions.ValueSerializationOptions; if (valueSerializationOptions == null) { var valueDefaultSerializationOptions = valueSerializer.GetDefaultSerializationOptions(); // special case for legacy dictionaries: allow BsonRepresentation on object if (valueDefaultSerializationOptions == null && serializer.GetType() == typeof(DictionarySerializer) && attribute.GetType() == typeof(BsonRepresentationAttribute)) { valueDefaultSerializationOptions = new RepresentationSerializationOptions(BsonType.Null); // will be modified later by ApplyAttribute } if (valueDefaultSerializationOptions == null) { var message = string.Format( "A serialization options attribute of type {0} cannot be used when the serializer is of type {1} and the value serializer is of type {2}.", BsonUtils.GetFriendlyTypeName(attribute.GetType()), BsonUtils.GetFriendlyTypeName(serializer.GetType()), BsonUtils.GetFriendlyTypeName(valueSerializer.GetType())); throw new NotSupportedException(message); } valueSerializationOptions = valueDefaultSerializationOptions.Clone(); } valueSerializationOptions.ApplyAttribute(valueSerializer, attribute); _keyValuePairSerializationOptions = new KeyValuePairSerializationOptions( _keyValuePairSerializationOptions.Representation, _keyValuePairSerializationOptions.KeySerializationOptions, valueSerializationOptions); }
/// <summary> /// Initializes a new instance of the BsonDictionaryOptionsAttribute class. /// </summary> /// <param name="representation">The representation to use for the Dictionary.</param> public BsonDictionaryOptionsAttribute(DictionaryRepresentation representation) { _representation = representation; }
public static void Test_Serialize_03(DictionaryRepresentation dictionaryRepresentation) { Trace.WriteLine("BsonPBSerializationProvider.RegisterProvider()"); BsonPBSerializationProvider.RegisterProvider(); PBDictionarySerializer.RegisterGenericDictionarySerializer(); try { PBDictionarySerializer.DefaultDictionaryRepresentation = dictionaryRepresentation; Test_Bson_Class_02 test = Create_Test_Bson_Class_02(); BsonDocument document = test.zToBsonDocument(); Trace.WriteLine(document.zToJson()); } finally { Trace.WriteLine("BsonPBSerializationProvider.UnregisterProvider()"); BsonPBSerializationProvider.UnregisterProvider(); PBDictionarySerializer.UnregisterGenericDictionarySerializer(); } }
public static void Test_Serialize_04(DictionaryRepresentation dictionaryRepresentation) { Trace.WriteLine("BsonPBSerializationProvider.RegisterProvider()"); BsonPBSerializationProvider.RegisterProvider(); PBDictionarySerializer.RegisterGenericDictionarySerializer(); try { //PBDictionarySerializer.DefaultDictionaryRepresentation = dictionaryRepresentation; DictionarySerializationOptions options = new DictionarySerializationOptions(dictionaryRepresentation); Dictionary<string, ZValue> infos2 = Create_Dictionary_string_ZValue_01(); //BsonDocument document = infos2.zToBsonDocument(options); //Trace.WriteLine(document.zToJson()); Trace.WriteLine("dictionaryRepresentation : {0}", dictionaryRepresentation); string json = infos2.ToJson(options); Trace.WriteLine(json); } finally { Trace.WriteLine("BsonPBSerializationProvider.UnregisterProvider()"); BsonPBSerializationProvider.UnregisterProvider(); PBDictionarySerializer.UnregisterGenericDictionarySerializer(); } }
// public methods /// <summary> /// Apply an attribute to these serialization options and modify the options accordingly. /// </summary> /// <param name="serializer">The serializer that these serialization options are for.</param> /// <param name="attribute">The serialization options attribute.</param> public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute) { EnsureNotFrozen(); var dictionaryOptionsAttribute = attribute as BsonDictionaryOptionsAttribute; if (dictionaryOptionsAttribute != null) { _representation = dictionaryOptionsAttribute.Representation; return; } // for backward compatibility reasons representations Array and Document apply to the Dictionary and not the values var representationAttribute = attribute as BsonRepresentationAttribute; if (representationAttribute != null) { switch (representationAttribute.Representation) { case BsonType.Array: _representation = DictionaryRepresentation.ArrayOfArrays; return; case BsonType.Document: _representation = DictionaryRepresentation.Document; return; } } // any other attributes are applied to the values var valueType = typeof(object); if (serializer.GetType().IsGenericType) { valueType = serializer.GetType().GetGenericArguments()[1]; // TValue } var valueSerializer = BsonSerializer.LookupSerializer(valueType); var valueSerializationOptions = _keyValuePairSerializationOptions.ValueSerializationOptions; if (valueSerializationOptions == null) { var valueDefaultSerializationOptions = valueSerializer.GetDefaultSerializationOptions(); // special case for legacy dictionaries: allow BsonRepresentation on object if (valueDefaultSerializationOptions == null && serializer.GetType() == typeof(DictionarySerializer) && attribute.GetType() == typeof(BsonRepresentationAttribute)) { valueDefaultSerializationOptions = new RepresentationSerializationOptions(BsonType.Null); // will be modified later by ApplyAttribute } if (valueDefaultSerializationOptions == null) { var message = string.Format( "A serialization options attribute of type {0} cannot be used when the serializer is of type {1} and the value serializer is of type {2}.", BsonUtils.GetFriendlyTypeName(attribute.GetType()), BsonUtils.GetFriendlyTypeName(serializer.GetType()), BsonUtils.GetFriendlyTypeName(valueSerializer.GetType())); throw new NotSupportedException(message); } valueSerializationOptions = valueDefaultSerializationOptions.Clone(); } valueSerializationOptions.ApplyAttribute(valueSerializer, attribute); _keyValuePairSerializationOptions = new KeyValuePairSerializationOptions( _keyValuePairSerializationOptions.Representation, _keyValuePairSerializationOptions.KeySerializationOptions, valueSerializationOptions); }
/// <summary> /// Initializes a new instance of the <see cref="ReadOnlyDictionaryInterfaceImplementerSerializer{TDictionary, TKey, TValue}"/> class. /// </summary> /// <param name="dictionaryRepresentation">The dictionary representation.</param> /// <param name="keySerializer">The key serializer.</param> /// <param name="valueSerializer">The value serializer.</param> public ReadOnlyDictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer <TKey> keySerializer, IBsonSerializer <TValue> valueSerializer) : base(dictionaryRepresentation, keySerializer, valueSerializer) { }
/// <summary> /// Initializes a new instance of the DictionarySerializationOptions class. /// </summary> /// <param name="representation">The representation to use for a Dictionary.</param> public DictionarySerializationOptions(DictionaryRepresentation representation) { _representation = representation; _keyValuePairSerializationOptions = (KeyValuePairSerializationOptions)KeyValuePairSerializationOptions.Defaults.Clone(); }
public DictionaryRepresentationConvention(DictionaryRepresentation dictionaryRepresentation) { _dictionaryRepresentation = dictionaryRepresentation; }