Exemplo n.º 1
0
        public void ReadBytesFailure()
        {
            AssertException.Throws<JsonReaderException>(() =>
            {
                JObject o =
                    new JObject(
                        new JProperty("Test1", 1)
                        );

                using (JTokenReader jsonReader = new JTokenReader(o))
                {
                    jsonReader.Read();
                    Assert.Equal(JsonToken.StartObject, jsonReader.TokenType);

                    jsonReader.Read();
                    Assert.Equal(JsonToken.PropertyName, jsonReader.TokenType);
                    Assert.Equal("Test1", jsonReader.Value);

                    jsonReader.ReadAsBytes();
                }
            }, "Error reading bytes. Unexpected token: Integer. Path 'Test1'.");
        }
Exemplo n.º 2
0
        public void ReadBytes()
        {
            byte[] data = Encoding.UTF8.GetBytes("Hello world!");

            JObject o =
                new JObject(
                    new JProperty("Test1", data)
                    );

            using (JTokenReader jsonReader = new JTokenReader(o))
            {
                jsonReader.Read();
                Assert.Equal(JsonToken.StartObject, jsonReader.TokenType);

                jsonReader.Read();
                Assert.Equal(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.Equal("Test1", jsonReader.Value);

                byte[] readBytes = jsonReader.ReadAsBytes();
                Assert.Equal(data, readBytes);

                Assert.True(jsonReader.Read());
                Assert.Equal(JsonToken.EndObject, jsonReader.TokenType);

                Assert.False(jsonReader.Read());
                Assert.Equal(JsonToken.None, jsonReader.TokenType);
            }
        }