Exemple #1
0
        public async void RequiredPropertyOccuringTwiceInThePayloadWorksAsExpected()
        {
            string json = "" "{" FirstName ":" foo "," MiddleName ":" "," LastName ":" bar "," FirstName ":" newfoo "}" "";
            PersonWithRequiredMembers deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembers>(json);

            Assert.Equal("newfoo", deserialized.FirstName);
            Assert.Equal("", deserialized.MiddleName);
            Assert.Equal("bar", deserialized.LastName);
        }
Exemple #2
0
        public async void RemovingPropertiesWithRequiredKeywordAllowsDeserialization()
        {
            JsonSerializerOptions options = new()
            {
                TypeInfoResolver = new DefaultJsonTypeInfoResolver()
                {
                    Modifiers =
                    {
                        (ti) =>
                        {
                            for (int i = 0; i < ti.Properties.Count; i++)
                            {
                                if (ti.Properties[i].Name == nameof(PersonWithRequiredMembers.FirstName))
                                {
                                    Assert.True(ti.Properties[i].IsRequired);
                                    JsonPropertyInfo property = ti.CreateJsonPropertyInfo(typeof(string), nameof(PersonWithRequiredMembers.FirstName));
                                    property.Get     = (obj) => ((PersonWithRequiredMembers)obj).FirstName;
                                    property.Set     = (obj, val) => ((PersonWithRequiredMembers)obj).FirstName = (string)val;
                                    ti.Properties[i] = property;
                                }
                                else if (ti.Properties[i].Name == nameof(PersonWithRequiredMembers.LastName))
                                {
                                    Assert.True(ti.Properties[i].IsRequired);
                                    JsonPropertyInfo property = ti.CreateJsonPropertyInfo(typeof(string), nameof(PersonWithRequiredMembers.LastName));
                                    property.Get     = (obj) => ((PersonWithRequiredMembers)obj).LastName;
                                    property.Set     = (obj, val) => ((PersonWithRequiredMembers)obj).LastName = (string)val;
                                    ti.Properties[i] = property;
                                }
                                else
                                {
                                    Assert.False(ti.Properties[i].IsRequired);
                                }
                            }
                        }
                    }
                }
            };

            var obj = new PersonWithRequiredMembers()
            {
                FirstName = "foo",
                LastName  = "bar"
            };

            string json = await Serializer.SerializeWrapper(obj, options);

            Assert.Equal("" "{" FirstName ":" foo "," MiddleName ":" "," LastName ":" bar "}" "", json);

            json = "" "{" LastName ":" bar "}" "";
            PersonWithRequiredMembers deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembers>(json, options);

            Assert.Null(deserialized.FirstName);
            Assert.Equal("", deserialized.MiddleName);
            Assert.Equal("bar", deserialized.LastName);
        }
Exemple #3
0
        public async void ClassWithRequiredKeywordDeserialization(bool ignoreNullValues)
        {
            JsonSerializerOptions options = new()
            {
                IgnoreNullValues = ignoreNullValues
            };

            AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo <PersonWithRequiredMembers>(options),
                                                    nameof(PersonWithRequiredMembers.FirstName),
                                                    nameof(PersonWithRequiredMembers.LastName));

            var obj = new PersonWithRequiredMembers()
            {
                FirstName = "foo",
                LastName  = "bar"
            };

            string json = await Serializer.SerializeWrapper(obj, options);

            Assert.Equal("" "{" FirstName ":" foo "," MiddleName ":" "," LastName ":" bar "}" "", json);

            PersonWithRequiredMembers deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembers>(json, options);

            Assert.Equal(obj.FirstName, deserialized.FirstName);
            Assert.Equal(obj.MiddleName, deserialized.MiddleName);
            Assert.Equal(obj.LastName, deserialized.LastName);

            json = "" "{" LastName ":" bar "}" "";
            JsonException exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembers>(json, options));

            Assert.Contains("FirstName", exception.Message);
            Assert.DoesNotContain("LastName", exception.Message);
            Assert.DoesNotContain("MiddleName", exception.Message);

            json      = "" "{" LastName ":null}" "";
            exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembers>(json, options));

            Assert.Contains("FirstName", exception.Message);
            Assert.DoesNotContain("LastName", exception.Message);
            Assert.DoesNotContain("MiddleName", exception.Message);

            json      = "{}";
            exception = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <PersonWithRequiredMembers>(json, options));

            Assert.Contains("FirstName", exception.Message);
            Assert.Contains("LastName", exception.Message);
            Assert.DoesNotContain("MiddleName", exception.Message);
        }
Exemple #4
0
        public async void ChangingPropertiesWithRequiredKeywordToNotBeRequiredAllowsDeserialization()
        {
            JsonSerializerOptions options = new()
            {
                TypeInfoResolver = new DefaultJsonTypeInfoResolver()
                {
                    Modifiers =
                    {
                        (ti) =>
                        {
                            for (int i = 0; i < ti.Properties.Count; i++)
                            {
                                ti.Properties[i].IsRequired = false;
                            }
                        }
                    }
                }
            };

            var obj = new PersonWithRequiredMembers()
            {
                FirstName = "foo",
                LastName  = "bar"
            };

            string json = await Serializer.SerializeWrapper(obj, options);

            Assert.Equal("" "{" FirstName ":" foo "," MiddleName ":" "," LastName ":" bar "}" "", json);

            json = "" "{" LastName ":" bar "}" "";
            PersonWithRequiredMembers deserialized = await Serializer.DeserializeWrapper <PersonWithRequiredMembers>(json, options);

            Assert.Null(deserialized.FirstName);
            Assert.Equal("", deserialized.MiddleName);
            Assert.Equal("bar", deserialized.LastName);
        }