diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int128Converter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int128Converter.cs index 177287966db715..5f2a01836dae87 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int128Converter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int128Converter.cs @@ -73,7 +73,7 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Int128 val { Span buffer = stackalloc byte[MaxFormatLength]; Format(buffer, value, out int written); - writer.WritePropertyName(buffer); + writer.WritePropertyName(buffer.Slice(0, written)); } internal override Int128 ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt128Converter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt128Converter.cs index b66e8e47bfba37..ea5b94e9ffcbed 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt128Converter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt128Converter.cs @@ -73,7 +73,7 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, UInt128 va { Span buffer = stackalloc byte[MaxFormatLength]; Format(buffer, value, out int written); - writer.WritePropertyName(buffer); + writer.WritePropertyName(buffer.Slice(0, written)); } internal override UInt128 ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options) diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.WriteTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.WriteTests.cs index a59e7dc9170334..f45410a76ea3a5 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.WriteTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.WriteTests.cs @@ -187,6 +187,44 @@ public static void TimeOnly_Write_Success(string value, string? expectedValue = string json = JsonSerializer.Serialize(ts); Assert.Equal($"\"{expectedValue ?? value}\"", json); } + + [Fact] + public static void Int128_AsDictionaryKey_SerializesCorrectly() + { + // Regression test for https://github.com/dotnet/runtime/issues/116855 + var dict = new Dictionary + { + [0] = "Zero", + [1] = "One", + [-1] = "MinusOne", + [Int128.MaxValue] = "Max", + [Int128.MinValue] = "Min" + }; + + string json = JsonSerializer.Serialize(dict); + Assert.Equal("""{"0":"Zero","1":"One","-1":"MinusOne","170141183460469231731687303715884105727":"Max","-170141183460469231731687303715884105728":"Min"}""", json); + + var deserialized = JsonSerializer.Deserialize>(json); + Assert.Equal(dict, deserialized); + } + + [Fact] + public static void UInt128_AsDictionaryKey_SerializesCorrectly() + { + // Regression test for https://github.com/dotnet/runtime/issues/116855 + var dict = new Dictionary + { + [0] = "Zero", + [1] = "One", + [UInt128.MaxValue] = "Max" + }; + + string json = JsonSerializer.Serialize(dict); + Assert.Equal("""{"0":"Zero","1":"One","340282366920938463463374607431768211455":"Max"}""", json); + + var deserialized = JsonSerializer.Deserialize>(json); + Assert.Equal(dict, deserialized); + } #endif } }