Пример #1
0
        public static void SerializeTypeInstance()
        {
            Type type = typeof(int);

            NotSupportedException ex = Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(type));
            string exAsStr           = ex.ToString();

            Assert.Contains("System.Type", exAsStr);
            Assert.Contains("$", exAsStr);

            type = null;
            string serialized = JsonSerializer.Serialize(type);

            Assert.Equal("null", serialized);

            ClassWithType obj = new ClassWithType {
                Type = typeof(int)
            };

            ex      = Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(obj));
            exAsStr = ex.ToString();
            Assert.Contains("System.Type", exAsStr);
            Assert.Contains("$.Type", exAsStr);

            obj.Type   = null;
            serialized = JsonSerializer.Serialize(obj);
            Assert.Equal(@"{""Type"":null}", serialized);

            serialized = JsonSerializer.Serialize(obj, new JsonSerializerOptions {
                IgnoreNullValues = true
            });
            Assert.Equal(@"{}", serialized);
        }
Пример #2
0
        public static void DeserializeUnsupportedType()
        {
            // Any test payload is fine.
            string json = @"""Some string""";

            RunTest <Type>();
            RunTest <SerializationInfo>();

            void RunTest <T>()
            {
                string fullName = typeof(T).FullName;

                NotSupportedException ex = Assert.Throws <NotSupportedException>(() => JsonSerializer.Deserialize <T>(json));
                string exAsStr           = ex.ToString();

                Assert.Contains(fullName, exAsStr);
                Assert.Contains("$", exAsStr);

                json = $@"{{""Prop"":{json}}}";

                ex      = Assert.Throws <NotSupportedException>(() => JsonSerializer.Deserialize <ClassWithType <T> >(json));
                exAsStr = ex.ToString();
                Assert.Contains(fullName, exAsStr);
                Assert.Contains("$.Prop", exAsStr);

                // NSE is not thrown because the serializer handles null.
                Assert.Null(JsonSerializer.Deserialize <T>("null"));

                ClassWithType <T> obj = JsonSerializer.Deserialize <ClassWithType <T> >(@"{""Prop"":null}");

                Assert.Null(obj.Prop);
            }
        }
Пример #3
0
        public static void SerializeUnsupportedType()
        {
            RunTest(typeof(int));
            RunTest(new SerializationInfo(typeof(Type), new FormatterConverter()));
            RunTest((IntPtr)123);
            RunTest((UIntPtr)123);
#if NETCOREAPP
            RunTest(DateOnly.MaxValue);
            RunTest(TimeOnly.MinValue);
#endif

            void RunTest <T>(T value)
            {
                Type   type     = typeof(T);
                string fullName = type.FullName;

                NotSupportedException ex = Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(value));
                string exAsStr           = ex.ToString();

                Assert.Contains(fullName, exAsStr);
                Assert.Contains("$", exAsStr);

                ClassWithType <T> obj = new ClassWithType <T> {
                    Prop = value
                };

                ex      = Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(obj));
                exAsStr = ex.ToString();
                Assert.Contains(fullName, exAsStr);
                Assert.Contains("$.Prop", exAsStr);

                if (!type.IsValueType)
                {
                    string serialized = JsonSerializer.Serialize((T)(object)null);
                    Assert.Equal("null", serialized);

                    obj.Prop   = (T)(object)null;
                    serialized = JsonSerializer.Serialize(obj);
                    Assert.Equal(@"{""Prop"":null}", serialized);

                    serialized = JsonSerializer.Serialize(obj, new JsonSerializerOptions {
                        IgnoreNullValues = true
                    });
                    Assert.Equal(@"{}", serialized);
                }
            }
        }
Пример #4
0
        public static void SerializeUnsupportedType()
        {
            RunTest <Type>(typeof(int));
            RunTest <SerializationInfo>(new SerializationInfo(typeof(Type), new FormatterConverter()));

            void RunTest <T>(T value)
            {
                string fullName = typeof(T).FullName;

                NotSupportedException ex = Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(value));
                string exAsStr           = ex.ToString();

                Assert.Contains(fullName, exAsStr);
                Assert.Contains("$", exAsStr);

                string serialized = JsonSerializer.Serialize((T)(object)null);

                Assert.Equal("null", serialized);

                ClassWithType <T> obj = new ClassWithType <T> {
                    Prop = value
                };

                ex      = Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(obj));
                exAsStr = ex.ToString();
                Assert.Contains(fullName, exAsStr);
                Assert.Contains("$.Prop", exAsStr);

                obj.Prop   = (T)(object)null;
                serialized = JsonSerializer.Serialize(obj);
                Assert.Equal(@"{""Prop"":null}", serialized);

                serialized = JsonSerializer.Serialize(obj, new JsonSerializerOptions {
                    IgnoreNullValues = true
                });
                Assert.Equal(@"{}", serialized);
            }
        }
Пример #5
0
        public static void DeserializeTypeInstance()
        {
            string json = @"""System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e""";

            NotSupportedException ex = Assert.Throws <NotSupportedException>(() => JsonSerializer.Deserialize <Type>(json));
            string exAsStr           = ex.ToString();

            Assert.Contains("System.Type", exAsStr);
            Assert.Contains("$", exAsStr);

            json = $@"{{""Type"":{json}}}";

            ex      = Assert.Throws <NotSupportedException>(() => JsonSerializer.Deserialize <ClassWithType>(json));
            exAsStr = ex.ToString();
            Assert.Contains("System.Type", exAsStr);
            Assert.Contains("$.Type", exAsStr);

            // NSE is not thrown because the serializer handles null.
            Assert.Null(JsonSerializer.Deserialize <Type>("null"));

            ClassWithType obj = JsonSerializer.Deserialize <ClassWithType>(@"{""Type"":null}");

            Assert.Null(obj.Type);
        }