本文整理汇总了C#中JsonTextReader.Read方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.Read方法的具体用法?C# JsonTextReader.Read怎么用?C# JsonTextReader.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonTextReader
的用法示例。
在下文中一共展示了JsonTextReader.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: 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);
}
示例3: 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);
}
示例4: 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)));
}
示例5: 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);
#if !NET20
v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""1970-01-01T00:00:00+12:31"""))
{
DateParseHandling = DateParseHandling.DateTimeOffset
});
Assert.AreEqual(typeof(DateTimeOffset), v.Value.GetType());
Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, new TimeSpan(12, 31, 0)), v.Value);
#endif
}
示例6: ParseRequest
private void ParseRequest(string request)
{
using (var stringReader = new StringReader(request))
using (var reader = new JsonTextReader(stringReader))
{
reader.DateParseHandling = DateParseHandling.None;
// Messages parsed as a dictionary with the following
// parameters:
//
// 'r': Request type: 0, 1 or 2
// 'a': Association ID when the request type is 1 or 2
// 't': Message type
// 'p': Payload
if (!reader.Read())
throw new HttpException("Invalid request");
if (reader.TokenType == JsonToken.StartObject)
{
ProcessMessageRequest(reader);
}
else if (reader.TokenType == JsonToken.StartArray)
{
ReadToken(reader, JsonToken.String);
string action = (string)reader.Value;
ReadToken(reader, JsonToken.EndArray);
switch (action)
{
case "close":
_client.Dispose();
break;
default:
throw new HttpException("Invalid request");
}
}
else
{
throw new HttpException("Invalid request");
}
if (reader.Read())
throw new HttpException("Invalid request");
}
}
示例7: Example
public void Example()
{
#region Usage
string json = @"{
'CPU': 'Intel',
'PSU': '500W',
'Drives': [
'DVD read/writer'
/*(broken)*/,
'500 gigabyte hard drive',
'200 gigabype hard drive'
]
}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
while (reader.Read())
{
if (reader.Value != null)
{
Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
}
else
{
Console.WriteLine("Token: {0}", reader.TokenType);
}
}
// Token: StartObject
// Token: PropertyName, Value: CPU
// Token: String, Value: Intel
// Token: PropertyName, Value: PSU
// Token: String, Value: 500W
// Token: PropertyName, Value: Drives
// Token: StartArray
// Token: String, Value: DVD read/writer
// Token: Comment, Value: (broken)
// Token: String, Value: 500 gigabyte hard drive
// Token: String, Value: 200 gigabype hard drive
// Token: EndArray
// Token: EndObject
#endregion
}
开发者ID:Chunshan-Theta,项目名称:Xamarin_WeatherAPP_iOS_Android-,代码行数:42,代码来源:ReadJsonWithJsonTextReader.cs
示例8: LoadFromNestedObjectIncomplete
public void LoadFromNestedObjectIncomplete()
{
string jsonText = @"{
""short"":
{
""error"":
{
""code"":0";
JsonReader reader = new JsonTextReader(new StringReader(jsonText));
reader.Read();
reader.Read();
reader.Read();
reader.Read();
reader.Read();
JToken.ReadFrom(reader);
}
示例9: ToPerson
public static Person ToPerson(this string s)
{
StringReader sr = new StringReader(s);
JsonTextReader reader = new JsonTextReader(sr);
Person p = new Person();
// {
reader.Read();
// "name"
reader.Read();
// "Jerry"
p.Name = reader.ReadAsString();
// "likes"
reader.Read();
// [
reader.Read();
// "Comedy", "Superman", ]
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
{
p.Likes.Add((string)reader.Value);
}
// }
reader.Read();
return p;
}
示例10: ReadWithSupportMultipleContent
public void ReadWithSupportMultipleContent()
{
string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";
IList<JObject> roles = new List<JObject>();
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.SupportMultipleContent = true;
while (true)
{
JObject role = (JObject)JToken.ReadFrom(reader);
roles.Add(role);
if (!reader.Read())
break;
}
Assert.AreEqual(2, roles.Count);
Assert.AreEqual("Admin", (string)roles[0]["name"]);
Assert.AreEqual("Publisher", (string)roles[1]["name"]);
}
示例11: LoadFromNestedObjectIncomplete
public void LoadFromNestedObjectIncomplete()
{
ExceptionAssert.Throws<JsonReaderException>("Unexpected end of content while loading JObject. Path 'short.error.code', line 6, position 15.",
() =>
{
string jsonText = @"{
""short"":
{
""error"":
{
""code"":0";
JsonReader reader = new JsonTextReader(new StringReader(jsonText));
reader.Read();
reader.Read();
reader.Read();
reader.Read();
reader.Read();
JToken.ReadFrom(reader);
});
}
示例12: DeserializeMessage
private static object DeserializeMessage(JsonTextReader reader, ServiceType type, bool skipRead)
{
Require.NotNull(reader, "reader");
Require.NotNull(type, "type");
if (!skipRead && !reader.Read())
throw new HttpException("Invalid request");
if (reader.TokenType == JsonToken.Null)
return null;
object instance = Activator.CreateInstance(type.Type);
if (reader.TokenType != JsonToken.StartObject)
throw new HttpException("Invalid request");
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
else if (reader.TokenType != JsonToken.PropertyName)
throw new HttpException("Invalid request");
int tag;
if (!int.TryParse((string)reader.Value, NumberStyles.None, CultureInfo.InvariantCulture, out tag))
throw new HttpException("Invalid request");
ServiceTypeField field;
if (!type.Fields.TryGetValue(tag, out field))
throw new HttpException("Unknown tag");
if (field.CollectionType != null)
{
var collection = new ArrayList();
if (!reader.Read())
throw new HttpException("Invalid request");
if (reader.TokenType == JsonToken.Null)
continue;
if (reader.TokenType != JsonToken.StartArray)
throw new HttpException("Invalid request");
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndArray)
break;
if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
collection.Add(DeserializeMessage(reader, field.ServiceType, true));
else
collection.Add(Convert.ChangeType(reader.Value, field.Type));
}
if (reader.TokenType != JsonToken.EndArray)
throw new HttpException("Invalid request");
var targetCollection = (IList)Activator.CreateInstance(field.CollectionType, new object[] { collection.Count });
for (int i = 0; i < collection.Count; i++)
{
if (field.CollectionType.IsArray)
targetCollection[i] = collection[i];
else
targetCollection.Add(collection[i]);
}
field.Setter(instance, targetCollection);
}
else if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
{
if (!reader.Read())
throw new HttpException("Invalid request");
field.Setter(instance, DeserializeMessage(reader, field.ServiceType, true));
}
else
{
if (!reader.Read())
throw new HttpException("Invalid request");
field.Setter(instance, reader.Value);
}
}
if (reader.TokenType != JsonToken.EndObject)
throw new HttpException("Invalid request");
return instance;
}
示例13: ReadToken
private static void ReadToken(JsonTextReader reader, JsonToken token)
{
if (!reader.Read() || reader.TokenType != token)
throw new HttpException("Invalid request");
}
示例14: ProcessMessageRequest
private void ProcessMessageRequest(JsonTextReader reader)
{
MessageKind? messageKind = null;
int? associationId = null;
int? messageType = null;
bool hadPayload = false;
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
else if (reader.TokenType != JsonToken.PropertyName)
throw new HttpException("Invalid request");
switch ((string)reader.Value)
{
case "r":
ReadToken(reader, JsonToken.Integer);
int value = (int)(long)reader.Value;
if (value < 0 || value > 2)
throw new HttpException("Invalid request type");
messageKind = (MessageKind)value;
break;
case "a":
ReadToken(reader, JsonToken.Integer);
associationId = (int)(long)reader.Value;
if (associationId < 0)
throw new HttpException("Invalid association id");
break;
case "t":
ReadToken(reader, JsonToken.Integer);
messageType = (int)(long)reader.Value;
if (messageType < 0)
throw new HttpException("Invalid message type");
break;
case "p":
hadPayload = true;
if (
!messageKind.HasValue ||
!messageType.HasValue ||
(messageKind.Value != MessageKind.OneWay && !associationId.HasValue)
)
throw new HttpException("Invalid request");
ProcessRequest(
messageKind.Value,
messageType.Value,
(uint)associationId.GetValueOrDefault(0),
reader
);
break;
default:
throw new HttpException("Invalid request");
}
}
if (!hadPayload)
throw new HttpException("Invalid request");
}
示例15: ReadDollarQuotePropertyWithTag
public void ReadDollarQuotePropertyWithTag()
{
string json = @"{$pp$Name'Of""Store$pp$:$tag$Forest's Bakery And Cafe$tag$}";
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
jsonTextReader.Read();
jsonTextReader.Read();
Assert.AreEqual(@"Name'Of""Store", jsonTextReader.Value);
jsonTextReader.Read();
Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
}