Skip to content

[Backport 8.3] Fix localization issue with floating-point deserialization from string #7815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions src/Elastic.Clients.Elasticsearch/Core/Fields/FieldValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Globalization;
using Elastic.Clients.Elasticsearch.Serialization;

namespace Elastic.Clients.Elasticsearch;
Expand Down Expand Up @@ -246,37 +247,18 @@ internal bool TryGetComposite([NotNullWhen(returnValue: true)] out object? value
return true;
}

public override string ToString()
{
if (Kind == ValueKind.String)
{
return Value as string;
}
else if (Kind == ValueKind.Double)
{
return ((double)Value).ToString();
}
else if (Kind == ValueKind.Long)
{
return ((long)Value).ToString();
}
else if (Kind == ValueKind.Boolean)
{
return ((bool)Value).ToString();
}
else if (Kind == ValueKind.Null)
{
return "null";
}
else if (Kind == ValueKind.LazyDocument || Kind == ValueKind.Composite)
{
throw new InvalidOperationException("Composite field value cannot be formatted as a string.");
}
else
public override string ToString() =>
Kind switch
{
throw new InvalidOperationException($"Unknown kind '{Kind}'");
}
}
ValueKind.Null => "null",
ValueKind.Double => ((double)Value).ToString(CultureInfo.InvariantCulture),
ValueKind.Long => ((long)Value).ToString(CultureInfo.InvariantCulture),
ValueKind.Boolean => ((bool)Value).ToString(CultureInfo.InvariantCulture),
ValueKind.String => Value as string,
ValueKind.LazyDocument or ValueKind.Composite => throw new InvalidOperationException(
"Composite field value cannot be formatted as a string."),
_ => throw new InvalidOperationException($"Unknown kind '{Kind}'")
};

public override bool Equals(object? obj) => obj is FieldValue value && Equals(value);
public bool Equals(FieldValue other) => Kind == other.Kind && EqualityComparer<object?>.Default.Equals(Value, other.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
{
var value = reader.GetString();

if (!double.TryParse(value, out var parsedValue))
if (!double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedValue))
ThrowHelper.ThrowJsonException($"Unable to parse '{value}' as a double.");

return parsedValue;
Expand Down Expand Up @@ -67,7 +67,7 @@ public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOp
var utf16Text = value.ToString("G17", CultureInfo.InvariantCulture);

if (utf16Text.Length < utf8bytes.Length)
{
{
try
{
var bytes = Encoding.UTF8.GetBytes(utf16Text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
{
var value = reader.GetString();

if (!float.TryParse(value, out var parsedValue))
if (!float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedValue))
ThrowHelper.ThrowJsonException($"Unable to parse '{value}' as a float.");

return parsedValue;
Expand Down Expand Up @@ -67,7 +67,7 @@ public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOpt
var utf16Text = value.ToString("G9", CultureInfo.InvariantCulture);

if (utf16Text.Length < utf8bytes.Length)
{
{
try
{
var bytes = Encoding.UTF8.GetBytes(utf16Text);
Expand Down