예제 #1
0
            public object Deserialize(string serializedString, Type type)
            {
                if (serializedString == null)
                {
                    return(null);
                }

                var tokens = serializedString.Split(',');

                var value = int.Parse(tokens[0]);

                var value2 = int.Parse(tokens[1]);

                var result = new ModelThatSerializesToString(value, value2);

                return(result);
            }
예제 #2
0
        public static void RoundtripSerialize___Should_use_dictionary_key_and_value_serializers___When_serializers_are_registered()
        {
            // Arrange
            var modelThatSerializesToStringForKey   = new ModelThatSerializesToString(-892, 441);
            var modelThatSerializesToStringForValue = new ModelThatSerializesToString(228, -761);

            var expected1 = new DictionariesOfModelThatSerializesToStringModel
            {
                DictionaryInterface = new Dictionary <ModelThatSerializesToString, ModelThatSerializesToString> {
                    { modelThatSerializesToStringForKey, modelThatSerializesToStringForValue }
                },
                ReadOnlyDictionaryInterface = new ReadOnlyDictionary <ModelThatSerializesToString, ModelThatSerializesToString>(
                    new Dictionary <ModelThatSerializesToString, ModelThatSerializesToString> {
                    { modelThatSerializesToStringForKey, modelThatSerializesToStringForValue }
                }),
                Dictionary = new Dictionary <ModelThatSerializesToString, ModelThatSerializesToString> {
                    { modelThatSerializesToStringForKey, modelThatSerializesToStringForValue }
                },
                ReadOnlyDictionary = new ReadOnlyDictionary <ModelThatSerializesToString, ModelThatSerializesToString>(
                    new Dictionary <ModelThatSerializesToString, ModelThatSerializesToString> {
                    { modelThatSerializesToStringForKey, modelThatSerializesToStringForValue }
                }),
                ConcurrentDictionary = new ConcurrentDictionary <ModelThatSerializesToString, ModelThatSerializesToString>(
                    new Dictionary <ModelThatSerializesToString, ModelThatSerializesToString> {
                    { modelThatSerializesToStringForKey, modelThatSerializesToStringForValue }
                }),
            };

            void ThrowIfObjectsDiffer(DescribedSerializationBase describedSerialization, DictionariesOfModelThatSerializesToStringModel deserialized)
            {
                deserialized.AsTest().Must().BeEqualTo(expected1);

                if (describedSerialization is StringDescribedSerialization stringDescribedSerialization)
                {
                    stringDescribedSerialization.SerializedPayload.AsTest().Must().ContainString("-892,441");
                    stringDescribedSerialization.SerializedPayload.AsTest().Must().ContainString("228,-761");
                }
            }

            var expected2 = A.Dummy <DictionariesOfModelThatSerializesToStringModel>();

            // Act, Assert
            expected1.RoundtripSerializeViaBsonWithCallbackVerification(ThrowIfObjectsDiffer, typeof(DictionariesOfModelThatSerializesToStringBsonSerializationConfiguration));
            expected2.RoundtripSerializeViaBsonWithBeEqualToAssertion(typeof(DictionariesOfModelThatSerializesToStringBsonSerializationConfiguration));
        }
        public static void RoundtripSerialize___Should_use_element_serializer___When_serializers_are_registered()
        {
            // Arrange
            var modelThatSerializesToString = new ModelThatSerializesToString(-603, 329);

            var expected1 = new CollectionsOfModelThatSerializesToStringModel
            {
                CollectionInterface = new List <ModelThatSerializesToString> {
                    modelThatSerializesToString
                },
                ReadOnlyCollectionInterface = new HashSet <ModelThatSerializesToString> {
                    modelThatSerializesToString
                },
                ListInterface = new List <ModelThatSerializesToString> {
                    modelThatSerializesToString
                },
                ReadOnlyListInterface = new[] { modelThatSerializesToString },
                List = new List <ModelThatSerializesToString> {
                    modelThatSerializesToString
                },
                Collection = new Collection <ModelThatSerializesToString> {
                    modelThatSerializesToString
                },
                ReadOnlyCollection = new ReadOnlyCollection <ModelThatSerializesToString>(new List <ModelThatSerializesToString> {
                    modelThatSerializesToString
                }),
            };

            var expected2 = A.Dummy <CollectionsOfModelThatSerializesToStringModel>();

            void ThrowIfObjectsDiffer(DescribedSerializationBase describedSerialization, CollectionsOfModelThatSerializesToStringModel deserialized)
            {
                deserialized.AsTest().Must().BeEqualTo(expected1);

                if (describedSerialization is StringDescribedSerialization stringDescribedSerialization)
                {
                    stringDescribedSerialization.SerializedPayload.AsTest().Must().ContainString("-603,329");
                }
            }

            // Act, Assert
            expected1.RoundtripSerializeViaBsonWithCallbackVerification(ThrowIfObjectsDiffer, typeof(CollectionsOfModelThatSerializesToStringBsonSerializationConfiguration));
            expected2.RoundtripSerializeViaBsonWithBeEqualToAssertion(typeof(CollectionsOfModelThatSerializesToStringBsonSerializationConfiguration));
        }