Skip to content

Commit

Permalink
Jrm/core/nan deserializer (#2550)
Browse files Browse the repository at this point in the history
* Disabled network hosting

* fix(revit): Networks no longer nested under their host

* fix(revit): views, and materials now not in collections

* fix(core): Added support for deserializing NaN and Infinity json values
  • Loading branch information
JR-Morgan authored May 12, 2023
1 parent f82e4ad commit e3f2225
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
31 changes: 28 additions & 3 deletions Core/Core/Serialisation/ValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,21 @@ public static bool ConvertValue(Type type, object value, out object convertedVal
convertedValue = (double)(long)value;
return true;
}
switch (value)
{
case "NaN":
convertedValue = double.NaN;
return true;
case "Infinity":
convertedValue = double.PositiveInfinity;
return true;
case "-Infinity":
convertedValue = double.NegativeInfinity;
return true;
default:
return false;
}

return false;
case "Single":
if (valueType == typeof(double))
{
Expand All @@ -127,8 +140,20 @@ public static bool ConvertValue(Type type, object value, out object convertedVal
convertedValue = (float)(long)value;
return true;
}

return false;
switch (value)
{
case "NaN":
convertedValue = float.NaN;
return true;
case "Infinity":
convertedValue = float.PositiveInfinity;
return true;
case "-Infinity":
convertedValue = float.NegativeInfinity;
return true;
default:
return false;
}

#endregion
}
Expand Down
50 changes: 43 additions & 7 deletions Core/Tests/SerializerNonBreakingChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ public void IntToEnum(MyEnum testCase)
var res = from.SerializeAsTAndDeserialize<EnumValueMock>();
Assert.That(res.value, Is.EqualTo(testCase));
}

[Test]
[TestCaseSource(nameof(Float64TestCases))]
[TestCaseSource(nameof(Float32TestCases))]
public void DoubleToDouble(double testCase)
{
var from = new DoubleValueMock { value = testCase };

var res = from.SerializeAsTAndDeserialize<DoubleValueMock>();
Assert.That(res.value, Is.EqualTo(testCase));
}
}

/// <summary>
Expand Down Expand Up @@ -213,15 +224,13 @@ protected SerializerMock()

public override string speckle_type => _speckle_type;

public void SerializeAs<T>()
where T : Base, new()
public void SerializeAs<T>() where T : Base, new()
{
T target = new();
_speckle_type = target.speckle_type;
}

internal TTo SerializeAsTAndDeserialize<TTo>()
where TTo : Base, new()
internal TTo SerializeAsTAndDeserialize<TTo>() where TTo : Base, new()
{
SerializeAs<TTo>();

Expand All @@ -241,11 +250,38 @@ public abstract class PrimitiveTestFixture
public static int[] Int32TestCases = { int.MinValue, int.MaxValue };
public static long[] Int64TestCases = { long.MaxValue, long.MinValue };

public static double[] Float64TestCases = { default, double.Epsilon, double.MaxValue, double.MinValue };
public static double[] Float64TestCases =
{
default,
double.Epsilon,
double.MaxValue,
double.MinValue,
double.PositiveInfinity,
double.NegativeInfinity,
double.NaN
};

public static float[] Float32TestCases = { default, float.Epsilon, float.MaxValue, float.MinValue };
public static float[] Float32TestCases =
{
default,
float.Epsilon,
float.MaxValue,
float.MinValue,
float.PositiveInfinity,
float.NegativeInfinity,
float.NaN
};

public static Half[] Float16TestCases = { default, Half.Epsilon, Half.MaxValue, Half.MinValue };
public static Half[] Float16TestCases =
{
default,
Half.Epsilon,
Half.MaxValue,
Half.MinValue,
Half.PositiveInfinity,
Half.NegativeInfinity,
Half.NaN
};
public static float[] FloatIntegralTestCases = { 0, 1, int.MaxValue, int.MinValue };

public static MyEnum[] MyEnums => Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().ToArray();
Expand Down

0 comments on commit e3f2225

Please sign in to comment.