Example #1
0
        public static void RoundtripSerialize___Should_roundtrip_TestModelWithOptionalConstructorParameters_and_include_all_properties_in_payload___When_model_constructed_with_all_default_T_values_of_nullable_underlying_type()
        {
            // Arrange
            string   stringValue1       = null;
            string   stringValue2       = null;
            string   stringValue3       = null;
            var      intValue1          = 0;
            var      intValue2          = 0;
            var      nullableIntValue1  = 0;
            var      nullableIntValue2  = 0;
            var      nullableIntValue3  = 0;
            var      enumValue1         = TestEnum.First;
            var      enumValue2         = TestEnum.First;
            TestEnum?nullableEnumValue1 = TestEnum.First;
            TestEnum?nullableEnumValue2 = TestEnum.First;
            TestEnum?nullableEnumValue3 = TestEnum.First;
            Dictionary <string, string> dictionaryValue = null;

            var expected = new TestModelWithOptionalConstructorParameters(stringValue1, stringValue2, stringValue3, intValue1, intValue2, nullableIntValue1, nullableIntValue2, nullableIntValue3, enumValue1, enumValue2, nullableEnumValue1, nullableEnumValue2, nullableEnumValue3, dictionaryValue);

            void VerificationCallback(DescribedSerializationBase describedSerialization, TestModelWithOptionalConstructorParameters deserialized)
            {
                deserialized.AsTest().Must().BeEqualTo(expected);

                if (describedSerialization is StringDescribedSerialization stringDescribedSerialization)
                {
                    stringDescribedSerialization.SerializedPayload.AsTest().Must().BeEqualTo("{\r\n  \"stringValue1\": null,\r\n  \"stringValue2\": null,\r\n  \"stringValue3\": null,\r\n  \"intValue1\": 0,\r\n  \"intValue2\": 0,\r\n  \"nullableIntValue1\": 0,\r\n  \"nullableIntValue2\": 0,\r\n  \"nullableIntValue3\": 0,\r\n  \"enumValue1\": \"first\",\r\n  \"enumValue2\": \"first\",\r\n  \"nullableEnumValue1\": \"first\",\r\n  \"nullableEnumValue2\": \"first\",\r\n  \"nullableEnumValue3\": \"first\",\r\n  \"dictionaryValue\": null\r\n}");
                }
            }

            // Act, Assert
            expected.RoundtripSerializeViaJsonWithCallbackVerification(VerificationCallback, typeof(TypesToRegisterJsonSerializationConfiguration <TestModelWithOptionalConstructorParameters>));
        }
Example #2
0
        public static void Deserialize___Should_roundtrip_serialized_TestModelWithOptionalConstructorParameters___When_payload_is_missing_properties_for_constructor_parameters_with_default_values()
        {
            // Arrange
            var payload = "{  \"stringValue1\": \"my-string-value\" }";

            var serializer = new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <TestModelWithOptionalConstructorParameters>).ToJsonSerializationConfigurationType());

            var expected = new TestModelWithOptionalConstructorParameters("my-string-value");

            // Act
            var actual = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload);

            // Assert
            actual.AsTest().Must().BeEqualTo(expected);
        }
Example #3
0
        public static void Deserialize___Should_roundtrip_serialized_TestModelWithOptionalConstructorParameters___When_payload_is_missing_properties_for_constructor_parameters_with_default_values_for_parameters_not_assignable_to_null_and_parameters_assignable_to_null_that_are_defaulted_to_null()
        {
            // Arrange
            var payload1 = "{\r\n    \"stringValue1\": \"my-string-value-1\",\r\n    \"stringValue3\": \"my-string-value-3\",\r\n    \"nullableIntValue2\": 28,\r\n    \"nullableIntValue3\": 32,\r\n    \"nullableEnumValue2\": \"third\",\r\n    \"nullableEnumValue3\": \"fourth\"\r\n}";
            var payload2 = "{\r\n    \"stringValue1\": \"my-string-value-1\",\r\n    \"stringValue3\": null,\r\n    \"nullableIntValue2\": null,\r\n    \"nullableIntValue3\": null,\r\n    \"nullableEnumValue2\": null,\r\n    \"nullableEnumValue3\": null\r\n}";
            var payload3 = "{\r\n    \"stringValue1\": \"my-string-value-1\",\r\n    \"stringValue3\": null,\r\n    \"nullableIntValue2\": 0,\r\n    \"nullableIntValue3\": 0,\r\n    \"nullableEnumValue2\": \"first\",\r\n    \"nullableEnumValue3\": \"first\"\r\n}";

            var serializer = new ObcJsonSerializer(typeof(TypesToRegisterJsonSerializationConfiguration <TestModelWithOptionalConstructorParameters>).ToJsonSerializationConfigurationType());

            var expected1 = new TestModelWithOptionalConstructorParameters("my-string-value-1", stringValue3: "my-string-value-3", nullableIntValue2: 28, nullableIntValue3: 32, nullableEnumValue2: TestEnum.Third, nullableEnumValue3: TestEnum.Fourth);
            var expected2 = new TestModelWithOptionalConstructorParameters("my-string-value-1", stringValue3: null, nullableIntValue2: null, nullableIntValue3: null, nullableEnumValue2: null, nullableEnumValue3: null);
            var expected3 = new TestModelWithOptionalConstructorParameters("my-string-value-1", stringValue3: null, nullableIntValue2: 0, nullableIntValue3: 0, nullableEnumValue2: TestEnum.First, nullableEnumValue3: TestEnum.First);

            // Act
            var actual1 = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload1);
            var actual2 = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload2);
            var actual3 = serializer.Deserialize <TestModelWithOptionalConstructorParameters>(payload3);

            // Assert
            actual1.AsTest().Must().BeEqualTo(expected1);
            actual2.AsTest().Must().BeEqualTo(expected2);
            actual3.AsTest().Must().BeEqualTo(expected3);
        }
Example #4
0
        public static void RoundtripSerialize___Should_roundtrip_TestModelWithOptionalConstructorParameters_and_include_all_properties_in_payload___When_model_constructed_with_all_non_default_values()
        {
            // Arrange
            var stringValue1       = "my-string-value-1";
            var stringValue2       = "my-string-value-2";
            var stringValue3       = "my-string-value-3";
            var intValue1          = -39;
            var intValue2          = 4;
            var nullableIntValue1  = 22;
            var nullableIntValue2  = 28;
            var nullableIntValue3  = 32;
            var enumValue1         = TestEnum.Second;
            var enumValue2         = TestEnum.Third;
            var nullableEnumValue1 = TestEnum.Second;
            var nullableEnumValue2 = TestEnum.Third;
            var nullableEnumValue3 = TestEnum.Fourth;
            var dictionaryValue    = new Dictionary <string, string>
            {
                { "abc", "def" },
                { "ghi", "jkl" },
            };

            var expected = new TestModelWithOptionalConstructorParameters(stringValue1, stringValue2, stringValue3, intValue1, intValue2, nullableIntValue1, nullableIntValue2, nullableIntValue3, enumValue1, enumValue2, nullableEnumValue1, nullableEnumValue2, nullableEnumValue3, dictionaryValue);

            void VerificationCallback(DescribedSerializationBase describedSerialization, TestModelWithOptionalConstructorParameters deserialized)
            {
                deserialized.AsTest().Must().BeEqualTo(expected);

                if (describedSerialization is StringDescribedSerialization stringDescribedSerialization)
                {
                    stringDescribedSerialization.SerializedPayload.AsTest().Must().BeEqualTo("{\r\n  \"stringValue1\": \"my-string-value-1\",\r\n  \"stringValue2\": \"my-string-value-2\",\r\n  \"stringValue3\": \"my-string-value-3\",\r\n  \"intValue1\": -39,\r\n  \"intValue2\": 4,\r\n  \"nullableIntValue1\": 22,\r\n  \"nullableIntValue2\": 28,\r\n  \"nullableIntValue3\": 32,\r\n  \"enumValue1\": \"second\",\r\n  \"enumValue2\": \"third\",\r\n  \"nullableEnumValue1\": \"second\",\r\n  \"nullableEnumValue2\": \"third\",\r\n  \"nullableEnumValue3\": \"fourth\",\r\n  \"dictionaryValue\": {\r\n    \"abc\": \"def\",\r\n    \"ghi\": \"jkl\"\r\n  }\r\n}");
                }
            }

            // Act, Assert
            expected.RoundtripSerializeViaJsonWithCallbackVerification(VerificationCallback, typeof(TypesToRegisterJsonSerializationConfiguration <TestModelWithOptionalConstructorParameters>));
        }