Пример #1
0
        public void ReadBytes_next_document_size_equal_zero()
        {
            JObjectValidation jObject = new JObjectValidation
            {
                BooleanField = true,
                ByteField    = 1,
                CharField    = 'a',
                DateField    = "2016-08-24T18:30:32.2387069+00:00",
                DoubleField  = 10.5,
                IntField     = 10,
                ListField    = new object[2] {
                    new { }, new { }
                },
                ObjectField = new { },
                StringField = "string",
                NullField   = null
            };

            byte[] jObjectBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jObject));
            byte[] length       = Encoding.UTF8.GetBytes(jObjectBytes.Length.ToString().PadLeft(8));

            Stream stream = new MemoryStream();

            stream.Write(length, 0, 8);
            stream.Write(jObjectBytes, 0, jObjectBytes.Length);
            stream.Position = stream.Length;

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                byte[] bytes = jsonStream.ReadBytes();
                Assert.IsNull(bytes);
            }
        }
Пример #2
0
        public void ReadJArray_pass()
        {
            JObjectValidation jObject = new JObjectValidation
            {
                BooleanField = true,
                ByteField    = 1,
                CharField    = 'a',
                DateField    = "2016-08-24T18:30:32.2387069+00:00",
                DoubleField  = 10.5,
                IntField     = 10,
                ListField    = new object[2] {
                    new { }, new { }
                },
                ObjectField = new { },
                StringField = "string",
                NullField   = null
            };

            JObjectValidation[] list = new JObjectValidation[1] {
                jObject
            };

            byte[] jObjectBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(list));
            byte[] length       = Encoding.UTF8.GetBytes(jObjectBytes.Length.ToString().PadLeft(8));

            Stream stream = new MemoryStream();

            stream.Write(length, 0, 8);
            stream.Write(jObjectBytes, 0, jObjectBytes.Length);
            stream.Position = 0;

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                JArray readArray = jsonStream.ReadJArray();
                Assert.AreEqual(1, readArray.Count);

                JObjectValidation readJObject = readArray.First.ToObject <JObjectValidation>();

                Assert.AreEqual(jObject.BooleanField, readJObject.BooleanField);
                Assert.AreEqual(jObject.ByteField, readJObject.ByteField);
                Assert.AreEqual(jObject.CharField, readJObject.CharField);
                Assert.AreEqual(jObject.DateField, readJObject.DateField);
                Assert.AreEqual(jObject.DoubleField, readJObject.DoubleField);
                Assert.AreEqual(jObject.IntField, readJObject.IntField);
                Assert.AreEqual(jObject.ListField.Length, readJObject.ListField.Length);
                Assert.AreEqual(jObject.StringField, readJObject.StringField);
                Assert.IsNotNull(readJObject.ObjectField);
                Assert.IsNull(readJObject.NullField);
            }
        }
Пример #3
0
        public void WriteBytes_when_validate_is_true_pass()
        {
            Stream stream = new MemoryStream();

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                JObjectValidation jObject = new JObjectValidation
                {
                    BooleanField = true,
                    ByteField    = 1,
                    CharField    = 'a',
                    DateField    = "2016-08-24T18:30:32.2387069+00:00",
                    DoubleField  = 10.5,
                    IntField     = 10,
                    ListField    = new object[2] {
                        new { }, new { }
                    },
                    ObjectField = new { },
                    StringField = "string",
                    NullField   = null
                };

                byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jObject));

                jsonStream.WriteBytes(bytes, true);
                stream.Position = 0;
                JObjectValidation readJObject = jsonStream.ReadObject <JObjectValidation>();
                Assert.AreEqual(jObject.BooleanField, readJObject.BooleanField);
                Assert.AreEqual(jObject.ByteField, readJObject.ByteField);
                Assert.AreEqual(jObject.CharField, readJObject.CharField);
                Assert.AreEqual(jObject.DateField, readJObject.DateField);
                Assert.AreEqual(jObject.DoubleField, readJObject.DoubleField);
                Assert.AreEqual(jObject.IntField, readJObject.IntField);
                Assert.AreEqual(jObject.ListField.Length, readJObject.ListField.Length);
                Assert.AreEqual(jObject.StringField, readJObject.StringField);
                Assert.IsNotNull(readJObject.ObjectField);
                Assert.IsNull(readJObject.NullField);
            }
        }
Пример #4
0
        public void ReadBytes_cant_read_entire_next_document()
        {
            JObjectValidation jObject = new JObjectValidation
            {
                BooleanField = true,
                ByteField    = 1,
                CharField    = 'a',
                DateField    = "2016-08-24T18:30:32.2387069+00:00",
                DoubleField  = 10.5,
                IntField     = 10,
                ListField    = new object[2] {
                    new { }, new { }
                },
                ObjectField = new { },
                StringField = "string",
                NullField   = null
            };

            byte[] jObjectBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jObject));
            byte[] length       = Encoding.UTF8.GetBytes((jObjectBytes.Length + 1).ToString().PadLeft(8));

            Stream stream = new MemoryStream();

            stream.Write(length, 0, 8);
            stream.Write(jObjectBytes, 0, jObjectBytes.Length);
            stream.Position = 0;

            InvalidJsonDocumentException exception = Assert.ThrowsException <InvalidJsonDocumentException>(() =>
            {
                using (IJsonStream jsonStream = new JsonStream(stream))
                {
                    byte[] bytes = jsonStream.ReadBytes();
                }
            });

            Assert.IsTrue(exception.Message.Contains("Cant't read all bytes of the json document at position"));
        }