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

Put in a strategy to determine how NaN/Infinity is handled #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/SimpleJson.Tests/SerializeObject.Primitive.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public void UnsingedInt32Serialization()
public void DoubleSerialization()
{
Assert.AreEqual("1.1", SimpleJson.SerializeObject(1.1));
Assert.AreEqual("NaN", SimpleJson.SerializeObject(double.NaN));
Assert.AreEqual("-Infinity", SimpleJson.SerializeObject(double.NegativeInfinity));
// Check with a strategy that does other things to Infinity/NaN
Assert.AreEqual("\"-Infinity\"", SimpleJson.SerializeObject(double.NegativeInfinity, new InfinityAsStringJsonSerializerStrategy()));
Assert.AreEqual("\"NaN\"", SimpleJson.SerializeObject(double.NaN, new InfinityAsStringJsonSerializerStrategy()));
}

[TestMethod]
Expand Down
42 changes: 40 additions & 2 deletions src/SimpleJson/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,13 @@ static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, objec
IEnumerable enumerableValue = value as IEnumerable;
if (enumerableValue != null)
success = SerializeArray(jsonSerializerStrategy, enumerableValue, builder);
else if (IsNumeric(value))
else if (IsNaNorInfinite(value)) {
object coerced = jsonSerializerStrategy.CoerceInfiniteOrNaN(value);
if (IsNumeric(coerced))
success = SerializeNumber(value, builder);
else
success = SerializeValue(jsonSerializerStrategy, coerced, builder);
} else if (IsNumeric(value))
success = SerializeNumber(value, builder);
else if (value is bool)
builder.Append((bool)value ? "true" : "false");
Expand Down Expand Up @@ -1178,6 +1184,25 @@ static bool IsNumeric(object value)
return false;
}

/// <summary>
/// Determines if a given object is a floating point representation,
/// (e.g. double or float) and that object is Infinity or NaN
/// </summary>
static bool IsNaNorInfinite(object value)
{
if (value is float)
{
if (float.IsInfinity((float)value)) return true;
if (float.IsNaN((float)value)) return true;
}
else if (value is double)
{
if (double.IsInfinity((double)value)) return true;
if (double.IsNaN((double)value)) return true;
}
return false;
}

private static IJsonSerializerStrategy _currentJsonSerializerStrategy;
public static IJsonSerializerStrategy CurrentJsonSerializerStrategy
{
Expand Down Expand Up @@ -1234,6 +1259,7 @@ interface IJsonSerializerStrategy
[SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate", Justification="Need to support .NET 2")]
bool TrySerializeNonPrimitiveObject(object input, out object output);
object DeserializeObject(object value, Type type);
object CoerceInfiniteOrNaN(object value);
}

[GeneratedCode("simple-json", "1.0.0")]
Expand Down Expand Up @@ -1512,6 +1538,11 @@ protected virtual bool TrySerializeUnknownTypes(object input, out object output)
output = obj;
return true;
}

public virtual object CoerceInfiniteOrNaN(object value)
{
return value;
}
}

#if SIMPLE_JSON_DATACONTRACT
Expand Down Expand Up @@ -1590,9 +1621,16 @@ private static bool CanAdd(MemberInfo info, out string jsonKey)
return true;
}
}

#endif

class InfinityAsStringJsonSerializerStrategy : PocoJsonSerializerStrategy
{
public override object CoerceInfiniteOrNaN(object value)
{
return value.ToString();
}
}
namespace Reflection
{
// This class is meant to be copied into other libraries. So we want to exclude it from Code Analysis rules
Expand Down