Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not returning negative double from box cache #2777

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions Src/Newtonsoft.Json.Tests/Issues/Issue2768.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}