本文整理汇总了C#中Raven.Imports.Newtonsoft.Json.JsonTextReader.ReadAsBytes方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.ReadAsBytes方法的具体用法?C# JsonTextReader.ReadAsBytes怎么用?C# JsonTextReader.ReadAsBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Raven.Imports.Newtonsoft.Json.JsonTextReader
的用法示例。
在下文中一共展示了JsonTextReader.ReadAsBytes方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadBytesWithUnexpectedEnd
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>(
"Unterminated string. Expected delimiter: '. Path '', line 1, position 17.",
() =>
{
reader.ReadAsBytes();
});
}
示例2: ReadBytesNoStartWithUnexpectedEnd
public void ReadBytesNoStartWithUnexpectedEnd()
{
JsonReader reader = new JsonTextReader(new StringReader(@"[ "));
Assert.IsTrue(reader.Read());
Assert.IsNull(reader.ReadAsBytes());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例3: ReadAsBytesArrayWithBadContent
public void ReadAsBytesArrayWithBadContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"[1.0]"));
ExceptionAssert.Throws<JsonReaderException>(
"Unexpected token when reading bytes: Float. Path '[0]', line 1, position 4.",
() =>
{
reader.ReadAsBytes();
});
}
示例4: ReadBytesWithBadCharacter
public void ReadBytesWithBadCharacter()
{
JsonReader reader = new JsonTextReader(new StringReader(@"true"));
ExceptionAssert.Throws<JsonReaderException>(
"Error reading bytes. Unexpected token: Boolean. Path '', line 1, position 4.",
() =>
{
reader.ReadAsBytes();
});
}
示例5: ReadBytesFollowingNumberInObject
public void ReadBytesFollowingNumberInObject()
{
string helloWorld = "Hello world!";
byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);
JsonReader reader = new JsonTextReader(new StringReader(@"{num:1,data:'" + Convert.ToBase64String(helloWorldData) + @"'}"));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsTrue(reader.Read());
byte[] data = reader.ReadAsBytes();
CollectionAssert.AreEquivalent(helloWorldData, data);
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
}
示例6: ReadAsBytesIntegerArrayWithComments
public void ReadAsBytesIntegerArrayWithComments()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"[/*hi*/1/*hi*/,2/*hi*/]"));
byte[] data = reader.ReadAsBytes();
Assert.AreEqual(2, data.Length);
Assert.AreEqual(1, data[0]);
Assert.AreEqual(2, data[1]);
}
示例7: ReadAsBytesIntegerArrayWithNoEnd
public void ReadAsBytesIntegerArrayWithNoEnd()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"[1"));
ExceptionAssert.Throws<JsonReaderException>(
"Unexpected end when reading bytes. Path '[0]', line 1, position 2.",
() =>
{
reader.ReadAsBytes();
});
}
示例8: ReadAsBytesNoContentWrappedObject
public void ReadAsBytesNoContentWrappedObject()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"{"));
ExceptionAssert.Throws<JsonReaderException>(
"Unexpected end when reading bytes. Path '', line 1, position 1.",
() =>
{
reader.ReadAsBytes();
});
}
示例9: ReadAsBytesBadContent
public void ReadAsBytesBadContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
ExceptionAssert.Throws<JsonReaderException>(
"Error reading bytes. Unexpected token: StartConstructor. Path '', line 1, position 9.",
() =>
{
reader.ReadAsBytes();
});
}
示例10: ReadAsBytesNoContent
public void ReadAsBytesNoContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@""));
Assert.IsNull(reader.ReadAsBytes());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例11: ReadSingleBytes
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);
}
示例12: ReadBytesAsStreamShouldPreserveJsonTextReaderInternalState
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
}
}
示例13: ReadBytesWithError
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());
}