本文整理汇总了C#中Newtonsoft.Json.JsonTextReader类的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader类的具体用法?C# JsonTextReader怎么用?C# JsonTextReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonTextReader类属于Newtonsoft.Json命名空间,在下文中一共展示了JsonTextReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleLoadedProperty
protected override void HandleLoadedProperty (JsonTextReader reader, string propertyName, object readValue) {
base.HandleLoadedProperty (reader, propertyName, readValue);
switch(propertyName) {
case "AimRotation": aimRotation = LoadManager.LoadQuaternion(reader); break;
default: break;
}
}
示例2: ReadRedirects
public static List<Redirect> ReadRedirects(HttpContext context)
{
if (RedirectHandler.Redirects == null)
{
string path = WebConfigurationManager.AppSettings["redirects"] ?? "~/App_Data/redirects.json";
path = context.Server.MapPath(path);
List<Redirect> newRedirects = new List<Redirect>();
using (TextReader file = File.OpenText(path))
using (JsonTextReader reader = new JsonTextReader(file))
{
string from = null;
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName)
{
from = ((string)reader.Value).ToLowerInvariant();
}
else if (reader.TokenType == JsonToken.String)
{
newRedirects.Add(new Redirect() { From = from, To = ((string)reader.Value).ToLowerInvariant() });
}
}
}
RedirectHandler.Redirects = newRedirects;
}
return RedirectHandler.Redirects;
}
示例3: ConstructorTest
public void ConstructorTest()
{
string repositoryName = "Mvc3Application";
string appName = "ConstructorTest";
using (var repo = Git.CreateLocalRepository(repositoryName))
{
ApplicationManager.Run(appName, appManager =>
{
using (HttpClient client = HttpClientHelper.CreateClient(appManager.ServiceUrl, appManager.DeploymentManager.Credentials))
{
HttpResponseMessage response = client.GetAsync("diagnostics/settings").Result.EnsureSuccessful();
using (var reader = new JsonTextReader(new StreamReader(response.Content.ReadAsStreamAsync().Result)))
{
JObject json = (JObject)JToken.ReadFrom(reader);
Assert.Equal(0, json.Count);
}
}
using (HttpClient client = HttpClientHelper.CreateClient(appManager.ServiceUrl, appManager.DeploymentManager.Credentials))
{
var ex = Assert.Throws<HttpRequestException>(() => client.GetAsync("diagnostics/settings/trace_level").Result.EnsureSuccessful());
Assert.Contains("404", ex.Message);
}
});
}
}
示例4: LoadColor
public static Color LoadColor(JsonTextReader reader)
{
if (reader == null) return new Color(0, 0, 0, 0);
Color color = new Color(0, 0, 0, 0);
string currVal = "";
while (reader.Read())
{
if (reader.Value != null)
{
if (reader.TokenType == JsonToken.PropertyName) currVal = (string)reader.Value;
else
{
switch (currVal)
{
case "r": color.r = (float)(double)reader.Value; break;
case "g": color.g = (float)(double)reader.Value; break;
case "b": color.b = (float)(double)reader.Value; break;
case "a": color.a = (float)(double)reader.Value; break;
default: break;
}
}
}
else if (reader.TokenType == JsonToken.EndObject) return color;
}
return color;
}
示例5: Load
public static void Load()
{
players.Clear();
string filename = "SavedGames" + Path.DirectorySeparatorChar + "Players.json";
if (File.Exists(filename))
{
//read contents of file
string input;
using (StreamReader sr = new StreamReader(filename))
{
input = sr.ReadToEnd();
}
if (input != null)
{
//parse contents of file
using (JsonTextReader reader = new JsonTextReader(new StringReader(input)))
{
while (reader.Read())
{
if (reader.Value != null)
{
if (reader.TokenType == JsonToken.PropertyName)
{
if ((string)reader.Value == "Players") LoadPlayers(reader);
}
}
}
}
}
}
}
示例6: YahooFinance
public void YahooFinance()
{
string input = @"{
""matches"" : [
{""t"":""C"", ""n"":""Citigroup Inc."", ""e"":""NYSE"", ""id"":""662713""}
,{""t"":""CHL"", ""n"":""China Mobile Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""660998""}
,{""t"":""PTR"", ""n"":""PetroChina Company Limited (ADR)"", ""e"":""NYSE"", ""id"":""664536""}
,{""t"":""RIO"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""671472""}
,{""t"":""RIOPR"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""3512643""}
,{""t"":""CSCO"", ""n"":""Cisco Systems, Inc."", ""e"":""NASDAQ"", ""id"":""99624""}
,{""t"":""CVX"", ""n"":""Chevron Corporation"", ""e"":""NYSE"", ""id"":""667226""}
,{""t"":""TM"", ""n"":""Toyota Motor Corporation (ADR)"", ""e"":""NYSE"", ""id"":""655880""}
,{""t"":""JPM"", ""n"":""JPMorgan Chase \\x26 Co."", ""e"":""NYSE"", ""id"":""665639""}
,{""t"":""COP"", ""n"":""ConocoPhillips"", ""e"":""NYSE"", ""id"":""1691168""}
,{""t"":""LFC"", ""n"":""China Life Insurance Company Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""688679""}
,{""t"":""NOK"", ""n"":""Nokia Corporation (ADR)"", ""e"":""NYSE"", ""id"":""657729""}
,{""t"":""KO"", ""n"":""The Coca-Cola Company"", ""e"":""NYSE"", ""id"":""6550""}
,{""t"":""VZ"", ""n"":""Verizon Communications Inc."", ""e"":""NYSE"", ""id"":""664887""}
,{""t"":""AMX"", ""n"":""America Movil S.A.B de C.V. (ADR)"", ""e"":""NYSE"", ""id"":""665834""}],
""all"" : false
}
";
using (JsonReader jsonReader = new JsonTextReader(new StringReader(input)))
{
while (jsonReader.Read())
{
Console.WriteLine(jsonReader.Value);
}
}
}
示例7: ParseStepsAsync
private async Task ParseStepsAsync(
Stream stream,
RecipeDescriptor descriptor,
Func<RecipeDescriptor, RecipeStepDescriptor, Task> stepActionAsync)
{
var serializer = new JsonSerializer();
StreamReader streamReader = new StreamReader(stream);
JsonTextReader reader = new JsonTextReader(streamReader);
// Go to Steps, then iterate.
while (reader.Read())
{
if (reader.Path == "steps" && reader.TokenType == JsonToken.StartArray)
{
int stepId = 0;
while (reader.Read() && reader.Depth > 1)
{
if (reader.Depth == 2)
{
var child = JToken.Load(reader);
await stepActionAsync(descriptor, new RecipeStepDescriptor
{
Id = (stepId++).ToString(CultureInfo.InvariantCulture),
RecipeName = descriptor.Name,
Name = child.Value<string>("name"),
Step = child
});
}
}
}
}
}
示例8: Load
public void Load(ZipArchive iArchive)
{
JObject pJOtImages = null;
ZipArchiveEntry pZAEImages = iArchive.GetEntry("images.json");
if (pZAEImages != null)
{
using (Stream pStmImages = pZAEImages.Open())
{
using (StreamReader pSRrReader = new StreamReader(pStmImages, Encoding.UTF8, false, 1024, true))
{
using (JsonTextReader pJTRReader = new JsonTextReader(pSRrReader))
{
pJOtImages = JObject.Load(pJTRReader);
}
}
}
}
JArray pJAyImages = pJOtImages["images"].Value<JArray>(); ;
foreach(JObject curImage in pJAyImages)
{
ProjectImage pPIeImage = ProjectImage.FromJSON(curImage);
ZipArchiveEntry pZAEImage = iArchive.GetEntry(pPIeImage.ID);
if (pZAEImage != null)
{
using (Stream pStmImage = pZAEImage.Open())
{
pPIeImage.Image = Image.FromStream(pStmImage);
cDicImages.Add(pPIeImage.ID, pPIeImage);
}
}
}
}
示例9: makeRequestForm
public RequestForm makeRequestForm(JObject json)
{
string currManagerName = (string)json["current"]["bazookaInfo"]["managerId"];
string futureManagerName = (string)json["future"]["bazookaInfo"]["managerId"];
int managerID = GetIDFromName(currManagerName);
int f_managerID = GetIDFromName(futureManagerName);
json["current"]["bazookaInfo"]["managerId"] = managerID;
json["current"]["ultiproInfo"]["supervisor"] = managerID;
json["future"]["bazookaInfo"]["managerId"] = f_managerID;
json["future"]["ultiproInfo"]["supervisor"] = f_managerID;
UserController uc = new UserController();
string[] name = uc.GetUserName().Split('.');
string creatorName = name[0] + " " + name[1];
int creatorID = GetIDFromName(creatorName);
RequestForm obj = null;
using (var sr = new StringReader(json.ToString()))
using (var jr = new JsonTextReader(sr))
{
var js = new JsonSerializer();
obj = (RequestForm)js.Deserialize<RequestForm>(jr);
}
obj.EmployeeId = GetIDFromName((string)json["name"]);
obj.CreatedByID = creatorID;
obj.Current.BazookaInfo.SecurityItemRights = "";
obj.ReviewInfo.FilesToBeRemovedFrom = "(" + obj.Current.BazookaInfo.Group + ")" + currManagerName.Replace(" ", ".")+" "+obj.ReviewInfo.FilesToBeRemovedFrom;
obj.ReviewInfo.FilesToBeAddedTo = "(" + obj.Future.BazookaInfo.Group + ")" + futureManagerName.Replace(" ", ".")+" "+obj.ReviewInfo.FilesToBeAddedTo;
return obj;
}
示例10: LoadGame
public static void LoadGame(string filename)
{
char separator = Path.DirectorySeparatorChar;
string path = "SavedGames" + separator + PlayerManager.GetPlayerName() + separator + filename + ".json";
if(!File.Exists(path)) {
Debug.Log("Unable to find " + path + ". Loading will crash, so aborting.");
return;
}
string input;
using(StreamReader sr = new StreamReader(path)) {
input = sr.ReadToEnd();
}
if(input != null) {
//parse contents of file
using(JsonTextReader reader = new JsonTextReader(new StringReader(input))) {
while(reader.Read()) {
if(reader.Value!=null) {
if(reader.TokenType == JsonToken.PropertyName) {
string property = (string)reader.Value;
switch(property) {
case "Sun": LoadLighting(reader); break;
case "Ground": LoadTerrain(reader); break;
case "Camera": LoadCamera(reader); break;
case "Resources": LoadResources(reader); break;
case "Players": LoadPlayers(reader); break;
default: break;
}
}
}
}
}
}
}
示例11: GetMedia
public static ObservableCollection<MediaFile> GetMedia()
{
if (File.Exists(libraryCachePath))
{
using (var file = File.OpenRead(libraryCachePath))
using (var sr = new StreamReader(file))
using (var jtr = new JsonTextReader(sr))
{
return new ObservableCollection<MediaFile>(serializer.Deserialize<List<MediaFile>>(jtr));
}
}
else
{
var library = RefreshLibrary();
if (!Directory.Exists(Path.GetDirectoryName(libraryCachePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(libraryCachePath));
}
using (var file = File.Open(libraryCachePath, FileMode.Create))
using (var sw = new StreamWriter(file))
using (var jtr = new JsonTextWriter(sw))
{
serializer.Serialize(jtr, library);
}
return new ObservableCollection<MediaFile>(library);
}
}
示例12: ReadBigInteger
public void ReadBigInteger()
{
string json = @"{
ParentId: 1,
ChildId: 333333333333333333333333333333333333333,
}";
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);
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.Integer, jsonTextReader.TokenType);
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType);
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.Integer, jsonTextReader.TokenType);
Assert.AreEqual(typeof(BigInteger), jsonTextReader.ValueType);
Assert.AreEqual(BigInteger.Parse("333333333333333333333333333333333333333"), jsonTextReader.Value);
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType);
Assert.IsFalse(jsonTextReader.Read());
JObject o = JObject.Parse(json);
var i = (BigInteger)((JValue)o["ChildId"]).Value;
Assert.AreEqual(BigInteger.Parse("333333333333333333333333333333333333333"), i);
}
示例13: ReadFromStreamAsync
//public JsonFormatter()
//{
// SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));
// SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
//}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var task = new TaskCompletionSource<object>();
using (var ms = new MemoryStream())
{
readStream.CopyTo(ms);
//ms.Seek(0, SeekOrigin.Begin);
//var result = JsonSchemaValidator.Instance().Validate(ms, type);
//if (!string.IsNullOrWhiteSpace(result))
// task.SetResult(result);
//else
{
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new JsonTextReader(new StreamReader(ms)))
{
var serializer = new JsonSerializer();
task.SetResult(serializer.Deserialize(reader, type));
}
}
}
return task.Task;
}
示例14: Parse
public void Parse()
{
if (_IsParsed) return;
var json = Encoding.UTF8.GetString(this.data);
using (var strReader = new System.IO.StringReader(json)) {
using (var r = new JsonTextReader(strReader)) {
while (r.Read()) {
if (r.TokenType == JsonToken.PropertyName) {
switch (r.Value.ToString()) {
case "region":
ParseRegions(r);
break;
case "nonpop":
_NonPops = r.ReadInt32Array();
break;
case "item":
_Items = r.ReadInt32Array();
break;
case "instance_contents":
_InstanceContents = r.ReadInt32Array();
break;
default:
Console.Error.WriteLine("Unknown 'BNpcName' data key: {0}", r.Value);
throw new NotSupportedException();
}
}
}
}
}
_IsParsed = true;
}
示例15: DeserializeXmlNode
private XmlNode DeserializeXmlNode(string json, string deserializeRootElementName)
{
JsonTextReader reader;
reader = new JsonTextReader(new StringReader(json));
reader.Read();
XmlNodeConverter converter = new XmlNodeConverter();
if (deserializeRootElementName != null)
converter.DeserializeRootElementName = deserializeRootElementName;
XmlNode node = (XmlNode)converter.ReadJson(reader, typeof (XmlDocument), null, new JsonSerializer());
#if !NET20
string xmlText = node.OuterXml;
reader = new JsonTextReader(new StringReader(json));
reader.Read();
XDocument d = (XDocument) converter.ReadJson(reader, typeof (XDocument), null, new JsonSerializer());
string linqXmlText = d.ToString(SaveOptions.DisableFormatting);
if (d.Declaration != null)
linqXmlText = d.Declaration + linqXmlText;
Assert.AreEqual(xmlText, linqXmlText);
#endif
return node;
}