JsonNumberHandling.AllowReadingFromString should support read empty string to null/default

Open
#117,404 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
api, backend

Research direction

Start with the provided System.Text.Json.JsonSerializer.Deserialize<decimal?> reproduction and the JsonNumberHandling.AllowReadingFromString option. Trace the number-conversion behavior for nullable decimal values, then verify that an empty JSON string becomes null while quoted numeric strings and existing handling continue to work.

Written by the indexing model from the issue text.

Description

area-System.Text.Json
Description

currently , S.T.J allow read "123" to number type like Nullable<int>/Nullable<decimal> , but it doesn't allow "" (eg. empty string) to null
Image

since the number is using string format, so it might be null | "" when there is no number , so null or "" both should be deserialize as null
for example : a html input bind a value of string , when user type a number and delete

Reproduction Steps
var op = new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web)
  {                                                                                                                           NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString | System.Text.Json.Seria
  lization.JsonNumberHandling.AllowNamedFloatingPointLiterals
  };
 System.Text.Json.JsonSerializer.Deserialize<decimal?>("\"\"", op)
Expected behavior

"" => null (decimal?)

Actual behavior

thow exception : The JSON value could not be converted to System.Nullable`1[System.Decimal]

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.