public void ReadAsBytes_MissingComma()
        {
            byte[] data = Encoding.UTF8.GetBytes("Hello world");

            string         json   = @"['" + Convert.ToBase64String(data) + "' '" + Convert.ToBase64String(data) + @"']";
            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            Assert.IsTrue(reader.Read());
            CollectionAssert.AreEquivalent(data, reader.ReadAsBytes());

            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.ReadAsBytes(),
                "After parsing a value an unexpected character was encountered: '. Path '[0]', line 1, position 20.");
        }
        public void ReadAsBytes_CommaErrors()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader("[,'']"));

            reader.Read();

            ExceptionAssert.Throws <JsonReaderException>(() =>
            {
                reader.ReadAsBytes();
            }, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 2.");

            CollectionAssert.AreEquivalent(new byte[0], reader.ReadAsBytes());
            Assert.IsTrue(reader.Read());
        }
        public void ReadBytesWithError()
        {
            string json = @"{
    ChildId: '123'
}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType);

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType);

            try
            {
                jsonTextReader.ReadAsBytes();
            }
            catch (FormatException)
            {
            }

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType);

            Assert.IsFalse(jsonTextReader.Read());
        }
Ejemplo n.º 4
0
        public void ReadBytesNoStartWithUnexpectedEnd()
        {
            JsonReader reader = new JsonTextReader(new StringReader(@"[  "));

            Assert.IsTrue(reader.Read());
            reader.ReadAsBytes();
        }
        public void ReadAsBytes_BadData()
        {
            string json = @"pie";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            ExceptionAssert.Throws <JsonReaderException>(() => { reader.ReadAsBytes(); }, "Unexpected character encountered while parsing value: p. Path '', line 1, position 1.");
        }
        public void ReadAsBytes_InvalidEndArray()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader("]"));

            ExceptionAssert.Throws <JsonReaderException>(() =>
            {
                reader.ReadAsBytes();
            }, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1.");
        }
Ejemplo n.º 7
0
        public void ReadBytesWithUnexpectedEnd()
        {
            string helloWorld = "Hello world!";

            byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);

            JsonReader reader = new JsonTextReader(new StringReader(@"'" + Convert.ToBase64String(helloWorldData)));

            reader.ReadAsBytes();
        }
        public void ReadBytesWithUnexpectedEnd()
        {
            string helloWorld = "Hello world!";

            byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);

            JsonReader reader = new JsonTextReader(new StringReader(@"'" + Convert.ToBase64String(helloWorldData)));

            ExceptionAssert.Throws <JsonReaderException>(() => { reader.ReadAsBytes(); }, "Unterminated string. Expected delimiter: '. Path '', line 1, position 17.");
        }
Ejemplo n.º 9
0
        public void ReadAsBytesIntegerArrayWithNoEnd()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(@"[1"));

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsBytes();
            },
                "Unexpected end when reading bytes. Path '[0]', line 1, position 2."
                );
        }
Ejemplo n.º 10
0
        public void ReadBytesWithBadCharacter()
        {
            JsonReader reader = new JsonTextReader(new StringReader(@"true"));

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsBytes();
            },
                "Unexpected character encountered while parsing value: t. Path '', line 1, position 1."
                );
        }
Ejemplo n.º 11
0
        public void ReadAsBytesBadContent()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsBytes();
            },
                "Unexpected character encountered while parsing value: e. Path '', line 1, position 2."
                );
        }
Ejemplo n.º 12
0
        public void ReadBytesEmptyWrappedObject()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(@"{}"));

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsBytes();
            },
                "Error reading bytes. Unexpected token: StartObject. Path '', line 1, position 2."
                );
        }
Ejemplo n.º 13
0
        public void ReadAsBytesNoContentWrappedObject()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(@"{"));

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsBytes();
            },
                "Unexpected end when reading JSON. Path '', line 1, position 1."
                );
        }
Ejemplo n.º 14
0
        public void ReadSingleBytes()
        {
            StringReader   s      = new StringReader(@"""SGVsbG8gd29ybGQu""");
            JsonTextReader reader = new JsonTextReader(s);

            byte[] data = reader.ReadAsBytes();
            Assert.IsNotNull(data);

            string text = Encoding.UTF8.GetString(data, 0, data.Length);

            Assert.AreEqual("Hello world.", text);
        }
Ejemplo n.º 15
0
        public void ReadAsBytesArrayWithBadContent()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(@"[1.0]"));

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsBytes();
            },
                "Unexpected token when reading bytes: Float. Path '[0]', line 1, position 4."
                );
        }
Ejemplo n.º 16
0
        public void ReadBytesFollowingNumberInArray()
        {
            string helloWorld = "Hello world!";

            byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);

            JsonReader reader = new JsonTextReader(new StringReader(@"[1,'" + Convert.ToBase64String(helloWorldData) + @"']"));

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            byte[] data = reader.ReadAsBytes();
            Assert.AreEqual(helloWorldData, data);
            Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.EndArray, reader.TokenType);

            Assert.IsFalse(reader.Read());
        }
Ejemplo n.º 17
0
 /// <summary>
 ///		Reads the next JSON token from the stream as a <see cref="byte" /><c>[]</c>.
 /// </summary>
 /// <returns>
 ///		A <see cref="byte" /><c>[]</c> or a <see langword="null"/> reference if the next JSON token is <see langword="null"/>. This method will return <see langword="null"/> at the end of an array.
 /// </returns>
 public override byte[] ReadAsBytes() => jsonTextReader.ReadAsBytes();
Ejemplo n.º 18
0
        public void ReadBytesAsStreamShouldPreserveJsonTextReaderInternalState(int size)
        {
            var random = new Random(size);

            var buffer = new byte[size];
            var items  = new Data[3];

            for (int i = 0; i < 3; i++)
            {
                random.NextBytes(buffer);
                items[i] = new Data
                {
                    Foo = buffer.ToArray()
                };
            }

            var jsonObj = RavenJToken.FromObject(items);

            var jsonObjAsString = jsonObj.ToString();

            using (var textReader = new StringReader(jsonObjAsString))
                using (var jsonReader = new JsonTextReader(textReader))
                {
                    jsonReader.Read(); //start array

                    jsonReader.Read();
                    jsonReader.Read(); // property name
                    var actual = new byte[buffer.Length];
                    buffer = items[0].Foo;
                    using (var objectStream = jsonReader.ReadBytesAsStream())
                    {
                        objectStream.Read(actual, 0, actual.Length);

                        for (int a = 0; a < actual.Length; a++)
                        {
                            if (buffer[a] != actual[a])
                            {
                                Assert.True(false, "not equal on byte " + a);
                            }
                        }
                    }

                    jsonReader.Read();                 //end object

                    jsonReader.Read();                 //start object
                    jsonReader.Read();                 // property name
                    actual = jsonReader.ReadAsBytes(); //	value;
                    buffer = items[1].Foo;
                    for (int a = 0; a < actual.Length; a++)
                    {
                        if (buffer[a] != actual[a])
                        {
                            Assert.True(false, "not equal on byte " + a);
                        }
                    }

                    jsonReader.Read(); //end object


                    jsonReader.Read(); //start object
                    jsonReader.Read(); // property name
                    actual = new byte[buffer.Length];
                    buffer = items[2].Foo;
                    using (var objectStream = jsonReader.ReadBytesAsStream())
                    {
                        objectStream.Read(actual, 0, actual.Length);

                        for (int a = 0; a < actual.Length; a++)
                        {
                            if (buffer[a] != actual[a])
                            {
                                Assert.True(false, "not equal on byte " + a);
                            }
                        }
                    }

                    jsonReader.Read(); //end object
                }
        }
Ejemplo n.º 19
0
        public void ReadBytesWithBadCharacter()
        {
            JsonReader reader = new JsonTextReader(new StringReader(@"true"));

            reader.ReadAsBytes();
        }
Ejemplo n.º 20
0
        public IList <SampleData> GetData()
        {
            List <SampleData> data    = new List <SampleData>();
            StreamReader      sr      = new StreamReader("/sampleData.json");
            string            strData = string.Empty;

            using (StreamReader s = new StreamReader("/sampleData.json"))
            {
                strData = s.ReadToEnd();
            }
            JsonTextReader js  = new JsonTextReader(sr);
            JsonSerializer ser = new JsonSerializer();

            //   ser.Deserialize(js,typeof(string));
            ser.Deserialize(js);
            //  JObject jsondata = new JObject(strData);



            while (js.Read())
            {
                var s = js.ReadAsBytes();
            }

            data.Add(new SampleData
            {
                Id            = new Guid("3f2b12b8-2a06-45b4-b057-45949279b4e5"),
                ApplicationId = 197104,
                Type          = "Debit",
                Summary       = "Payment",
                Amount        = 58.26,
                PostingDate   = Convert.ToDateTime("07/01/2016", new CultureInfo("en-US")),
                IsCleared     = true,
                ClearedDate   = Convert.ToDateTime("07/02/2016", new CultureInfo("en-US"))
            });

            data.Add(new SampleData
            {
                Id            = new Guid("d2032222-47a6-4048-9894-11ab8ebb9f69"),
                ApplicationId = 197104,
                Type          = "Debit",
                Summary       = "Payment",
                Amount        = 50.09,
                PostingDate   = Convert.ToDateTime("07/01/2016", new CultureInfo("en-US")),
                IsCleared     = true,
                ClearedDate   = Convert.ToDateTime("07/02/2016", new CultureInfo("en-US"))
            });
            data.Add(new SampleData
            {
                Id            = new Guid("d2032222-47a6-4048-9894-11ab8ebb9f69"),
                ApplicationId = 197104,
                Type          = "Debit",
                Summary       = "Payment",
                Amount        = 67.09,
                PostingDate   = Convert.ToDateTime("07/01/2016", new CultureInfo("en-US")),
                IsCleared     = true,
                ClearedDate   = Convert.ToDateTime("07/02/2016", new CultureInfo("en-US"))
            });

            return(data);
        }