-Fixed roundtripping double.MaxValue and float.MaxValue as dictionary keys
This commit is contained in:
@@ -63,6 +63,39 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[TestFixture]
|
||||
public class JsonSerializerCollectionsTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void DoubleKey_WholeValue()
|
||||
{
|
||||
Dictionary<double, int> dictionary = new Dictionary<double, int> { { 1d, 1 } };
|
||||
string output = JsonConvert.SerializeObject(dictionary);
|
||||
Assert.AreEqual(@"{""1"":1}", output);
|
||||
|
||||
Dictionary<double, int> deserializedValue = JsonConvert.DeserializeObject<Dictionary<double, int>>(output);
|
||||
Assert.AreEqual(1d, deserializedValue.First().Key);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DoubleKey_MaxValue()
|
||||
{
|
||||
Dictionary<double, int> dictionary = new Dictionary<double, int> { { double.MaxValue, 1 } };
|
||||
string output = JsonConvert.SerializeObject(dictionary);
|
||||
Assert.AreEqual(@"{""1.7976931348623157E+308"":1}", output);
|
||||
|
||||
Dictionary<double, int> deserializedValue = JsonConvert.DeserializeObject<Dictionary<double, int>>(output);
|
||||
Assert.AreEqual(double.MaxValue, deserializedValue.First().Key);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FloatKey_MaxValue()
|
||||
{
|
||||
Dictionary<float, int> dictionary = new Dictionary<float, int> { { float.MaxValue, 1 } };
|
||||
string output = JsonConvert.SerializeObject(dictionary);
|
||||
Assert.AreEqual(@"{""3.40282347E+38"":1}", output);
|
||||
|
||||
Dictionary<float, int> deserializedValue = JsonConvert.DeserializeObject<Dictionary<float, int>>(output);
|
||||
Assert.AreEqual(float.MaxValue, deserializedValue.First().Key);
|
||||
}
|
||||
|
||||
public class TestCollectionPrivateParameterized : IEnumerable<int>
|
||||
{
|
||||
private readonly List<int> _bars;
|
||||
|
||||
@@ -1130,28 +1130,49 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (contract.ContractType == JsonContractType.Primitive)
|
||||
{
|
||||
JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract;
|
||||
if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTime || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeNullable)
|
||||
switch (primitiveContract.TypeCode)
|
||||
{
|
||||
DateTime dt = DateTimeUtils.EnsureDateTime((DateTime)name, writer.DateTimeZoneHandling);
|
||||
case PrimitiveTypeCode.DateTime:
|
||||
case PrimitiveTypeCode.DateTimeNullable:
|
||||
{
|
||||
DateTime dt = DateTimeUtils.EnsureDateTime((DateTime)name, writer.DateTimeZoneHandling);
|
||||
|
||||
escape = false;
|
||||
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
|
||||
DateTimeUtils.WriteDateTimeString(sw, dt, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
|
||||
return sw.ToString();
|
||||
}
|
||||
escape = false;
|
||||
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
|
||||
DateTimeUtils.WriteDateTimeString(sw, dt, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
|
||||
return sw.ToString();
|
||||
}
|
||||
#if !NET20
|
||||
else if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffset || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffsetNullable)
|
||||
{
|
||||
escape = false;
|
||||
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
|
||||
DateTimeUtils.WriteDateTimeOffsetString(sw, (DateTimeOffset)name, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
|
||||
return sw.ToString();
|
||||
}
|
||||
case PrimitiveTypeCode.DateTimeOffset:
|
||||
case PrimitiveTypeCode.DateTimeOffsetNullable:
|
||||
{
|
||||
escape = false;
|
||||
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
|
||||
DateTimeUtils.WriteDateTimeOffsetString(sw, (DateTimeOffset)name, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
|
||||
return sw.ToString();
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
escape = true;
|
||||
return Convert.ToString(name, CultureInfo.InvariantCulture);
|
||||
case PrimitiveTypeCode.Double:
|
||||
case PrimitiveTypeCode.DoubleNullable:
|
||||
{
|
||||
double d = (double)name;
|
||||
|
||||
escape = false;
|
||||
return d.ToString("R", CultureInfo.InvariantCulture);
|
||||
}
|
||||
case PrimitiveTypeCode.Single:
|
||||
case PrimitiveTypeCode.SingleNullable:
|
||||
{
|
||||
float f = (float)name;
|
||||
|
||||
escape = false;
|
||||
return f.ToString("R", CultureInfo.InvariantCulture);
|
||||
}
|
||||
default:
|
||||
{
|
||||
escape = true;
|
||||
return Convert.ToString(name, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (TryConvertToString(name, name.GetType(), out propertyName))
|
||||
|
||||
Reference in New Issue
Block a user