Protobuf.System.Text.Json icon indicating copy to clipboard operation
Protobuf.System.Text.Json copied to clipboard

Implement full JsonNumberHandling support

Open Copilot opened this issue 4 months ago • 2 comments

Previously only AllowReadingFromString was partially supported. This adds complete support for all JsonNumberHandling flags per the Microsoft documentation.

Changes

FieldConverter.cs

  • Added WriteAsString support: numbers serialize as JSON strings when flag is set
  • Added AllowNamedFloatingPointLiterals support: handles "NaN", "Infinity", "-Infinity" for float/double types
  • Added validation: throws JsonException when named literals are used without the flag

JsonNumberHandlingTests.cs

  • Added 6 tests covering all flag combinations and error cases

Example Usage

// Serialize numbers as strings
var options = new JsonSerializerOptions();
options.NumberHandling = JsonNumberHandling.WriteAsString;
// Output: {"value": "123.45"}

// Read named floating-point literals
options.NumberHandling = JsonNumberHandling.AllowReadingFromString | 
                         JsonNumberHandling.AllowNamedFloatingPointLiterals;
// Accepts: {"value": "NaN"}, {"value": "Infinity"}

// Flags work in combination
options.NumberHandling = JsonNumberHandling.WriteAsString | 
                         JsonNumberHandling.AllowReadingFromString;
// Full round-trip support for numbers as strings

All numeric types (int32, int64, float, double, etc.) are supported. Named literals only apply to float/double as per spec.

Original prompt

This section details on the original issue you should resolve

<issue_title>Use NumberHandling</issue_title> <issue_description>https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonnumberhandling </issue_description>

<agent_instructions>Implement properly JsonNumberHandling. It was already implemented for AllowReadingFromString but we need full support. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

  • Fixes Havret/Protobuf.System.Text.Json#65

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Nov 09 '25 23:11 Copilot