Fix not returning negative double from box cache (#2777)

This commit is contained in:
James Newton-King
2022-12-16 20:39:41 +08:00
committed by GitHub
parent 2afdccdb63
commit c908de3017
2 changed files with 58 additions and 5 deletions
+54 -3
View File
@@ -74,6 +74,22 @@ namespace Newtonsoft.Json.Tests.Issues
Assert.AreEqual("0.0", d.ToString());
}
[Test]
public void Test_Deserialize_Negative()
{
decimal d = JsonConvert.DeserializeObject<decimal>("-0.0");
Assert.AreEqual("0.0", d.ToString());
}
[Test]
public void Test_Deserialize_NegativeNoTrailingZero()
{
decimal d = JsonConvert.DeserializeObject<decimal>("-0");
Assert.AreEqual("0", d.ToString());
}
[Test]
public void Test_Deserialize_MultipleTrailingZeroes()
{
@@ -100,18 +116,18 @@ namespace Newtonsoft.Json.Tests.Issues
FloatParseHandling = FloatParseHandling.Decimal
};
decimal? parsedDecimal = null;
decimal? parsedValue = null;
while (reader.Read())
{
if (reader.TokenType == JsonToken.Float)
{
parsedDecimal = (decimal)reader.Value;
parsedValue = (decimal)reader.Value;
break;
}
}
Assert.AreEqual("0.0", parsedDecimal.ToString());
Assert.AreEqual("0.0", parsedValue.ToString());
}
[Test]
@@ -144,7 +160,42 @@ namespace Newtonsoft.Json.Tests.Issues
#else
Assert.IsFalse(object.ReferenceEquals(boxedDecimals[0], boxedDecimals[1]));
#endif
}
[Test]
public void Test_Deserialize_Double_Negative()
{
double d = JsonConvert.DeserializeObject<double>("-0.0");
#if NETCOREAPP3_1_OR_GREATER
Assert.AreEqual("-0", d.ToString());
#else
Assert.AreEqual("0", d.ToString());
#endif
}
[Test]
public void Test_Deserialize_Double_NegativeNoTrailingZero()
{
double d = JsonConvert.DeserializeObject<double>("-0");
#if NETCOREAPP3_1_OR_GREATER
Assert.AreEqual("-0", d.ToString());
#else
Assert.AreEqual("0", d.ToString());
#endif
}
[Test]
public void JValueDouble_ToString()
{
var d = new JValue(-0.0d);
#if NETCOREAPP3_1_OR_GREATER
Assert.AreEqual("-0", d.ToString());
#else
Assert.AreEqual("0", d.ToString());
#endif
}
}
}
@@ -131,7 +131,8 @@ namespace Newtonsoft.Json.Utilities
{
if (value == 0.0d)
{
return DoubleZero;
// Double supports -0.0. Detection logic from https://stackoverflow.com/a/4739883/11829.
return double.IsNegativeInfinity(1.0 / value) ? DoubleNegativeZero : DoubleZero;
}
if (double.IsInfinity(value))
{
@@ -147,6 +148,7 @@ namespace Newtonsoft.Json.Utilities
internal static readonly object DoubleNaN = double.NaN;
internal static readonly object DoublePositiveInfinity = double.PositiveInfinity;
internal static readonly object DoubleNegativeInfinity = double.NegativeInfinity;
internal static readonly object DoubleZero = (double)0;
internal static readonly object DoubleZero = 0.0d;
internal static readonly object DoubleNegativeZero = -0.0d;
}
}