Serialize a subset of a class

I will not go into everything that I tried ...

This is roughly what I'm trying to accomplish.

public interface IClientDTO {
    string FirstName { get; }
    string LastName { get; }
}

public class DTO : IClientDTO {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SSN { get; set; }
}

class Program {
    static void Main(string[] args) {
        DTO dto = new DTO() { FirstName = "John", LastName = "Doe", SSN = "111001111" };
        IClientDTO clientDTO = dto;
        string sDTO = JsonConvert.SerializeObject(dto);
        string sClientDTO = JsonConvert.SerializeObject(clientDTO);

        Debug.WriteLine(sDTO);
        Debug.WriteLine(sClientDTO);
    }
}

I want my conclusion to look like this ...

{"PLA": "111001111", "FirstName": "John", "LastName": "Doe"}

{"FirstName": "John", "LastName": "Doe"}

I did not think it would be difficult, but since the Serializer always defines the type as DTO, I always get the SSN value even in cases where I do not want to.

This, of course, is just a small test. Suppose I have a WebAPI application mocking something like this.

public class AdminController : ApiController {
    public DTO Get() { return new Model().DTO); }
}

public class ClientController : ApiController {
    public IClientDTO Get() { return new Model().DTO); }
}

I would like the Serializer to serialize only those elements that are defined in the return type.

+4
3

JsonIgnoreAttribute, SSN.

public class DTO : IClientDTO {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [JsonIgnore]
    public string SSN { get; set; }
}

, - ClientDTO FirstName, LastName .. ClientWithSSN, ClientDTO . , SSN , ssn ClientWithSSN.

+1

. , .

, , . , . Json.net ...

return base.Serializer._contractResolver.ResolveContract(value.GetType());

. , , , . - Cam.

public interface IClientDTO {
    string FirstName { get; }
    string LastName { get; }
}

public interface IAdminDTO {
    string FirstName { get; }
    string LastName { get; }
    string SSN { get; }
}

class ClientDTO : IClientDTO {
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class AdminDTO : IAdminDTO {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SSN { get; set; }
}

class Model {
    class DTO {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string SSN { get; set; }
    }
    private DTO dto = new DTO() { FirstName = "John", LastName = "Doe", SSN = "111001111" };
    public IClientDTO ClientDTO { get { return new ClientDTO() { FirstName = dto.FirstName, LastName = dto.LastName }; } }
    public IAdminDTO AdminDTO { get { return new AdminDTO() { SSN = dto.SSN, FirstName = dto.FirstName, LastName = dto.LastName }; } }
}

class Program {

    static void Main(string[] args) {

        Model model = new Model();
        string sClientDTO = JsonConvert.SerializeObject(model.ClientDTO);
        string sAdminDTO = JsonConvert.SerializeObject(model.AdminDTO);

        Debug.WriteLine(sClientDTO);
        Debug.WriteLine(sAdminDTO);

    }
}

, , - , , , .

+1

ContractResolver :

private class IgnoreSSNResolver : DefaultContractResolver
{

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        Debug.Write(type.Name);
        var properties = base.CreateProperties(type, memberSerialization);

        var props = properties.Where((x) => x.PropertyName != "SSN");


        return props.ToList();
    }
}

, , :

JsonSerializerSettings sets = new JsonSerializerSettings();
sets.ContractResolver = new IgnoreSSNResolver();

string sClientDTO = JsonConvert.SerializeObject (clientDTO,sets);
0

Source: https://habr.com/ru/post/1537598/


All Articles