.NET 8 incluir miembros no públicos en la serialización JSON
Aprende a incluir propiedades private, protected e internal en la serialización JSON en .NET 8 usando el atributo JsonInclude.
A partir de .NET 8 puedes incluir propiedades no públicas en la serialización al usar System.Text.Json. Para ello, simplemente decora la propiedad no pública con el atributo JsonIncludeAttribute.
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class JsonIncludeAttribute : System.Text.Json.Serialization.JsonAttribute
El atributo funciona con cualquier modificador no público, como private, protected o internal. Veamos un ejemplo:
string json = JsonSerializer.Serialize(new MyClass(1, 2, 3));
Console.WriteLine(json);
public class MyClass
{
public MyClass(int privateProperty, int protectedProperty, int internalProperty)
{
PrivateProperty = privateProperty;
ProtectedProperty = protectedProperty;
InternalProperty = internalProperty;
}
[JsonInclude]
private int PrivateProperty { get; }
[JsonInclude]
protected int ProtectedProperty { get; }
[JsonInclude]
internal int InternalProperty { get; }
}
Como cabría esperar, esto producirá la siguiente salida:
{"PrivateProperty":1,"ProtectedProperty":2,"InternalProperty":3}
Comments
Sign in with GitHub to comment. Reactions and replies thread back to the comments repo.