Beispiel #1
0
        static void Main()
        {
            var user = new User();
            user.Id = 1;
            user.Email = "*****@*****.**";
            user.Image = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            user.CreatedAt = DateTime.Now;
            user.Tags = new List<string>();
            user.Tags.Add("soccer");

            var serializer = new Serializer();

            var content = new PushStreamContent((stream, httpContent, transportContext) =>
            {
                serializer.SerializeToStream(user, stream);
                stream.Close();
            });

            var client = new HttpClient();

            var response = client.PostAsync("http://localhost:19012/api/values/myItem?replace=false", content).ConfigureAwait(false).GetAwaiter().GetResult();
            response.EnsureSuccessStatusCode();

            var nextResponse = client.GetAsync("http://localhost:19012/api/values/myItem").ConfigureAwait(false).GetAwaiter().GetResult();

            var responseStream = nextResponse.Content.ReadAsStreamAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            var otherUser = serializer.DeserializeFromStream<User>(responseStream);
        }
        public void Serializes_and_deserializes_without_attributes()
        {
            var serializer = new Serializer();
            var user = new User();
            user.Id = 1;
            user.Email = "*****@*****.**";
            user.Image = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            user.CreatedAt = DateTime.Now;
            user.Tags = new List<string>();
            user.Tags.Add("soccer");

            var serialized = serializer.SerializeToStream(user);
            serialized.Position = 0;
            var deserialized = serializer.DeserializeFromStream<User>(serialized);
            Assert.AreEqual(user.Id, deserialized.Id);
            Assert.AreEqual(user.Email, deserialized.Email);
            Assert.AreEqual(user.Image, deserialized.Image);
            Assert.AreEqual(user.CreatedAt, deserialized.CreatedAt);
            Assert.AreEqual(user.Tags, deserialized.Tags);
        }