[Fix] If the local culture of the server was not english, numeric values greenfield were not properly interpreted

This commit is contained in:
nicolas.dorier
2021-10-26 00:46:09 +09:00
parent fd27bd94e2
commit e50c9266b4
2 changed files with 10 additions and 3 deletions

View File

@@ -22,13 +22,17 @@ namespace BTCPayServer.JsonConverters
switch (token.Type) switch (token.Type)
{ {
case JTokenType.Float: case JTokenType.Float:
if (objectType == typeof(decimal) || objectType == typeof(decimal?))
return token.Value<decimal>();
if (objectType == typeof(double) || objectType == typeof(double?))
return token.Value<double>();
throw new JsonSerializationException("Unexpected object type: " + objectType);
case JTokenType.Integer: case JTokenType.Integer:
case JTokenType.String: case JTokenType.String:
if (objectType == typeof(decimal) || objectType == typeof(decimal?) ) if (objectType == typeof(decimal) || objectType == typeof(decimal?) )
return decimal.Parse(token.ToString(), CultureInfo.InvariantCulture); return decimal.Parse(token.ToString(), CultureInfo.InvariantCulture);
if (objectType == typeof(double) || objectType == typeof(double?)) if (objectType == typeof(double) || objectType == typeof(double?))
return double.Parse(token.ToString(), CultureInfo.InvariantCulture); return double.Parse(token.ToString(), CultureInfo.InvariantCulture);
throw new JsonSerializationException("Unexpected object type: " + objectType); throw new JsonSerializationException("Unexpected object type: " + objectType);
case JTokenType.Null when objectType == typeof(decimal?) || objectType == typeof(double?): case JTokenType.Null when objectType == typeof(decimal?) || objectType == typeof(double?):
return null; return null;

View File

@@ -1979,10 +1979,13 @@ namespace BTCPayServer.Tests
} }
[Fact(Timeout = TestTimeout)] [Theory(Timeout = TestTimeout)]
[InlineData("DE-de")]
[InlineData("")]
[Trait("Fast", "Fast")] [Trait("Fast", "Fast")]
public void NumericJsonConverterTests() public void NumericJsonConverterTests(string culture)
{ {
System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(culture);
JsonReader Get(string val) JsonReader Get(string val)
{ {
return new JsonTextReader(new StringReader(val)); return new JsonTextReader(new StringReader(val));