本文整理汇总了C#中Newtonsoft.Json.JsonTextReader.ReadAsDecimal方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.ReadAsDecimal方法的具体用法?C# JsonTextReader.ReadAsDecimal怎么用?C# JsonTextReader.ReadAsDecimal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.JsonTextReader
的用法示例。
在下文中一共展示了JsonTextReader.ReadAsDecimal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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(); });
}
示例2: WriteReadBoundaryDecimals
public void WriteReadBoundaryDecimals()
{
StringWriter sw = new StringWriter();
JsonTextWriter writer = new JsonTextWriter(sw);
writer.WriteStartArray();
writer.WriteValue(decimal.MaxValue);
writer.WriteValue(decimal.MinValue);
writer.WriteEndArray();
string json = sw.ToString();
StringReader sr = new StringReader(json);
JsonTextReader reader = new JsonTextReader(sr);
Assert.IsTrue(reader.Read());
decimal? max = reader.ReadAsDecimal();
Assert.AreEqual(decimal.MaxValue, max);
decimal? min = reader.ReadAsDecimal();
Assert.AreEqual(decimal.MinValue, min);
Assert.IsTrue(reader.Read());
}
示例3: ScientificNotation
public void ScientificNotation()
{
double d;
d = Convert.ToDouble("6.0221418e23", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString(new CultureInfo("fr-FR")));
Console.WriteLine(d.ToString("0.#############################################################################"));
//CultureInfo info = CultureInfo.GetCultureInfo("fr-FR");
//Console.WriteLine(info.NumberFormat.NumberDecimalSeparator);
string json = @"[0e-10,0E-10,0.25e-5,0.3e10,6.0221418e23]";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Read();
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0.0000025d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(3000000000d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(602214180000000000000000d, reader.Value);
reader.Read();
reader = new JsonTextReader(new StringReader(json));
reader.Read();
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0.0000025m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(3000000000m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(602214180000000000000000m, reader.Value);
reader.Read();
}
示例4: ParseHexNumber
public void ParseHexNumber()
{
string json = @"0x20";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(32m, reader.Value);
}
示例5: ReadAsDecimalFrench
public void ReadAsDecimalFrench()
{
string json = @"{""decimal"":""9,99""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Culture = new CultureInfo("fr-FR");
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
decimal? d = reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(typeof(decimal), reader.ValueType);
Assert.AreEqual(9.99m, d);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例6: ReadAsDecimal
public void ReadAsDecimal()
{
string json = @"{""decimal"":-7.92281625142643E+28}";
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);
decimal? d = reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(typeof(decimal), reader.ValueType);
Assert.AreEqual(-79228162514264300000000000000m, d);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例7: ReadAsDecimalInt
public void ReadAsDecimalInt()
{
string json = @"{""Name"":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);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(typeof(decimal), reader.ValueType);
Assert.AreEqual(1m, reader.Value);
}
示例8: ReadAsDecimalBadContent
public void ReadAsDecimalBadContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
ExceptionAssert.Throws<JsonReaderException>(
"Error reading decimal. Unexpected token: StartConstructor. Path '', line 1, position 9.",
() =>
{
reader.ReadAsDecimal();
});
}
示例9: ReadAsDecimalNoContent
public void ReadAsDecimalNoContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@""));
Assert.IsNull(reader.ReadAsDecimal());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例10: 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(); });
}
示例11: ParseDecimals
public void ParseDecimals()
{
JsonTextReader reader = null;
reader = new JsonTextReader(new StringReader("1.1"));
Assert.AreEqual(1.1m, reader.ReadAsDecimal());
reader = new JsonTextReader(new StringReader("-1.1"));
Assert.AreEqual(-1.1m, reader.ReadAsDecimal());
reader = new JsonTextReader(new StringReader("0.0"));
Assert.AreEqual(0.0m, reader.ReadAsDecimal());
reader = new JsonTextReader(new StringReader("-0.0"));
Assert.AreEqual(0, reader.ReadAsDecimal());
reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: s. Path '', line 1, position 77.", () => reader.ReadAsDecimal());
reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
reader.FloatParseHandling = Json.FloatParseHandling.Decimal;
ExceptionAssert.Throws<JsonReaderException>("Unexpected character encountered while parsing number: s. Path '', line 1, position 77.", () => reader.Read());
reader = new JsonTextReader(new StringReader("1E-06"));
Assert.AreEqual(0.000001m, reader.ReadAsDecimal());
reader = new JsonTextReader(new StringReader(""));
Assert.AreEqual(null, reader.ReadAsDecimal());
reader = new JsonTextReader(new StringReader("-"));
ExceptionAssert.Throws<JsonReaderException>("Input string '-' is not a valid decimal. Path '', line 1, position 1.", () => reader.ReadAsDecimal());
}
示例12: ScientificNotation
public void ScientificNotation()
{
double d;
d = Convert.ToDouble("6.0221418e23", CultureInfo.InvariantCulture);
Assert.AreEqual("6,0221418E+23", d.ToString(new CultureInfo("fr-FR")));
Assert.AreEqual("602214180000000000000000", d.ToString("0.#############################################################################"));
string json = @"[0e-10,0E-10,0.25e-5,0.3e10,6.0221418e23]";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Read();
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0.0000025d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(3000000000d, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(602214180000000000000000d, reader.Value);
reader.Read();
reader = new JsonTextReader(new StringReader(json));
reader.Read();
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0.0000025m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(3000000000m, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(602214180000000000000000m, reader.Value);
reader.Read();
}
示例13: 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;
}
示例14: ReadAsDecimalBadContent_SecondLine
public void ReadAsDecimalBadContent_SecondLine()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"
new Date()"));
ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsDecimal(); }, "Unexpected character encountered while parsing value: e. Path '', line 2, position 2.");
}
示例15: ReadAsDecimalBadContent
public void ReadAsDecimalBadContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
reader.ReadAsDecimal();
}