Skip to content

Commit b371a67

Browse files
authored
Merge pull request #2442 from bjornhellander/Issue2440_LightJsonCulture
Update embedded LightJson to be culture agnostic
2 parents 04deed8 + 85126d0 commit b371a67

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LightJson/JsonValueTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ namespace StyleCop.Analyzers.Test.LightJson
66
using System;
77
using global::LightJson;
88
using global::LightJson.Serialization;
9+
using StyleCop.Analyzers.Test.Helpers;
910
using Xunit;
1011

12+
[UseCulture("en-US")]
1113
public class JsonValueTests
1214
{
1315
[Fact]
@@ -106,18 +108,35 @@ public void TestAsNumber()
106108
Assert.Equal(0.0, new JsonValue(new JsonArray()).AsNumber);
107109
}
108110

111+
[Fact]
112+
[WorkItem(2440, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2440")]
113+
[UseCulture("sv-SE")]
114+
public void TestAsNumbersUsingCultureWithNonStandardNumberFormat()
115+
{
116+
Assert.Equal(1.0, new JsonValue("1.0").AsNumber);
117+
}
118+
109119
[Fact]
110120
public void TestAsString()
111121
{
112122
Assert.Equal("false", new JsonValue(false).AsString);
113123
Assert.Equal("true", new JsonValue(true).AsString);
124+
Assert.Equal("0.5", new JsonValue(0.5).AsString);
114125
Assert.Equal("1", new JsonValue(1.0).AsString);
115126
Assert.Equal("text", new JsonValue("text").AsString);
116127
Assert.Null(new JsonValue(new JsonObject()).AsString);
117128
Assert.Null(new JsonValue(default(JsonObject)).AsString);
118129
Assert.Null(new JsonValue(new JsonArray()).AsString);
119130
}
120131

132+
[Fact]
133+
[WorkItem(2440, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2440")]
134+
[UseCulture("sv-SE")]
135+
public void TestAsStringUsingCultureWithNonStandardNumberFormat()
136+
{
137+
Assert.Equal("0.5", new JsonValue(0.5).AsString);
138+
}
139+
121140
[Fact]
122141
public void TestAsJsonObject()
123142
{

StyleCop.Analyzers/StyleCop.Analyzers/LightJson/JsonValue.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace LightJson
77
using System.Collections.Generic;
88
using System.Diagnostics;
99
using System.Diagnostics.CodeAnalysis;
10+
using System.Globalization;
1011
using LightJson.Serialization;
1112

1213
/// <summary>
@@ -332,7 +333,7 @@ public double AsNumber
332333

333334
case JsonValueType.String:
334335
double number;
335-
if (double.TryParse((string)this.reference, out number))
336+
if (double.TryParse((string)this.reference, NumberStyles.Float, CultureInfo.InvariantCulture, out number))
336337
{
337338
return number;
338339
}
@@ -363,7 +364,7 @@ public string AsString
363364
: "false";
364365

365366
case JsonValueType.Number:
366-
return this.value.ToString();
367+
return this.value.ToString(CultureInfo.InvariantCulture);
367368

368369
case JsonValueType.String:
369370
return (string)this.reference;

0 commit comments

Comments
 (0)