Skip to content

Commit 725585e

Browse files
authored
Merge pull request #395 from angularsen/angularsen/json-use-projectref
json: Use project reference and fix deserialization In order to catch breaking changes in UnitsNet that was not observed because the nuget dependency was pointing to an older version of UnitsNet. With this change we always build against the latest (local checked out) UnitsNet project code. See #387 for issue on change in UnitsNet that broke JSON serialization due to the new QuantityValue type not supporting IConvertible. This PR addresses both building against the latest version of UnitsNet as well as fixes #387 by passing in QuantityValue instead of double to the reflected From(value, unit) method.
2 parents 61ec479 + 5282b58 commit 725585e

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.Signed.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
<AssemblyOriginatorKeyFile>$(MSBuildProjectDirectory)\..\UnitsNet.snk</AssemblyOriginatorKeyFile>
1414
</PropertyGroup>
1515

16+
<!-- Signed project references, will also generate the corresponding nuget dependencies -->
1617
<ItemGroup>
17-
<PackageReference Include="UnitsNet.Signed" Version="3.65.0-alpha3" />
18+
<ProjectReference Include="..\UnitsNet\UnitsNet.NetStandard10.Signed.csproj" />
1819
</ItemGroup>
1920
</Project>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="UnitsNet.Serialization.JsonNet.Common.props" />
33

4+
<!-- Unsigned project references, will also generate the corresponding nuget dependencies -->
45
<ItemGroup>
5-
<PackageReference Include="UnitsNet" Version="3.65.0-alpha3" />
6+
<ProjectReference Include="..\UnitsNet\UnitsNet.NetStandard10.csproj" />
67
</ItemGroup>
78
</Project>

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)