Beispiel #1
0
        public static void JsonIgnoreAttribute_UnsupportedBigInteger()
        {
            string json        = @"{""MyBigInteger"":1}";
            string wrapperJson = @"{""MyClass"":{""MyBigInteger"":1}}";

            // Unsupported types will throw by default.
            Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <ClassWithUnsupportedBigInteger>(json));
            // Using new options instance to prevent using previously cached metadata.
            JsonSerializerOptions options = new JsonSerializerOptions();

            Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <WrapperForClassWithUnsupportedBigInteger>(wrapperJson, options));

            // When ignored, we can serialize and deserialize without exceptions.
            options = new JsonSerializerOptions();
            ClassWithIgnoredUnsupportedBigInteger obj = JsonSerializer.Deserialize <ClassWithIgnoredUnsupportedBigInteger>(json, options);

            Assert.Null(obj.MyBigInteger);

            options = new JsonSerializerOptions();
            Assert.Equal("{}", JsonSerializer.Serialize(new ClassWithIgnoredUnsupportedBigInteger()));

            options = new JsonSerializerOptions();
            WrapperForClassWithIgnoredUnsupportedBigInteger wrapperObj = JsonSerializer.Deserialize <WrapperForClassWithIgnoredUnsupportedBigInteger>(wrapperJson, options);

            Assert.Null(wrapperObj.MyClass.MyBigInteger);

            options = new JsonSerializerOptions();
            Assert.Equal(@"{""MyClass"":{}}", JsonSerializer.Serialize(new WrapperForClassWithIgnoredUnsupportedBigInteger()
            {
                MyClass = new ClassWithIgnoredUnsupportedBigInteger(),
            }, options));
        }
Beispiel #2
0
        public static void JsonIgnoreAttribute_UnsupportedBigInteger()
        {
            string json        = @"{""MyBigInteger"":1}";
            string wrapperJson = @"{""MyClass"":{""MyBigInteger"":1}}";

            // Unsupported types will throw by default.
            Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <ClassWithUnsupportedBigInteger>(json));
            Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <WrapperForClassWithUnsupportedBigInteger>(wrapperJson));

            // When ignored, we can serialize and deserialize without exceptions.
            ClassWithIgnoredUnsupportedBigInteger obj = JsonSerializer.Deserialize <ClassWithIgnoredUnsupportedBigInteger>(json);

            Assert.Null(obj.MyBigInteger);
            Assert.Equal("{}", JsonSerializer.Serialize(new ClassWithIgnoredUnsupportedBigInteger()));

            WrapperForClassWithIgnoredUnsupportedBigInteger wrapperObj = JsonSerializer.Deserialize <WrapperForClassWithIgnoredUnsupportedBigInteger>(wrapperJson);

            Assert.Null(wrapperObj.MyClass.MyBigInteger);
            Assert.Equal(@"{""MyClass"":{}}", JsonSerializer.Serialize(new WrapperForClassWithIgnoredUnsupportedBigInteger()
            {
                MyClass = new ClassWithIgnoredUnsupportedBigInteger(),
            }));;
        }