Implement full JsonNumberHandling support
Previously only AllowReadingFromString was partially supported. This adds complete support for all JsonNumberHandling flags per the Microsoft documentation.
Changes
FieldConverter.cs
- Added
WriteAsStringsupport: numbers serialize as JSON strings when flag is set - Added
AllowNamedFloatingPointLiteralssupport: handles "NaN", "Infinity", "-Infinity" for float/double types - Added validation: throws
JsonExceptionwhen 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.