Skip to content

Commit

Permalink
Fix not returning negative double from box cache (#2777)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Dec 16, 2022
1 parent 2afdccd commit c908de3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
57 changes: 54 additions & 3 deletions Src/Newtonsoft.Json.Tests/Issues/Issue2768.cs
Expand Up @@ -74,6 +74,22 @@ public void Test_Deserialize()
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()
{
Expand All @@ -100,18 +116,18 @@ public void ParseJsonDecimal()
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]
Expand Down Expand Up @@ -144,7 +160,42 @@ public void ParseJsonDecimal_IsBoxedInstanceSame()
#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
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions Src/Newtonsoft.Json/Utilities/BoxedPrimitives.cs
Expand Up @@ -131,7 +131,8 @@ internal static object Get(double value)
{
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))
{
Expand All @@ -147,6 +148,7 @@ internal static object Get(double value)
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;
}
}

0 comments on commit c908de3

Please sign in to comment.