System.Text.Json
- Removing the property info?
#103941
Replies: 1 comment
-
I recommend using a resolver modifier function/method (https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/custom-contracts) and let this function configure the (
Doing this is fine under the following restrictions:
Side note: Altering JsonTypeInfo's outside of a resolver or modifier method can work in certain circumstances, but it all depends on how your code is handling the JsonTypeInfo instances you are altering, thus making it more of a headache and unreliable compared to the ease-of-use of a modifier method i mentioned above. The reflection-based DefaultJsonTypeInfoResolver does not cache modified JsonTypeInfo instances. So, if you pull a JsonTypeInfo instance directly from this resolver, alter it but then don't use the JsonTypeInfo explicitly by passing it to the JsonSerializer.(De)Serialize methods, the altered JsonTypeInfo won't get used by the serializer. When STJ source gen is used, JsonTypeInfo instanced obtained directly from the JsonSerializerContext's GetTypeInfo method as well as the JsonTypeInfo's accessible through the generated JsonSerializerContext are locked as read-only and cannot be altered. (Attempting to alter them will lead to an exception.) If you pull a JsonTypeInfo from a JsonSerializerOptions instance using the JsonSerializerOptions.GetTypeInfo method, keep in mind that JsonSerializerOptions caches JsonTypeInfo instances but only after JsonSerializerOptions has been made read-only, which in turn would prevent modification of JsonTypeInfo instances obtained from it. So, in essence, you would either succeed in modifying an non-cached JsonTypeInfo obtained from JsonSerializerOptions while requiring you to explicitly pass this JsonTypeInfo to the JsonSerializer.(De)Serialize methods; or attempting to modify the JsonTypeInfo would fail with an exception when the JsonTypeInfo has been locked as read-only by the serializer or by explicitly calling JsonSerializerOptions.MakeReadOnly(). And even if you are able to successfully remove a JsonPropertyInfo, if that JsonPropertyInfo is mapped to a parameter of a parameterized constructor for a type involved in deserialization, the deserialization will fail with an exception when the deserializer wants to use this parameterized constructor and a JsonPropertyInfo for a constructor parameter is missing in the JsonTypeInfo.Properties list for that type. |
Beta Was this translation helpful? Give feedback.
-
Hi team,
I'm working with STJ, and I have to support a custom property in {de}serialization, let's call it
[MyJson(string propertyName)]
.If a property isn't annotated with
MyJson
, then it gets excluded from both serialization and deserialization (equivalent property name should not bind).Is the correct approach here to call
jsonTypeInfo.Properties[x].Remove(y)
? Are there any potential adverse affects to this, or does STJ tolerate properties just being removed?If not, what can I do to avoid deserialization binding?
type.ShouldSerialize = (_, _) => false;
doesn't handle that for me.Beta Was this translation helpful? Give feedback.
All reactions