public async Task ReadBytesAsync() { byte[] data = Encoding.UTF8.GetBytes("Hello world!"); JObject o = new JObject( new JProperty("Test1", data) ); using (JTokenReader jsonReader = new JTokenReader(o)) { await jsonReader.ReadAsync(); Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType); await jsonReader.ReadAsync(); Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType); Assert.AreEqual("Test1", jsonReader.Value); byte[] readBytes = await jsonReader.ReadAsBytesAsync(); Assert.AreEqual(data, readBytes); Assert.IsTrue(await jsonReader.ReadAsync()); Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType); Assert.IsFalse(await jsonReader.ReadAsync()); Assert.AreEqual(JsonToken.None, jsonReader.TokenType); } }
public async Task ReadAsBytes_NullAsync() { JValue n = JValue.CreateNull(); JTokenReader reader = new JTokenReader(n); Assert.AreEqual(null, await reader.ReadAsBytesAsync()); }
public async Task ReadAsBytes_ArrayAsync() { JArray a = new JArray { 1, 2 }; JTokenReader reader = new JTokenReader(a); byte[] bytes = await reader.ReadAsBytesAsync(); Assert.AreEqual(2, bytes.Length); Assert.AreEqual(1, bytes[0]); Assert.AreEqual(2, bytes[1]); }
public async Task ReadBytesFailureAsync() { await ExceptionAssert.ThrowsAsync <JsonReaderException>(async() => { JObject o = new JObject( new JProperty("Test1", 1) ); using (JTokenReader jsonReader = new JTokenReader(o)) { await jsonReader.ReadAsync(); Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType); await jsonReader.ReadAsync(); Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType); Assert.AreEqual("Test1", jsonReader.Value); await jsonReader.ReadAsBytesAsync(); } }, "Error reading bytes. Unexpected token: Integer. Path 'Test1'."); }