.NET 8 incluindo membros não públicos na serialização JSON
Aprenda a incluir propriedades private, protected e internal na serialização JSON no .NET 8 usando o atributo JsonInclude.
A partir do .NET 8, você pode incluir propriedades não públicas na serialização quando usa System.Text.Json. Para isso, basta decorar a propriedade não pública com o atributo JsonIncludeAttribute.
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public sealed class JsonIncludeAttribute : System.Text.Json.Serialization.JsonAttribute
O atributo funciona com qualquer modificador não público, como private, protected ou internal. Veja um exemplo:
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 esperado, isso produz a seguinte saída:
{"PrivateProperty":1,"ProtectedProperty":2,"InternalProperty":3}
Comments
Sign in with GitHub to comment. Reactions and replies thread back to the comments repo.