public void Deserialization_Throws_TypeLoadException_When_UnknownType()
        {
            var foo      = new FooCommand <BarEntity>();
            var envelope = new Envelope <FooCommand <BarEntity> >(foo);

            var resultJson = JsonSerializer.Serialize <Envelope>(envelope);

            resultJson = resultJson.Replace("JsonEnvelopes.Tests.FooCommand", "NewNamespace.FooCommand");

            Assert.Throws <TypeLoadException>(() =>
            {
                var resultFoo = JsonSerializer.Deserialize <Envelope>(resultJson)
                                .GetContent() as FooCommand <BarEntity>;
            });
        }
示例#2
0
        public void WrapContent_Test()
        {
            var foo = new FooCommand <BarEntity>(new BarEntity("bar1"), "foo1");

            var envelope = Envelope.WrapContent(foo);
            var content  = envelope.GetContent();

            Assert.Equal(foo.GetType(), content.GetType());
            var fooContent = content as FooCommand <BarEntity>;

            if (fooContent == null)
            {
                Assert.NotNull(fooContent);
                return;
            }

            Assert.Equal(foo.Id, fooContent.Id);
            Assert.Equal(foo.Bar !.Id, fooContent.Bar !.Id);
        }
        public void Serialization_Then_Deserialization_Produces_Equal_Object(bool useCamelCase, bool caseInsensitive)
        {
            var jsonOptions = new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = caseInsensitive
            };

            if (useCamelCase)
            {
                jsonOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
            }
            var foo      = new FooCommand <BarEntity>(new BarEntity("pdq"), "abc");
            var envelope = new Envelope <FooCommand <BarEntity> >(foo);

            var resultJson = JsonSerializer.Serialize <Envelope>(envelope, jsonOptions);
            var resultFoo  = JsonSerializer.Deserialize <Envelope>(resultJson, jsonOptions)
                             .GetContent() as FooCommand <BarEntity>;

            Assert.NotNull(resultFoo);
            Assert.NotNull(foo.Bar !.Id);
            Assert.Equal(foo.Id, resultFoo !.Id);
            Assert.Equal(foo.Bar.Id, resultFoo.Bar?.Id);
        }