-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[release/5.0-rc2] Add JsonNumberHandling.AllowReadingFromString as a JsonSerializer web default (#41539) #42240
Conversation
… default (dotnet#41539) * Add JsonNumberHandling.AllowReadingFromString as a JsonSerializer web default * Add test assertion for number handling option in S.N.H.Json * Test number-as-string behavior in System.Net.Http.Json
Thanks for the detailed write up. This change seems like a feature. It will be hard to get the okay for it without strong customer motivation. Can you share more about the customers? How many or how large? How impactful if it to them if we don’t take this change eg they can’t migrate? |
Hit close by mistake |
This is changing the default settings of |
Thanks @ahsonkhan . That's helpful but still we would need strong customer data to support the change. |
@danmosemsft The number handling feature is the most requested feature in System.Text.Json (both closed and open) with 91 likes. The next highest, the reference handling feature, has 77 likes. The major motivation of the feature was to support interchanging data with various web services including JavaScript APIs, Blazor, Python etc. The primary capability desired was being able to read numbers from JSON strings produced by these entities ( The setting of FWIW during API review for the number handling feature, there was even discussion about making this a general default on JsonSerializerOptions. It seemed a bit too general for low level serializer usages, so we settled on making it a web default where it is certainly more applicable.
Just to clarify that this behavior is making a scenario more permissive, specifically going from throwing BTW, the breaking change angle here would be from the perspective of changing the behavior of a named default (
App authors can revert to strict settings by setting That said, I believe it is still worth it to make the change now to avoid a potential inconvenience to some users in 6.0. Specifically, this will be users that directly use |
Removing label while discussion continues. We can take Thurs if needed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM; direct port except for minor doc change that will be added to master.
My 2 cents: was also surprised by this when updating to use System.Text.Json. Number serialization in json has a few limitations, specially for anything outside the float range/precision, thus quite often one has to fall back to use strings instead |
Backport of #41539 to release/5.0-rc2
Customer Impact
Follow up from #39363 / #30255.
During API review for the number handling feature, it was discussed that number types being written as strings is a common pattern in many JSON producers (e.g API endpoints) across the web. Adding
JsonNumberHandling.AllowReadingFromString
as a JsonSerializer web default makes it easier for .NET web applications to consume these numbers in a consistent manner (same options for client/shared/server).Testing
HttpClient
andJsonContent
were updated to use theJsonSerializerDefaults.Web
setting. Appropriate tests were added to make sure the options are honored accordingly.Risk
The
JsonSerializerDefaults
and number handling features are new in .NET 5.0. .NET web products such as ASP.NET MVC,HttpRequestExtensions
, and the extension methods onHttpClient
/JsonContent
have been updated to useJsonSerializerDefaults
by default. The change in this PR affects these areas and makes reading numbers from JSON strings the default behavior.