Skip to content

Commit 4f89f1e

Browse files
committed
Ensure jsonconvertors use invariant culture
1 parent f0842a1 commit 4f89f1e

File tree

8 files changed

+20
-8
lines changed

8 files changed

+20
-8
lines changed

src/Synercoding.Primitives/JsonConverters/PointJsonConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Text.Json;
34
using System.Text.Json.Serialization;
45

@@ -72,6 +73,6 @@ public override Point Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
7273
/// <param name="options">An object that specifies serialization options to use.</param>
7374
public override void Write(Utf8JsonWriter writer, Point value, JsonSerializerOptions options)
7475
{
75-
writer.WriteStringValue(value.ToString());
76+
writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
7677
}
7778
}

src/Synercoding.Primitives/JsonConverters/RectangleJsonConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Text.Json;
34
using System.Text.Json.Serialization;
45

@@ -80,6 +81,6 @@ public override Rectangle Read(ref Utf8JsonReader reader, Type typeToConvert, Js
8081
/// <param name="options">An object that specifies serialization options to use.</param>
8182
public override void Write(Utf8JsonWriter writer, Rectangle value, JsonSerializerOptions options)
8283
{
83-
writer.WriteStringValue(value.ToString());
84+
writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
8485
}
8586
}

src/Synercoding.Primitives/JsonConverters/SizeJsonConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Text.Json;
34
using System.Text.Json.Serialization;
45

@@ -72,6 +73,6 @@ public override Size Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer
7273
/// <param name="options">An object that specifies serialization options to use.</param>
7374
public override void Write(Utf8JsonWriter writer, Size value, JsonSerializerOptions options)
7475
{
75-
writer.WriteStringValue(value.ToString());
76+
writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
7677
}
7778
}

src/Synercoding.Primitives/JsonConverters/SpacingJsonConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Text.Json;
34
using System.Text.Json.Serialization;
45

@@ -80,6 +81,6 @@ public override Spacing Read(ref Utf8JsonReader reader, Type typeToConvert, Json
8081
/// <param name="options">An object that specifies serialization options to use.</param>
8182
public override void Write(Utf8JsonWriter writer, Spacing value, JsonSerializerOptions options)
8283
{
83-
writer.WriteStringValue(value.ToString());
84+
writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
8485
}
8586
}

src/Synercoding.Primitives/JsonConverters/UnitJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public override Unit Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer
4444
/// <param name="options">An object that specifies serialization options to use.</param>
4545
public override void Write(Utf8JsonWriter writer, Unit value, JsonSerializerOptions options)
4646
{
47-
writer.WriteStringValue(value.ToString());
47+
writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
4848
}
4949
}
5050

src/Synercoding.Primitives/JsonConverters/ValueJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public override Value Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
7373
/// <param name="options">An object that specifies serialization options to use.</param>
7474
public override void Write(Utf8JsonWriter writer, Value value, JsonSerializerOptions options)
7575
{
76-
writer.WriteStringValue(value.ToString());
76+
writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
7777
}
7878
}

src/Synercoding.Primitives/PackageDetails.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Product>Synercoding.Primitives</Product>
1111
<Title>Synercoding.Primitives</Title>
1212
<Description>Primitives with units attached (think mm, cm, in, px) that can be used for 2D graphics.</Description>
13-
<PackageReleaseNotes>Limit to latest .NET version (8) and add System.Json serialization support.</PackageReleaseNotes>
13+
<PackageReleaseNotes>Added IParsable support and added overload for ToString to be culture aware.</PackageReleaseNotes>
1414
</PropertyGroup>
1515

1616
</Project>

src/Synercoding.Primitives/Unit.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ public bool Equals(Unit other)
4444

4545
/// <inheritdoc/>
4646
public override string ToString()
47+
=> ToString(null);
48+
49+
/// <summary>
50+
/// Returns a string representation of the unit using the specified format provider.
51+
/// </summary>
52+
/// <param name="formatProvider">The format provider to use for formatting numeric values in pixel units (DPI). Ignored for other unit types.</param>
53+
/// <returns>A string representation of the unit. For pixel units, returns "dpi(value)" format. For other units, returns their short form (e.g., "mm", "cm", "in", "pts").</returns>
54+
public string ToString(IFormatProvider? formatProvider)
4755
=> Designation switch
4856
{
49-
UnitDesignation.Pixels => $"dpi({PerInch})",
57+
UnitDesignation.Pixels => $"dpi({PerInch.ToString(formatProvider)})",
5058
var x => x.Shortform()
5159
};
5260

0 commit comments

Comments
 (0)