This is an automated email from the ASF dual-hosted git repository.
CurtHagenlocher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git
The following commit(s) were added to refs/heads/main by this push:
new 52f7928d7e fix(csharp): serialize NaN and infinity in struct JSON
(#4921)
52f7928d7e is described below
commit 52f7928d7eac545ff547e91f777090a64a894c7f
Author: Curt Hagenlocher <[email protected]>
AuthorDate: Sun Oct 18 10:56:34 2026 -0700
fix(csharp): serialize NaN and infinity in struct JSON (#4921)
## What's changed
When a struct is serialized to JSON (the default
`StructBehavior.JsonString`), float and double fields are written with
`Utf8JsonWriter.WriteNumberValue`. That throws `ArgumentException` for
NaN and ±Infinity, so a single non-finite value made its whole row
unreadable.
These values are now written as the strings `"NaN"`, `"Infinity"` and
`"-Infinity"`. That's how System.Text.Json writes them when
`JsonNumberHandling.AllowNamedFloatingPointLiterals` is set.
`StructBehavior.Strict` is unchanged and still returns the actual
`float`/`double` values.
This came up in review of #4919, where variants serialized to JSON use
the same writer.
### Tests
`NonFiniteFloat` and `NonFiniteDouble` check the JSON for NaN, Infinity
and -Infinity. `NonFiniteDouble` also checks that the struct result
keeps the actual NaN value. Without the fix, both throw the
`ArgumentException`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../Extensions/IArrowArrayExtensions.cs | 20 ++++++++++++++++--
.../SerializeStructToJsonTests.cs | 24 ++++++++++++++++++++++
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/csharp/src/Apache.Arrow.Adbc/Extensions/IArrowArrayExtensions.cs
b/csharp/src/Apache.Arrow.Adbc/Extensions/IArrowArrayExtensions.cs
index acdf80e75f..a9d298af24 100644
--- a/csharp/src/Apache.Arrow.Adbc/Extensions/IArrowArrayExtensions.cs
+++ b/csharp/src/Apache.Arrow.Adbc/Extensions/IArrowArrayExtensions.cs
@@ -363,10 +363,16 @@ namespace Apache.Arrow.Adbc.Extensions
writer.WriteNumberValue(us);
return;
case float f:
- writer.WriteNumberValue(f);
+ if (float.IsNaN(f) || float.IsInfinity(f))
+ WriteNonFiniteValue(writer, f);
+ else
+ writer.WriteNumberValue(f);
return;
case double d:
- writer.WriteNumberValue(d);
+ if (double.IsNaN(d) || double.IsInfinity(d))
+ WriteNonFiniteValue(writer, d);
+ else
+ writer.WriteNumberValue(d);
return;
case decimal dec:
writer.WriteNumberValue(dec);
@@ -434,6 +440,16 @@ namespace Apache.Arrow.Adbc.Extensions
}
}
+ /// <summary>
+ /// JSON numbers can't represent NaN or infinity, so these are written
as the strings
+ /// "NaN", "Infinity" and "-Infinity", which is how System.Text.Json
writes them when
+ /// <see
cref="System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals"/>
is set.
+ /// </summary>
+ private static void WriteNonFiniteValue(Utf8JsonWriter writer, double
value)
+ {
+ writer.WriteStringValue(double.IsNaN(value) ? "NaN" : value > 0 ?
"Infinity" : "-Infinity");
+ }
+
/// <summary>
/// Converts an item in the StructArray at the index position to a
Dictionary<string, object?>.
/// </summary>
diff --git a/csharp/test/Apache.Arrow.Adbc.Tests/SerializeStructToJsonTests.cs
b/csharp/test/Apache.Arrow.Adbc.Tests/SerializeStructToJsonTests.cs
index d5ff7d0bca..78394c593c 100644
--- a/csharp/test/Apache.Arrow.Adbc.Tests/SerializeStructToJsonTests.cs
+++ b/csharp/test/Apache.Arrow.Adbc.Tests/SerializeStructToJsonTests.cs
@@ -79,6 +79,30 @@ namespace Apache.Arrow.Adbc.Tests
[Fact]
public void Double() => AssertJson("{\"f\":3.25}", DoubleType.Default,
new DoubleArray.Builder().Append(3.25).Build());
+ [Fact]
+ public void NonFiniteFloat()
+ {
+ var values = new
FloatArray.Builder().Append(float.NaN).Append(float.PositiveInfinity).Append(float.NegativeInfinity).Build();
+ var s = SingleFieldStruct("f", FloatType.Default, values);
+ Assert.Equal("{\"f\":\"NaN\"}", SerializeRow(s, 0));
+ Assert.Equal("{\"f\":\"Infinity\"}", SerializeRow(s, 1));
+ Assert.Equal("{\"f\":\"-Infinity\"}", SerializeRow(s, 2));
+ }
+
+ [Fact]
+ public void NonFiniteDouble()
+ {
+ var values = new
DoubleArray.Builder().Append(double.NaN).Append(double.PositiveInfinity).Append(double.NegativeInfinity).Build();
+ var s = SingleFieldStruct("f", DoubleType.Default, values);
+ Assert.Equal("{\"f\":\"NaN\"}", SerializeRow(s, 0));
+ Assert.Equal("{\"f\":\"Infinity\"}", SerializeRow(s, 1));
+ Assert.Equal("{\"f\":\"-Infinity\"}", SerializeRow(s, 2));
+
+ // The struct result keeps the actual values
+ var obj = (Dictionary<string, object?>)s.ValueAt(0,
StructResultType.Object)!;
+ Assert.True(double.IsNaN((double)obj["f"]!));
+ }
+
[Fact]
public void String() => AssertJson("{\"f\":\"hi\"}",
StringType.Default, new StringArray.Builder().Append("hi").Build());