public async Task CanCreateBinaryDataFromCustomType()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };
            var serializer = new JsonObjectSerializer();

            await AssertData(BinaryData.FromObject(payload, serializer));
            await AssertData(await BinaryData.FromObjectAsync(payload, serializer));

            await AssertData(BinaryData.FromObject(payload));
            await AssertData(await BinaryData.FromObjectAsync(payload));

            await AssertData(new BinaryData(payload, type : typeof(TestModel)));
            await AssertData(new BinaryData(payload, null, typeof(TestModel)));
            await AssertData(new BinaryData(payload, serializer, typeof(TestModel)));

            async Task AssertData(BinaryData data)
            {
                Assert.AreEqual(payload.A, data.ToObject <TestModel>(serializer).A);
                Assert.AreEqual(payload.B, data.ToObject <TestModel>(serializer).B);
                Assert.AreEqual(payload.C, data.ToObject <TestModel>(serializer).C);
                Assert.AreEqual(payload.A, (await data.ToObjectAsync <TestModel>(serializer)).A);
                Assert.AreEqual(payload.B, (await data.ToObjectAsync <TestModel>(serializer)).B);
                Assert.AreEqual(payload.C, (await data.ToObjectAsync <TestModel>(serializer)).C);

                Assert.AreEqual(payload.A, data.ToObject <TestModel>().A);
                Assert.AreEqual(payload.B, data.ToObject <TestModel>().B);
                Assert.AreEqual(payload.C, data.ToObject <TestModel>().C);
                Assert.AreEqual(payload.A, (await data.ToObjectAsync <TestModel>()).A);
                Assert.AreEqual(payload.B, (await data.ToObjectAsync <TestModel>()).B);
                Assert.AreEqual(payload.C, (await data.ToObjectAsync <TestModel>()).C);
            }
        }
        public void FromObjectThrowsOnNullSerializer()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };

            Assert.That(
                () => BinaryData.FromObject(payload, null),
                Throws.InstanceOf <ArgumentNullException>());
            Assert.That(
                async() => await BinaryData.FromObjectAsync(payload, null),
                Throws.InstanceOf <ArgumentNullException>());
        }
        public async Task ToObjectThrowsExceptionOnIncompatibleType()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };

            AssertData(BinaryData.FromObject(payload));
            AssertData(await BinaryData.FromObjectAsync(payload));

            void AssertData(BinaryData data)
            {
                Assert.That(
                    () => data.ToObject <string>(),
                    Throws.InstanceOf <Exception>());
                Assert.That(
                    async() => await data.ToObjectAsync <string>(),
                    Throws.InstanceOf <Exception>());
            }
        }