-Fixed reading scientific notation numbers with no decimal point

-Changed the ReadAsXXX methods to return null at the end of an array instead of throwing an error
This commit is contained in:
JamesNK
2012-02-18 01:04:50 +00:00
parent 446b6f0d0a
commit c876e1d9cc
9 changed files with 163 additions and 88 deletions
@@ -941,7 +941,7 @@ bye", reader.Value);
public void ReadFloatingPointNumber()
{
string json =
@"[0.0,0.0,0.1,1.0,1.000001,1E-06,4.94065645841247E-324,Infinity,-Infinity,NaN,1.7976931348623157E+308,-1.7976931348623157E+308,Infinity,-Infinity,NaN]";
@"[0.0,0.0,0.1,1.0,1.000001,1E-06,4.94065645841247E-324,Infinity,-Infinity,NaN,1.7976931348623157E+308,-1.7976931348623157E+308,Infinity,-Infinity,NaN,0e-10,0.25e-5,0.3e10]";
using (JsonReader jsonReader = new JsonTextReader(new StringReader(json)))
{
@@ -1008,6 +1008,18 @@ bye", reader.Value);
Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
Assert.AreEqual(double.NaN, jsonReader.Value);
jsonReader.Read();
Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
Assert.AreEqual(0, jsonReader.Value);
jsonReader.Read();
Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
Assert.AreEqual(0.0000025, jsonReader.Value);
jsonReader.Read();
Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
Assert.AreEqual(3000000000, jsonReader.Value);
jsonReader.Read();
Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType);
}
@@ -1933,5 +1945,73 @@ bye", reader.Value);
Assert.IsTrue(reader.Read());
reader.Read();
}
[Test]
public void ScientificNotation()
{
double d;
d = Convert.ToDouble("6.0221418e23");
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(0, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0.0000025, reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(3000000000, 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(0, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0.0000025, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(3000000000, reader.Value);
reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(602214180000000000000000m, reader.Value);
reader.Read();
}
}
}
}
@@ -65,7 +65,7 @@ namespace Newtonsoft.Json.Tests
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("James", reader.Value.ToString());
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.AreEqual(typeof (string), reader.ValueType);
Assert.AreEqual('"', reader.QuoteChar);
Assert.IsTrue(reader.Read());
@@ -302,7 +302,7 @@ namespace Newtonsoft.Json.Tests
}
[Test]
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Integer 10 exceeds maximum value of 5. Line 1, position 2.")]
[ExpectedException(typeof (JsonSchemaException), ExpectedMessage = "Integer 10 exceeds maximum value of 5. Line 1, position 2.")]
public void ThrowExceptionWhenNoValidationEventHandler()
{
string schemaJson = @"{
@@ -597,13 +597,13 @@ namespace Newtonsoft.Json.Tests
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 11.", validationEventArgs.Message);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNotNull(validationEventArgs);
}
[Test]
public void ArrayCountGreaterThanMaximumItems()
{
@@ -887,7 +887,11 @@ namespace Newtonsoft.Json.Tests
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; errors.Add(validationEventArgs.Message); };
reader.ValidationEventHandler += (sender, args) =>
{
validationEventArgs = args;
errors.Add(validationEventArgs.Message);
};
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
@@ -1175,7 +1179,11 @@ namespace Newtonsoft.Json.Tests
List<string> errors = new List<string>();
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; errors.Add(validationEventArgs.Message); };
reader.ValidationEventHandler += (sender, args) =>
{
validationEventArgs = args;
errors.Add(validationEventArgs.Message);
};
reader.Schema = secondSchema;
Assert.IsTrue(reader.Read());
@@ -1232,7 +1240,7 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual("three", reader.Value.ToString());
Assert.AreEqual(5, errors.Count);
Assert.AreEqual("String 'three' is less than minimum length of 6. Line 6, position 25.", errors[4]);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
@@ -1380,57 +1388,70 @@ namespace Newtonsoft.Json.Tests
[Test]
public void ReadAsInt32()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (int));
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1")))
{
Schema = s
};
{
Schema = s
};
int? i = reader.ReadAsInt32();
Assert.AreEqual(1, i);
}
[Test]
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Integer 5 exceeds maximum value of 2. Line 1, position 1.")]
[ExpectedException(typeof (JsonSchemaException), ExpectedMessage = "Integer 5 exceeds maximum value of 2. Line 1, position 1.")]
public void ReadAsInt32Failure()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (int));
s.Maximum = 2;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5")))
{
Schema = s
};
{
Schema = s
};
reader.ReadAsInt32();
}
[Test]
public void ReadAsDecimal()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (decimal));
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1.5")))
{
Schema = s
};
{
Schema = s
};
decimal? d = reader.ReadAsDecimal();
Assert.AreEqual(1.5, d);
}
[Test]
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Float 5.5 is not evenly divisible by 1. Line 1, position 3.")]
[ExpectedException(typeof (JsonSchemaException), ExpectedMessage = "Float 5.5 is not evenly divisible by 1. Line 1, position 3.")]
public void ReadAsDecimalFailure()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (decimal));
s.DivisibleBy = 1;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
{
Schema = s
};
{
Schema = s
};
reader.ReadAsDecimal();
}
[Test]
public void ReadAsInt32FromSerializer()
{
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("[1,2,3]")));
reader.Schema = new JsonSchemaGenerator().Generate(typeof(int[]));
int[] values = new JsonSerializer().Deserialize<int[]>(reader);
Assert.AreEqual(3, values.Length);
Assert.AreEqual(1, values[0]);
Assert.AreEqual(2, values[1]);
Assert.AreEqual(3, values[2]);
}
}
}
@@ -72,5 +72,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.8.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.8.14611")]
[assembly: AssemblyFileVersion("4.0.8.14618")]
#endif