예제 #1
0
        public void DataArray()
        {
            // Verify that we can deserialize arrays of data items.

            var converter = new JsonDataConverter();
            var formatter = new ArrayFormatter();

            byte[]   contents;
            object[] items;

            // Empty array.

            contents = null;
            items    = converter.FromDataArray(contents, Array.Empty <Type>());

            Assert.Empty(items);

            // Single item.

            formatter.Clear();
            formatter.Append("\"foo\"");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(string) });

            Assert.Equal(new string[] { "foo" }, items);

            // Multiple object items

            formatter.Clear();
            formatter.Append("\"foo\"");
            formatter.Append("1234");
            formatter.Append("{Hello: \"World!\"}");
            formatter.Append("null");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(string), typeof(int), typeof(TestData), typeof(TestData) });

            Assert.Equal(4, items.Length);
            Assert.Equal("foo", items[0]);
            Assert.Equal(1234, items[1]);
            Assert.Equal("World!", ((TestData)items[2]).Hello);
            Assert.Null(items[3]);

            // Roundtrip objects

            var bob = new Person()
            {
                Name   = "Bob",
                Age    = 27,
                Data   = new byte[] { 0, 1, 2, 3, 4 },
                Gender = Gender.Male
            };

            bob.__O.Add("extra", "data");

            formatter.Clear();
            formatter.Clear();
            formatter.Append("\"foo\"");
            formatter.Append("1234");
            formatter.Append(bob.ToString(indented: false));
            formatter.Append("null");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(string), typeof(int), typeof(Person), typeof(Person) });

            Assert.Equal(4, items.Length);
            Assert.Equal("foo", items[0]);
            Assert.Equal(1234, items[1]);
            Assert.Equal(bob, (Person)items[2]);
            Assert.Equal("data", ((Person)items[2]).__O["extra"].ToString());
            Assert.Null(items[3]);

            // Arrays of other types.

            formatter.Clear();

            var guid = Guid.NewGuid();

            formatter.Append("10");
            formatter.Append("123.4");
            formatter.Append("\"Hello World!\"");
            formatter.Append("null");
            formatter.Append("\"female\"");
            formatter.Append("true");
            formatter.Append(NeonHelper.JsonSerialize(new DateTime(2019, 7, 17, 12, 0, 0)));
            formatter.Append(NeonHelper.JsonSerialize(TimeSpan.FromSeconds(1.5)));
            formatter.Append($"\"{guid}\"");

            contents = formatter.ToBytes();
            items    = converter.FromDataArray(contents, new Type[] { typeof(int), typeof(double), typeof(string), typeof(string), typeof(Gender), typeof(bool), typeof(DateTime), typeof(TimeSpan), typeof(Guid) });

            Assert.Equal(9, items.Length);
            Assert.Equal(10, (int)items[0]);
            Assert.Equal(123.4, (double)items[1]);
            Assert.Equal("Hello World!", (string)items[2]);
            Assert.Null((string)items[3]);
            Assert.Equal(Gender.Female, (Gender)items[4]);
            Assert.True((bool)items[5]);
            Assert.Equal(new DateTime(2019, 7, 17, 12, 0, 0), (DateTime)items[6]);
            Assert.Equal(TimeSpan.FromSeconds(1.5), (TimeSpan)items[7]);
            Assert.Equal(guid, (Guid)items[8]);
        }