-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() public void ReadFloatingPointNumber()
{ {
string json = 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))) using (JsonReader jsonReader = new JsonTextReader(new StringReader(json)))
{ {
@@ -1008,6 +1008,18 @@ bye", reader.Value);
Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
Assert.AreEqual(double.NaN, jsonReader.Value); 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(); jsonReader.Read();
Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType); Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType);
} }
@@ -1933,5 +1945,73 @@ bye", reader.Value);
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
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.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType); Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("James", reader.Value.ToString()); Assert.AreEqual("James", reader.Value.ToString());
Assert.AreEqual(typeof(string), reader.ValueType); Assert.AreEqual(typeof (string), reader.ValueType);
Assert.AreEqual('"', reader.QuoteChar); Assert.AreEqual('"', reader.QuoteChar);
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
@@ -302,7 +302,7 @@ namespace Newtonsoft.Json.Tests
} }
[Test] [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() public void ThrowExceptionWhenNoValidationEventHandler()
{ {
string schemaJson = @"{ string schemaJson = @"{
@@ -597,13 +597,13 @@ namespace Newtonsoft.Json.Tests
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType); Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 11.", validationEventArgs.Message); Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 11.", validationEventArgs.Message);
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNotNull(validationEventArgs); Assert.IsNotNull(validationEventArgs);
} }
[Test] [Test]
public void ArrayCountGreaterThanMaximumItems() public void ArrayCountGreaterThanMaximumItems()
{ {
@@ -887,7 +887,11 @@ namespace Newtonsoft.Json.Tests
Json.Schema.ValidationEventArgs validationEventArgs = null; Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json))); 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); reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
@@ -1175,7 +1179,11 @@ namespace Newtonsoft.Json.Tests
List<string> errors = new List<string>(); List<string> errors = new List<string>();
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json))); 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; reader.Schema = secondSchema;
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
@@ -1232,7 +1240,7 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual("three", reader.Value.ToString()); Assert.AreEqual("three", reader.Value.ToString());
Assert.AreEqual(5, errors.Count); Assert.AreEqual(5, errors.Count);
Assert.AreEqual("String 'three' is less than minimum length of 6. Line 6, position 25.", errors[4]); Assert.AreEqual("String 'three' is less than minimum length of 6. Line 6, position 25.", errors[4]);
Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
@@ -1380,57 +1388,70 @@ namespace Newtonsoft.Json.Tests
[Test] [Test]
public void ReadAsInt32() 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"))) JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1")))
{ {
Schema = s Schema = s
}; };
int? i = reader.ReadAsInt32(); int? i = reader.ReadAsInt32();
Assert.AreEqual(1, i); Assert.AreEqual(1, i);
} }
[Test] [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() public void ReadAsInt32Failure()
{ {
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int)); JsonSchema s = new JsonSchemaGenerator().Generate(typeof (int));
s.Maximum = 2; s.Maximum = 2;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5"))) JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5")))
{ {
Schema = s Schema = s
}; };
reader.ReadAsInt32(); reader.ReadAsInt32();
} }
[Test] [Test]
public void ReadAsDecimal() 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"))) JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1.5")))
{ {
Schema = s Schema = s
}; };
decimal? d = reader.ReadAsDecimal(); decimal? d = reader.ReadAsDecimal();
Assert.AreEqual(1.5, d); Assert.AreEqual(1.5, d);
} }
[Test] [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() public void ReadAsDecimalFailure()
{ {
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal)); JsonSchema s = new JsonSchemaGenerator().Generate(typeof (decimal));
s.DivisibleBy = 1; s.DivisibleBy = 1;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5.5"))) JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
{ {
Schema = s Schema = s
}; };
reader.ReadAsDecimal(); 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: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.8.0")] [assembly: AssemblyVersion("4.0.8.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.0.8.14611")] [assembly: AssemblyFileVersion("4.0.8.14618")]
#endif #endif
+8 -8
View File
@@ -176,7 +176,7 @@ namespace Newtonsoft.Json.Bson
/// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>. /// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null. /// A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null. This method will return <c>null</c> at the end of an array.
/// </returns> /// </returns>
public override byte[] ReadAsBytes() public override byte[] ReadAsBytes()
{ {
@@ -195,7 +195,7 @@ namespace Newtonsoft.Json.Bson
if (TokenType == JsonToken.Bytes) if (TokenType == JsonToken.Bytes)
return (byte[])Value; return (byte[])Value;
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading bytes. Expected bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading bytes. Expected bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -228,7 +228,7 @@ namespace Newtonsoft.Json.Bson
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Decimal}"/>.</returns> /// <returns>A <see cref="Nullable{Decimal}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override decimal? ReadAsDecimal() public override decimal? ReadAsDecimal()
{ {
Read(); Read();
@@ -256,7 +256,7 @@ namespace Newtonsoft.Json.Bson
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading decimal. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading decimal. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -265,7 +265,7 @@ namespace Newtonsoft.Json.Bson
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Int32}"/>.</returns> /// <returns>A <see cref="Nullable{Int32}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override int? ReadAsInt32() public override int? ReadAsInt32()
{ {
Read(); Read();
@@ -293,7 +293,7 @@ namespace Newtonsoft.Json.Bson
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading integer. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading integer. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -304,7 +304,7 @@ namespace Newtonsoft.Json.Bson
/// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="Nullable{DateTimeOffset}"/>. /// A <see cref="Nullable{DateTimeOffset}"/>. This method will return <c>null</c> at the end of an array.
/// </returns> /// </returns>
public override DateTimeOffset? ReadAsDateTimeOffset() public override DateTimeOffset? ReadAsDateTimeOffset()
{ {
@@ -333,7 +333,7 @@ namespace Newtonsoft.Json.Bson
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading date. Expected date but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading date. Expected date but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
+4 -15
View File
@@ -102,14 +102,8 @@ namespace Newtonsoft.Json
private char _quoteChar; private char _quoteChar;
internal State _currentState; internal State _currentState;
private JTokenType _currentTypeContext; private JTokenType _currentTypeContext;
private bool _serializerInArray;
private CultureInfo _culture; private CultureInfo _culture;
internal void SetSerializeInArray(bool serializerInArray)
{
_serializerInArray = serializerInArray;
}
/// <summary> /// <summary>
/// Gets the current reader state. /// Gets the current reader state.
/// </summary> /// </summary>
@@ -235,26 +229,26 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Int32}"/>.</returns> /// <returns>A <see cref="Nullable{Int32}"/>. This method will return <c>null</c> at the end of an array.</returns>
public abstract int? ReadAsInt32(); public abstract int? ReadAsInt32();
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>. /// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null.</returns> /// <returns>A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null. This method will return <c>null</c> at the end of an array.</returns>
public abstract byte[] ReadAsBytes(); public abstract byte[] ReadAsBytes();
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Decimal}"/>.</returns> /// <returns>A <see cref="Nullable{Decimal}"/>. This method will return <c>null</c> at the end of an array.</returns>
public abstract decimal? ReadAsDecimal(); public abstract decimal? ReadAsDecimal();
#if !NET20 #if !NET20
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{DateTimeOffset}"/>.</returns> /// <returns>A <see cref="Nullable{DateTimeOffset}"/>. This method will return <c>null</c> at the end of an array.</returns>
public abstract DateTimeOffset? ReadAsDateTimeOffset(); public abstract DateTimeOffset? ReadAsDateTimeOffset();
#endif #endif
@@ -434,11 +428,6 @@ namespace Newtonsoft.Json
} }
} }
internal bool ReaderIsSerializerInArray()
{
return (TokenType == JsonToken.EndArray && _serializerInArray);
}
/// <summary> /// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary> /// </summary>
+12 -9
View File
@@ -362,7 +362,7 @@ namespace Newtonsoft.Json
/// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>. /// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null. /// A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null. This method will return <c>null</c> at the end of an array.
/// </returns> /// </returns>
public override byte[] ReadAsBytes() public override byte[] ReadAsBytes()
{ {
@@ -414,7 +414,7 @@ namespace Newtonsoft.Json
throw CreateReaderException(this, "Unexpected end when reading bytes."); throw CreateReaderException(this, "Unexpected end when reading bytes.");
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -423,7 +423,7 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Decimal}"/>.</returns> /// <returns>A <see cref="Nullable{Decimal}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override decimal? ReadAsDecimal() public override decimal? ReadAsDecimal()
{ {
_readType = ReadType.ReadAsDecimal; _readType = ReadType.ReadAsDecimal;
@@ -454,7 +454,7 @@ namespace Newtonsoft.Json
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Unexpected token when reading decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Unexpected token when reading decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -463,7 +463,7 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Int32}"/>.</returns> /// <returns>A <see cref="Nullable{Int32}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override int? ReadAsInt32() public override int? ReadAsInt32()
{ {
_readType = ReadType.ReadAsInt32; _readType = ReadType.ReadAsInt32;
@@ -494,7 +494,7 @@ namespace Newtonsoft.Json
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Unexpected token when reading integer: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Unexpected token when reading integer: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -504,7 +504,7 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="DateTimeOffset"/>.</returns> /// <returns>A <see cref="DateTimeOffset"/>. This method will return <c>null</c> at the end of an array.</returns>
public override DateTimeOffset? ReadAsDateTimeOffset() public override DateTimeOffset? ReadAsDateTimeOffset()
{ {
_readType = ReadType.ReadAsDateTimeOffset; _readType = ReadType.ReadAsDateTimeOffset;
@@ -535,7 +535,7 @@ namespace Newtonsoft.Json
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Unexpected token when reading date: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Unexpected token when reading date: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -1270,7 +1270,10 @@ namespace Newtonsoft.Json
JsonToken numberType; JsonToken numberType;
bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1); bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1);
bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1 && _stringReference.Chars[_stringReference.StartIndex + 1] != '.'); bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1
&& _stringReference.Chars[_stringReference.StartIndex + 1] != '.'
&& _stringReference.Chars[_stringReference.StartIndex + 1] != 'e'
&& _stringReference.Chars[_stringReference.StartIndex + 1] != 'E');
if (_readType == ReadType.ReadAsInt32) if (_readType == ReadType.ReadAsInt32)
{ {
+9 -9
View File
@@ -32,7 +32,7 @@ namespace Newtonsoft.Json.Linq
/// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>. /// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null. /// A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null. This method will return <c>null</c> at the end of an array.
/// </returns> /// </returns>
public override byte[] ReadAsBytes() public override byte[] ReadAsBytes()
{ {
@@ -59,7 +59,7 @@ namespace Newtonsoft.Json.Linq
if (TokenType == JsonToken.Bytes) if (TokenType == JsonToken.Bytes)
return (byte[])Value; return (byte[])Value;
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading bytes. Expected bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading bytes. Expected bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -92,7 +92,7 @@ namespace Newtonsoft.Json.Linq
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Decimal}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Decimal}"/>.</returns> /// <returns>A <see cref="Nullable{Decimal}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override decimal? ReadAsDecimal() public override decimal? ReadAsDecimal()
{ {
Read(); Read();
@@ -120,7 +120,7 @@ namespace Newtonsoft.Json.Linq
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading decimal. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading decimal. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -129,7 +129,7 @@ namespace Newtonsoft.Json.Linq
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{Int32}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{Int32}"/>.</returns> /// <returns>A <see cref="Nullable{Int32}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override int? ReadAsInt32() public override int? ReadAsInt32()
{ {
Read(); Read();
@@ -157,7 +157,7 @@ namespace Newtonsoft.Json.Linq
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading integer. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading integer. Expected a number but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -167,7 +167,7 @@ namespace Newtonsoft.Json.Linq
/// <summary> /// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>. /// Reads the next JSON token from the stream as a <see cref="Nullable{DateTimeOffset}"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Nullable{DateTimeOffset}"/>.</returns> /// <returns>A <see cref="Nullable{DateTimeOffset}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override DateTimeOffset? ReadAsDateTimeOffset() public override DateTimeOffset? ReadAsDateTimeOffset()
{ {
Read(); Read();
@@ -194,8 +194,8 @@ namespace Newtonsoft.Json.Linq
throw CreateReaderException(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, Value)); throw CreateReaderException(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
} }
} }
if (ReaderIsSerializerInArray()) if (TokenType == JsonToken.EndArray)
return null; return null;
throw CreateReaderException(this, "Error reading date. Expected date but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); throw CreateReaderException(this, "Error reading date. Expected date but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -85,7 +85,7 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.8.0")] [assembly: AssemblyVersion("4.0.8.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.0.8.14611")] [assembly: AssemblyFileVersion("4.0.8.14618")]
#endif #endif
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
@@ -1093,12 +1093,6 @@ namespace Newtonsoft.Json.Serialization
return propertyValues; return propertyValues;
} }
private void SetSerializeInArray(JsonReader reader, bool inArray, bool enable)
{
if (inArray)
reader.SetSerializeInArray(enable);
}
private bool ReadForType(JsonReader reader, JsonContract contract, bool hasConverter, bool inArray) private bool ReadForType(JsonReader reader, JsonContract contract, bool hasConverter, bool inArray)
{ {
// don't read properties with converters as a specific value // don't read properties with converters as a specific value
@@ -1119,29 +1113,17 @@ namespace Newtonsoft.Json.Serialization
return true; return true;
case ReadType.ReadAsInt32: case ReadType.ReadAsInt32:
SetSerializeInArray(reader, inArray, true);
reader.ReadAsInt32(); reader.ReadAsInt32();
SetSerializeInArray(reader, inArray, false);
return true; return true;
case ReadType.ReadAsDecimal: case ReadType.ReadAsDecimal:
SetSerializeInArray(reader, inArray, true);
reader.ReadAsDecimal(); reader.ReadAsDecimal();
SetSerializeInArray(reader, inArray, false);
return true; return true;
case ReadType.ReadAsBytes: case ReadType.ReadAsBytes:
SetSerializeInArray(reader, inArray, true);
reader.ReadAsBytes(); reader.ReadAsBytes();
SetSerializeInArray(reader, inArray, false);
return true; return true;
#if !NET20 #if !NET20
case ReadType.ReadAsDateTimeOffset: case ReadType.ReadAsDateTimeOffset:
SetSerializeInArray(reader, inArray, true);
reader.ReadAsDateTimeOffset(); reader.ReadAsDateTimeOffset();
SetSerializeInArray(reader, inArray, false);
return true; return true;
#endif #endif
default: default: