本文整理汇总了C#中JsonTextReader类的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader类的具体用法?C# JsonTextReader怎么用?C# JsonTextReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonTextReader类属于命名空间,在下文中一共展示了JsonTextReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Example
public void Example()
{
#region Usage
// person.json, has a relative external schema reference 'address.json'
// --------
// {
// 'type': 'object',
// 'properties': {
// 'name': {'type':'string'},
// 'addresses': {
// 'type': 'array',
// 'items': {'$ref': 'address.json'}
// }
// }
// }
// --------
using (StreamReader file = File.OpenText(@"c:\person.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JSchemaUrlResolver resolver = new JSchemaUrlResolver();
JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
{
Resolver = resolver,
// where the schema is being loaded from
// referenced 'address.json' schema will be loaded from disk at 'c:\address.json'
BaseUri = new Uri(@"c:\person.json")
});
// validate JSON
}
#endregion
}
示例2: ReadFrom
public void ReadFrom()
{
JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader("{'pie':true}")));
Assert.AreEqual(true, (bool)o["pie"]);
JArray a = (JArray)JToken.ReadFrom(new JsonTextReader(new StringReader("[1,2,3]")));
Assert.AreEqual(1, (int)a[0]);
Assert.AreEqual(2, (int)a[1]);
Assert.AreEqual(3, (int)a[2]);
JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
reader.Read();
reader.Read();
JProperty p = (JProperty)JToken.ReadFrom(reader);
Assert.AreEqual("pie", p.Name);
Assert.AreEqual(true, (bool)p.Value);
JConstructor c = (JConstructor)JToken.ReadFrom(new JsonTextReader(new StringReader("new Date(1)")));
Assert.AreEqual("Date", c.Name);
Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));
JValue v;
v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""stringvalue""")));
Assert.AreEqual("stringvalue", (string)v);
v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1")));
Assert.AreEqual(1, (int)v);
v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1.1")));
Assert.AreEqual(1.1, (double)v);
}
示例3: Example
public void Example()
{
#region Usage
string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";
IList<Role> roles = new List<Role>();
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.SupportMultipleContent = true;
while (true)
{
if (!reader.Read())
break;
JsonSerializer serializer = new JsonSerializer();
Role role = serializer.Deserialize<Role>(reader);
roles.Add(role);
}
foreach (Role role in roles)
{
Console.WriteLine(role.Name);
}
// Admin
// Publisher
#endregion
Assert.AreEqual(2, roles.Count);
Assert.AreEqual("Admin", roles[0].Name);
Assert.AreEqual("Publisher", roles[1].Name);
}
示例4: ReadLargeJson
public static void ReadLargeJson()
{
using (var jsonFile = System.IO.File.OpenText("large.json"))
using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
}
Console.WriteLine("Press any key to start deserialization");
Console.ReadKey();
Console.WriteLine("Deserializing...");
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 5; i++)
{
using (var jsonFile = System.IO.File.OpenText("large.json"))
using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
}
}
sw.Stop();
Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds);
}
示例5: AssertImport
private static void AssertImport(object expected, string input)
{
JsonTextReader reader = new JsonTextReader(new StringReader(input));
Type expectedType = expected.GetType();
object o = JsonImporterStock.Get(expectedType).Import(reader);
Assert.IsInstanceOfType(expectedType, o);
Assert.AreEqual(expected, o);
}
示例6: CheckInnerReader
public void CheckInnerReader()
{
string json = "{'name':'James','hobbies':['pie','cake']}";
JsonReader reader = new JsonTextReader(new StringReader(json));
JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
Assert.AreEqual(reader, validatingReader.Reader);
}
示例7: AssertImport
private static void AssertImport(object expected, string input)
{
JsonTextReader reader = new JsonTextReader(new StringReader(input));
Type expectedType = expected.GetType();
ImportContext context = new ImportContext();
object o = context.Import(expectedType, reader);
Assert.IsInstanceOfType(expectedType, o);
Assert.AreEqual(expected, o);
}
示例8: Load
public void Load()
{
JsonReader reader = new JsonTextReader(new StringReader("new Date(123)"));
reader.Read();
JConstructor constructor = JConstructor.Load(reader);
Assert.AreEqual("Date", constructor.Name);
Assert.IsTrue(JToken.DeepEquals(new JValue(123), constructor.Values().ElementAt(0)));
}
示例9: ReaderValidation
private void ReaderValidation()
{
JsonTextReader reader = new JsonTextReader(new StringReader(Json));
JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
validatingReader.Schema = Schema;
while (validatingReader.Read())
{
}
}
示例10: AssertImport
private static void AssertImport(object expected, string input)
{
JsonTextReader reader = new JsonTextReader(new StringReader(input));
GuidImporter importer = new GuidImporter();
object o = importer.Import(new ImportContext(), reader);
Assert.IsTrue(reader.EOF, "Reader must be at EOF.");
if (expected != null)
Assert.IsInstanceOfType(typeof(Guid), o);
Assert.AreEqual(expected, o);
}
示例11: Example
public void Example()
{
JObject o1 = JObject.Parse(File.ReadAllText(@"c:\videogames.json"));
// read JSON directly from a file
using (StreamReader file = File.OpenText(@"c:\videogames.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o2 = (JObject) JToken.ReadFrom(reader);
}
}
示例12: CanReadComma
public void CanReadComma()
{
// arrange
var reader = new JsonTextReader(new StringReader(","));
// act
var token = reader.NextToken();
// assert
Assert.Equal(JsonToken.Comma, token);
}
示例13: CanReadDecimal
public void CanReadDecimal()
{
// arrange
var reader = new JsonTextReader(new StringReader("123.456"));
// act
var token = reader.NextToken();
// assert
Assert.Equal(new JsonToken(JsonTokenKind.Fractional, "123.456"), token);
}
示例14: CanReadBooleanTrue
public void CanReadBooleanTrue()
{
// arrange
var reader = new JsonTextReader(new StringReader("true"));
// act
var token = reader.NextToken();
// assert
Assert.Equal(JsonToken.True, token);
}
示例15: ReadDollarQuoteStringWithTag
public void ReadDollarQuoteStringWithTag()
{
string json = @"{""NameOfStore"":$tag$Forest's Bakery And Cafe$tag$}";
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
jsonTextReader.Read();
jsonTextReader.Read();
jsonTextReader.Read();
Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
}