-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Honor custom number handling only when property/type is a number/collection of numbers #41679
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
Conversation
…ection of numbers
|
This is a breaking change because applying public class ClassWith_AttributeOnComplexListProperty
{
// Attribute not allowed here.
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<ClassWith_StrictAttribute> MyList { get; set; }
// Attribute allowed here and will be honored.
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<int> MyOtherList { get; set; }
}Motivation Even though it's a behavorial no-op to put the attribute on a collection of non-numbers, the serializer would still go down a slower code-path if we allowed the attribute. Generally in the serializer, we throw when a setting is indicated but not honored. A somewhat related example is with preserve-reference handling when object is deserialized with a parameterized ctor. Rather than do a no-op (i.e deserialize as usual without the objects pointing to each other), we throw up front so that the user fully understands how the setting works. The attribute on a POCO-typed property is truly a no-op and will not influence how number properties are (de)serialized. The serializer doesn't pass options down in that manner for any scenario, but it should be achievable for number handling if desired by many users. This is another reason to throw explicitly now rather than let this be a no-op and then change the behavior in the future. The ways to influence number handling of a number property today:
Making this change allows us to have clean logic in the serializer today which matches with supported behavior, and also maintain the best perf with this feature on: the |
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs
Outdated
Show resolved
Hide resolved
|
Breaking change doc: dotnet/docs#26771. |
Don't take slightly slower code-paths that honor custom number handling when the property/type is not a number or collection of numbers. Cost for the number handling feature is now only paid per number property.
Brings (de)serialization performance of hypothetical object graphs that don't contain numbers when
JsonSerializerOptions.NumberHandling != JsonNumberHandling.Strictto par with when the option is the default value (JsonNumberHandling.Strict).This change is especially useful now that
JsonNumberHandling.AllowReadingFromStringis being set as aJsonSerializerweb default.Base = before this change,
JsonSerializerOptions.NumberHandling == AllowReadingFromString | WriteAsStringDiff = after this change,
JsonSerializerOptions.NumberHandling == AllowReadingFromString | WriteAsStringBenchmark classes contain little or no numbers.