예제 #1
0
    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
0
    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
0
    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
0
    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
0
    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
0
    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
0
    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
0
    public void ReadAsDecimalNoContent()
    {
      JsonTextReader reader = new JsonTextReader(new StringReader(@""));

      Assert.IsNull(reader.ReadAsDecimal());
      Assert.AreEqual(JsonToken.None, reader.TokenType);
    }
예제 #9
0
        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
0
        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
0
        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.");
        }