本文整理汇总了C#中Raven.Imports.Newtonsoft.Json.JsonTextReader.ReadAsDecimal方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.ReadAsDecimal方法的具体用法?C# JsonTextReader.ReadAsDecimal怎么用?C# JsonTextReader.ReadAsDecimal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Raven.Imports.Newtonsoft.Json.JsonTextReader
的用法示例。
在下文中一共展示了JsonTextReader.ReadAsDecimal方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例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: 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);
}
示例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: 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);
}
示例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: 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();
});
}
示例8: ReadAsDecimalNoContent
public void ReadAsDecimalNoContent()
{
JsonTextReader reader = new JsonTextReader(new StringReader(@""));
Assert.IsNull(reader.ReadAsDecimal());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例9: 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();
}
示例10: ReadInvalidNonBase10Number
public void ReadInvalidNonBase10Number()
{
string json = "0aq2dun13.hod";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>(() => { reader.Read(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2.");
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsDecimal(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2.");
reader = new JsonTextReader(new StringReader(json));
ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsInt32(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2.");
}
示例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>(() => reader.ReadAsDecimal(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77.");
reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
reader.FloatParseHandling = Json.FloatParseHandling.Decimal;
ExceptionAssert.Throws<JsonReaderException>(() => reader.Read(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77.");
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>(() => reader.ReadAsDecimal(), "Input string '-' is not a valid decimal. Path '', line 1, position 1.");
}