本文整理汇总了C#中Newtonsoft.Json.JsonTextReader.ReadAsInt32方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.ReadAsInt32方法的具体用法?C# JsonTextReader.ReadAsInt32怎么用?C# JsonTextReader.ReadAsInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.JsonTextReader
的用法示例。
在下文中一共展示了JsonTextReader.ReadAsInt32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessRequestCoreAsync
protected override async Task ProcessRequestCoreAsync(HttpContext ctx, string viewer, string world, long startTime)
{
var req = ctx.Request;
var resp = ctx.Response;
if(!req.IsAuthenticated) {
resp.StatusCode = 403;
return;
}
var currentTs = Utils.UnixTimestamp.CurrentMillisecondTimestamp;
var start2Ts = start2Timestamp;
if((currentTs - startTime < Start2ValidityPeriod) && (currentTs - start2Ts > Start2ValidityPeriod)) {
string referer = null;
if(req.Headers["Referer"] != null) {
var hdrLine = req.Headers["Referer"];
var schema = hdrLine.IndexOf("//") + 2;
var kcsUrl = hdrLine.IndexOf("/kcs", schema);
if(kcsUrl > 0) {
referer = world + hdrLine.Substring(kcsUrl + 1);
}
}
var proxyResponse = await Utils.Forwarder.ForwardRequest(req, world + "kcsapi/api_start2", referer);
if(proxyResponse != null) {
using(proxyResponse) {
if(proxyResponse.StatusCode == HttpStatusCode.OK) {
string newStart2;
using(StreamReader rdr = new StreamReader(proxyResponse.GetResponseStream())) {
newStart2 = (await rdr.ReadToEndAsync());
}
JsonReader json = new JsonTextReader(new StringReader(newStart2.Substring(7)));
while(json.Read()) {
if(json.TokenType == JsonToken.PropertyName && json.Value.ToString() == "api_result") {
if(json.ReadAsInt32() == 1) {
if(System.Threading.Interlocked.CompareExchange(ref start2Timestamp, currentTs, start2Ts) == start2Ts) {
start2Content = newStart2;
File.WriteAllText(ctx.Server.MapPath("~/App_Data/api_start2.json"), start2Content);
}
}
break;
}
}
}
}
}
}
if(start2Content == null) {
ctx.Response.StatusCode = 503;
return;
}
ctx.Response.ContentType = "text/plain";
ctx.Response.Write(start2Content);
return;
}
示例2: Read
public void Read(JsonTextReader reader)
{
Regions.Clear();
if ( !reader.ReadStartObject() ) return;
if ( reader.ReadPropertyName() != "regions" ) return;
if ( !reader.ReadStartArray() ) return;
while ( reader.ReadStartObject() ) {
if ( reader.ReadPropertyName() != "start" ) break;
int start = reader.ReadAsInt32().Value;
if ( reader.ReadPropertyName() != "length" ) break;
int end = reader.ReadAsInt32().Value;
reader.ReadEndObject();
Tuple<int, int> region = new Tuple<int,int>(start, end);
Regions.Add(region);
}
}
示例3: ReadInvalidNonBase10Number
public void ReadInvalidNonBase10Number()
{
string json = "0aq2dun13.hod";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid number. Path '', line 1, position 2.",
() => { reader.Read(); });
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid decimal. Path '', line 1, position 2.",
() => { reader.ReadAsDecimal(); });
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid integer. Path '', line 1, position 2.",
() => { reader.ReadAsInt32(); });
}
示例4: TryParseNetworkMessage
private bool TryParseNetworkMessage(string messageText, out INetworkMessage message)
{
/* Let's assume message structure is:
* {
* messagetype: short,
* version: byte,
* payload: {
*
* serialised form of message payload type
* }
*/
short messageType = 0;
byte version = 0;
long? correlationId = null;
JObject payloadJson = null;
message = null;
using (var text = new StringReader(messageText))
using (var reader = new JsonTextReader(text) {CloseInput = true})
{
while (reader.Read())
{
if (reader.Value == null || reader.TokenType != JsonToken.PropertyName)
continue;
var propertyName = reader.Value.ToString();
switch (propertyName)
{
case "messageType":
messageType = (short)(reader.ReadAsInt32() ?? 0);
if (messageType == 0)
return false;
break;
case "version":
version = (byte)(reader.ReadAsInt32() ?? 0);
if (version == 0)
return false;
break;
case "correlationId":
var correlationAsDecimal = reader.ReadAsDecimal();
if (correlationAsDecimal.HasValue)
correlationId = (long)correlationAsDecimal.Value;
break;
case "payload":
reader.Read();
// todo: better solution to this???
payloadJson = JObject.Load(reader);
break;
default:
return false;
}
}
}
if (messageType == 0 || version == 0 || payloadJson == null)
return false;
var payloadRegistration = MessageFactory.GetPayloadRegistration(messageType, version);
message = payloadRegistration.CreateMessage(
correlationId,
(MessagePayload)serializer.Deserialize(
payloadJson.CreateReader(),
payloadRegistration.PayloadType));
return true;
}
示例5: ReadAsIntDecimal
public void ReadAsIntDecimal()
{
string json = @"{""Name"": 1.1}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
ExceptionAssert.Throws<FormatException>(
"Input string was not in a correct format.",
() =>
{
reader.ReadAsInt32();
});
}
示例6: ParseIntegers
public void ParseIntegers()
{
JsonTextReader reader = null;
reader = new JsonTextReader(new StringReader("1"));
Assert.AreEqual(1, reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("-1"));
Assert.AreEqual(-1, reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("0"));
Assert.AreEqual(0, reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("-0"));
Assert.AreEqual(0, reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader(int.MaxValue.ToString()));
Assert.AreEqual(int.MaxValue, reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader(int.MinValue.ToString()));
Assert.AreEqual(int.MinValue, reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader(long.MaxValue.ToString()));
ExceptionAssert.Throws<OverflowException>("Arithmetic operation resulted in an overflow.", () => reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
ExceptionAssert.Throws<FormatException>("Input string was not in a correct format.", () => reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("1E-06"));
ExceptionAssert.Throws<FormatException>("Input string was not in a correct format.", () => reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("1.1"));
ExceptionAssert.Throws<FormatException>("Input string was not in a correct format.", () => reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader(""));
Assert.AreEqual(null, reader.ReadAsInt32());
reader = new JsonTextReader(new StringReader("-"));
ExceptionAssert.Throws<FormatException>("Input string was not in a correct format.", () => reader.ReadAsInt32());
}
示例7: ReadInt32Overflow
public void ReadInt32Overflow()
{
long i = int.MaxValue;
JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture)));
reader.Read();
Assert.AreEqual(typeof(long), reader.ValueType);
for (int j = 1; j < 1000; j++)
{
long total = j + i;
ExceptionAssert.Throws<JsonReaderException>(
"JSON integer " + total + " is too large or small for an Int32. Path '', line 1, position 10.",
() =>
{
reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture)));
reader.ReadAsInt32();
});
}
}
示例8: ReadInvalidNonBase10Number
public void ReadInvalidNonBase10Number()
{
string json = "0aq2dun13.hod";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: q. Path '', line 1, position 2.",
() => { reader.Read(); });
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: q. Path '', line 1, position 2.",
() => { reader.ReadAsDecimal(); });
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: q. Path '', line 1, position 2.",
() => { reader.ReadAsInt32(); });
}
示例9: ReadIntegerWithErrorInArray
public void ReadIntegerWithErrorInArray()
{
string json = @"[
333333333333333333333333333333333333333,
3.3,
,
0f
]";
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.StartArray, jsonTextReader.TokenType);
ExceptionAssert.Throws<JsonReaderException>(
"JSON integer 333333333333333333333333333333333333333 is too large or small for an Int32. Path '[0]', line 2, position 42.",
() => jsonTextReader.ReadAsInt32());
ExceptionAssert.Throws<JsonReaderException>(
"Input string '3.3' is not a valid integer. Path '[1]', line 3, position 6.",
() => jsonTextReader.ReadAsInt32());
ExceptionAssert.Throws<JsonReaderException>(
"Error reading integer. Unexpected token: Undefined. Path '[2]', line 4, position 3.",
() => jsonTextReader.ReadAsInt32());
ExceptionAssert.Throws<JsonReaderException>(
"Input string '0f' is not a valid integer. Path '[3]', line 5, position 5.",
() => jsonTextReader.ReadAsInt32());
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.EndArray, jsonTextReader.TokenType);
Assert.IsFalse(jsonTextReader.Read());
}
示例10: ReadStringValue_CommaErrors_Multiple
public void ReadStringValue_CommaErrors_Multiple()
{
JsonTextReader reader = new JsonTextReader(new StringReader("['',,'']"));
reader.Read();
reader.ReadAsInt32();
ExceptionAssert.Throws<JsonReaderException>(() =>
{
reader.ReadAsString();
}, "Unexpected character encountered while parsing value: ,. Path '[1]', line 1, position 5.");
Assert.AreEqual(string.Empty, reader.ReadAsString());
Assert.IsTrue(reader.Read());
}
示例11: ReadNumberValue_InvalidEndArray
public void ReadNumberValue_InvalidEndArray()
{
JsonTextReader reader = new JsonTextReader(new StringReader("]"));
ExceptionAssert.Throws<JsonReaderException>(() =>
{
reader.ReadAsInt32();
}, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1.");
}
示例12: ReadNumberValue_CommaErrors
public void ReadNumberValue_CommaErrors()
{
JsonTextReader reader = new JsonTextReader(new StringReader("[,1]"));
reader.Read();
ExceptionAssert.Throws<JsonReaderException>(() =>
{
reader.ReadAsInt32();
}, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 2.");
Assert.AreEqual(1, reader.ReadAsInt32());
Assert.IsTrue(reader.Read());
}
示例13: ReadInt32WithBadCharacter
public void ReadInt32WithBadCharacter()
{
JsonReader reader = new JsonTextReader(new StringReader(@"true"));
ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsInt32(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1.");
}
示例14: ReadIntegerWithErrorInArray
public void ReadIntegerWithErrorInArray()
{
string json = @"[
333333333333333333333333333333333333333,
3.3,
,
0f
]";
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.StartArray, jsonTextReader.TokenType);
ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "JSON integer 333333333333333333333333333333333333333 is too large or small for an Int32. Path '[0]', line 2, position 41.");
ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "Input string '3.3' is not a valid integer. Path '[1]', line 3, position 5.");
ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "Unexpected character encountered while parsing value: ,. Path '[2]', line 4, position 3.");
ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "Input string '0f' is not a valid integer. Path '[3]', line 5, position 4.");
Assert.IsTrue(jsonTextReader.Read());
Assert.AreEqual(JsonToken.EndArray, jsonTextReader.TokenType);
Assert.IsFalse(jsonTextReader.Read());
}
示例15: ParseJsonResponse
public static ApiResponse ParseJsonResponse(Stream stream)
{
using (var jsonReader = new JsonTextReader(new StreamReader(stream)))
{
var apiResponse = new ApiResponse();
while (jsonReader.Read())
{
if (jsonReader.TokenType == JsonToken.PropertyName)
{
switch ((string)jsonReader.Value)
{
case "statusCode":
apiResponse.StatusCode = jsonReader.ReadAsInt32() ?? 0;
break;
case "count":
apiResponse.Count = jsonReader.ReadAsInt32() ?? 0;
break;
case "response":
apiResponse.Response = jsonReader.GetInnerJson();
break;
case "error":
jsonReader.Read();
throw ParseError(jsonReader);
default:
jsonReader.Skip();
break;
}
}
}
return apiResponse;
}
}