0

There is a book class with public and private properties:

public class Book
{
    private int ID { get; set; } = -1;
    private string? BaseName { get; set; } = null;

    public string? Title { get; private set; } = null;
    public string? Author { get; set; } = null;
}

I need the ability to serialize both including private properties and without them.

JsonSerializerOptions does not support this option.

Other serializers are model-bound: above each model property, I need to specify whether the property should be serialized but I need to manage this after the program has run.

The only solution I see is converting one model with private properties to another that makes these properties public, but this way I create a lot of duplicate classes.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Angry Fox
  • 53
  • 1
  • 7
  • How do you determine at runtime when to include or not those properties? Also, can you use .NET 7? – CodeCaster May 19 '23 at 08:15
  • I need to get json for the client (without private properties) and in order to be able to save the data to the server (with private properties). project on .NET 6 – Angry Fox May 19 '23 at 08:20
  • 1
    That answered neither of my questions. So you save the model's JSON serverside _and_ you want to expose the same model's serialized representation to the client? _How_? You can pass different JsonSerializerOptions to ASP.NET Core and to your own serializer. As for .NET 7, you can write [custom modifiers](https://stackoverflow.com/questions/76273639/using-system-text-json-to-ignore-default-values-for-bool-data-type-only/76273891#76273891), but that won't seem to help in that scenario. Anyway the most maintainable solution would still be to have different model classes, one for each scenario. – CodeCaster May 19 '23 at 08:22
  • Yes, I want to operate on one model, but in one case, I want to hide private properties. I know about the ability to pass different JsonSerializerOptions when serializing, but the JsonSerializer cannot display hidden model properties – Angry Fox May 19 '23 at 08:30
  • 3
    Well you [can](https://stackoverflow.com/questions/61869393/get-net-core-jsonserializer-to-serialize-private-members) but you shouldn't want to. – CodeCaster May 19 '23 at 08:34

1 Answers1

2

you don't need to serialize your private property (there is way to do that but not likely to use), instead you only need a DTO to hide your data from your client

public class BookDto
{
    public string? Title { get; set; } = null;
    public string? Author { get; set; } = null;
}
public class Book
{
    public int ID { get; set; } = -1;
    public string? BaseName { get; set; } = null;

    public string? Title { get; private set; } = null;
    public string? Author { get; set; } = null;
}
Ibram Reda
  • 1,882
  • 2
  • 7
  • 18
  • I think that only this option is suitable, but the problem is that now there are about 10 models and for each model it will be necessary to create an DTO. – Angry Fox May 19 '23 at 08:33
  • 1
    Actually, you should transfer all your object as DTO to have more control on it, you can use [Automapper](https://automapper.org/) to facilitate your work – Ibram Reda May 19 '23 at 08:37
  • 1
    This [tutorial](https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5) can help you to get the idea of DTO – Ibram Reda May 19 '23 at 08:38