Skip to content

Commit

Permalink
Fix localization issue with floating-point deserialization from string (
Browse files Browse the repository at this point in the history
#7811) (#7813)

Co-authored-by: Florian Bernd <git@flobernd.de>
  • Loading branch information
github-actions[bot] and flobernd authored Jul 11, 2023
1 parent 51035b7 commit 4bbde6d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
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

0 comments on commit 4bbde6d

Please sign in to comment.