Skip to content

Commit

Permalink
Fix deserializing decimal quantities regardless of local culture (#911)
Browse files Browse the repository at this point in the history
Fixes parsing JSON for decimal quantities like Power on machines with cultures like Norwegian, where a comma is decimal separator is used.

Related to #847, #868
  • Loading branch information
angularsen authored Apr 4, 2021
1 parent 6bd8b26 commit fbd89e7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private class TestConverter : UnitsNetBaseJsonConverter<string>
var result = ConvertIQuantity(value);
if (result is ExtendedValueUnit {ValueType: "decimal"} decimalResult)
{
return (result.Unit, decimal.Parse(decimalResult.ValueString));
return (result.Unit, decimal.Parse(decimalResult.ValueString, CultureInfo.InvariantCulture));
}

throw new ArgumentException("The quantity does not have a decimal value", nameof(value));
Expand Down Expand Up @@ -274,7 +274,7 @@ public IQuantity Test_ConvertDecimalValueUnit(string unit, decimal value) => Tes

if (result is ExtendedValueUnit {ValueType: "decimal"} decimalResult)
{
return (result.Unit, decimal.Parse(decimalResult.ValueString));
return (result.Unit, decimal.Parse(decimalResult.ValueString, CultureInfo.InvariantCulture));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected IQuantity ConvertValueUnit(ValueUnit valueUnit)

return valueUnit switch
{
ExtendedValueUnit {ValueType: "decimal"} extendedValueUnit => Quantity.From(decimal.Parse(extendedValueUnit.ValueString), unit),
ExtendedValueUnit {ValueType: "decimal"} extendedValueUnit => Quantity.From(decimal.Parse(extendedValueUnit.ValueString, CultureInfo.InvariantCulture), unit),
_ => Quantity.From(valueUnit.Value, unit)
};
}
Expand Down

0 comments on commit fbd89e7

Please sign in to comment.