-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Equivalent of DefaultContractResolver in System.Text.Json #31257
Comments
@xsoheilalizadeh if you find any workaround then please do share. |
@KamranShahid, I'm trying to ignore empty collection with // { "numbers":[1, 2] }
var document = await JsonSerializer.DeserializeAsync<JsonElement>(body);
var numbers = document.GetProperty("numbers"); // [1, 2]
if (numbers.GetArrayLength() == 0)
{
using var memoryStream = new MemoryStream();
var utf8Json = new Utf8JsonWriter(memoryStream);
utf8Json.WriteNull("orders");
// or
utf8Json.WriteNullValue();
// or ...
numbers.WriteTo(utf8Json);
} |
For the time being what i have done is created another smaller class and assigned only required fields there. Let's see if there can be something added in .net core 3.1 |
/cc @steveharter |
Currently there is no mechanism for a per-property callback to control (de)serialization based on custom logic. We are considering adding a more flexible model in 5.0 however. If you just want to ignore null properties, you can use JsonSerializerOptions.IgnoreNullValues. To ignore empty collections, or to add other logic, you would need to author a custom converter by authoring a class that derives from One the custom converter class is authored, it can be registered in the following ways:
|
From @KamranShahid in https://github.com/dotnet/corefx/issues/42043:
|
@xsoheilalizadeh @layomia Any updates on this ticket or a solution to be able to replicate complex logic implemented in contract resolvers in System.Text.Json? |
I'm not sure how this could be achieved in a generic way with converters since they're only called after the name of their property has been written. No-opping inside one will simply leave you with the invalid JSON From being unable to deserialize Is there any chance that we'll see features like this made accessible in previews before November? I'd love to use these APIs in projects but keep running into roadblocks that look less like performance considerations and more like missing features. |
I really need to equivalent of DefaultContractResolver in System.Text.Json. I am getting stuck at multiple places due to this. It's not possible to treat all null strings as empty in using converter. Similarly any custom advance logic is not possible in converters. |
Any new traction/workarounds on this issue ? |
For 5.0 we are likely going to add a virtual property "HandleNullValue" (pending naming). Then you will be able to write a System.String converter, override that property and return true, and then implement Read\Write to treat nulls as empty strings. |
So you're essentially saying stj will not be supporting a way to ignore a value at runtime? e.g. a An easy use-case to demonstrate this being necessary would be an |
I was honestly very excited to see a standardized version of JSON serialization finally making its way into .NET. Wrote a bunch of code, started testing, and then I came across this issue. I MINIMALLY need an EASY option for preventing serialization of properties with any of the following characteristics: null value, default value, empty string (zero length), and empty collection (zero count). While more would be better, the above would cover the majority of code I've ever written. Without this minimal support, this package is simply unusable. I'm forced to return to using XML serialization or Newtonsoft's JSON.NET. Why not support the "ShouldSerializeXXX" convention? It is supported by pretty much every other serializer out there. Is its absence simply to differentiate this product? Or, are you concerned about performance issues? If its the latter, perhaps provide a parameter/option to the serializer that selectively enables/disables "ShouldSerializeXXX" support? I suggest this anyway to lessen the burden on developers refactoring pre-existing code (to use this package). I'm really bummed out now :( I was so happy to see this package. Now, I need to eliminate dependencies on this package and revert to a different solution. |
I have different use case for |
I again needed similar thing for converting one implementation as mentioned in my stackoverflow post Basically trying equivalent of
Any help in it? |
@KamranShahid same is done here: https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Generation/SystemTextJsonUtilities.cs#L45 But not sure whether this is a “sustainable” solution. |
I am looking for solution within System.Text.Json. Not wanted to have NewtonSoft dependency anymore |
Please support a way to handle NHibernate Proxy objects serialization: public class NHibernateContractResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType))
{
return base.CreateContract(objectType.BaseType);
}
else
{
return base.CreateContract(objectType);
}
}
} |
As is, this use-case can lead to very many instances of a I appreciate that a |
Closing this; it is referenced by #36785. Note two features (serialization order and OnSerialize callbacks) were added that will be able to unblock some "DefaultContractResolver" scenarios. For 6.0, there was prototyping that exposed the object and property metadata with callbacks that allowed editing such as adding or modifying properties. However, since the prototyping was done later in the release, it would have received little feedback and usage information so the effort was closed and focus was switched to implement property ordering + OnSerialize callbacks. |
@steveharter In order to implement correct schema definition generation, we probably need "prototyping" so that all metadata is available... do you have a link to the issue for this feature? |
I used to
DefaultContractResolver
in Json.NET for ignoring empty collections and sometimes for changing json values. Currently I switched to System.Text.Json and haven't any idea about how to implement equivalent process in new JSON APIs. Also triedJsonConverter
but it doesn't act asDefaultContractResolver
.Ignoring empty collections
The text was updated successfully, but these errors were encountered: