Skip to content

Commit 5282b58

Browse files
committed
json: Pass in QuantityValue instead of double
We use reflection to get the From(value, unit) method, but this method was recently changed to take a value of QuantityType instead of double, to use implicit cast to avoid method overload explosion for all number types. Reflection, however, does not do implicit cast so we update the deserialization code to pass in the proper type.
1 parent 9abbcba commit 5282b58

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,14 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
112112
!m.ReturnType.IsGenericType);
113113
#endif
114114

115+
// Implicit cast: we use this type to avoid explosion of method overloads to handle multiple number types
116+
QuantityValue quantityValue = vu.Value;
117+
115118
// Ex: Mass.From(55, MassUnit.Gram)
116119
// TODO: there is a possible loss of precision if base value requires higher precision than double can represent.
117120
// Example: Serializing Information.FromExabytes(100) then deserializing to Information
118121
// will likely return a very different result. Not sure how we can handle this?
119-
return fromMethod.Invoke(null, new[] {vu.Value, unit});
122+
return fromMethod.Invoke(null, new[] {quantityValue, unit});
120123
}
121124

122125
private static object TryDeserializeIComparable(JsonReader reader, JsonSerializer serializer)

0 commit comments

Comments
 (0)