Exemple #1
0
        public static void NoSetter()
        {
            var obj = new ClassWithNoSetter();

            string json = JsonSerializer.Serialize(obj);

            Assert.Contains(@"""MyString"":""DefaultValue""", json);
            Assert.Contains(@"""MyInts"":[1,2]", json);

            obj = JsonSerializer.Deserialize <ClassWithNoSetter>(@"{""MyString"":""IgnoreMe"",""MyInts"":[0]}");
            Assert.Equal("DefaultValue", obj.MyString);
            Assert.Equal(2, obj.MyInts.Length);
        }
Exemple #2
0
        public static void IgnoreReadOnlyProperties()
        {
            var options = new JsonSerializerOptions();

            options.IgnoreReadOnlyProperties = true;

            var obj = new ClassWithNoSetter();

            string json = JsonSerializer.Serialize(obj, options);

            // Collections are always serialized unless they have [JsonIgnore].
            Assert.Equal(@"{""MyInts"":[1,2]}", json);
        }
Exemple #3
0
        public static void ClassWithNoSetterAndValidProperty(string json)
        {
            ClassWithNoSetter parsedObject = JsonSerializer.Deserialize <ClassWithNoSetter>(json);

            Assert.Null(parsedObject.SkippedChild);

            Assert.NotNull(parsedObject.ParsedChild);
            Assert.Equal(18, parsedObject.ParsedChild.MyInt16);

            Assert.Null(parsedObject.AnotherSkippedChild);

            Assert.NotNull(parsedObject.AnotherParsedChild);
            Assert.Equal(20, parsedObject.AnotherParsedChild.MyInt16);
        }
Exemple #4
0
        public static void ClassWithNoSetterAndValidProperty()
        {
            // Tests that the parser picks back up after skipping/draining ignored elements.
            string json = @"{
                ""SkippedChild"": {},
                ""ParsedChild"": {""MyInt16"":18},
                ""UnmatchedProp"": null,
                ""AnotherSkippedChild"": {""DrainProp1"":{}, ""DrainProp2"":{""SubProp"":0}},
                ""AnotherSkippedChild"": {},
                ""AnotherParsedChild"": {""MyInt16"":20}
            }";

            ClassWithNoSetter parsedObject = JsonSerializer.Deserialize <ClassWithNoSetter>(json);

            Assert.Null(parsedObject.SkippedChild);

            Assert.NotNull(parsedObject.ParsedChild);
            Assert.Equal(18, parsedObject.ParsedChild.MyInt16);

            Assert.Null(parsedObject.AnotherSkippedChild);

            Assert.NotNull(parsedObject.AnotherParsedChild);
            Assert.Equal(20, parsedObject.AnotherParsedChild.MyInt16);
        }