Using the JsonConverter attribute to prevent self-regulation

I get the following error: "Native loop detected for property" DefaultAssignTo "

Here is the class I'm trying to serialize:

public class Account
{
    public int Id { get; set; }
    public string AccountName { get; set; }

    [JsonConverter(typeof(AccountConverter))]
    public virtual Account DefaultAssignTo { get; set; }
    public int? DefaultAssignToId { get; set; }

    public virtual ICollection<Role> Roles { get; set; }

    [JsonConverter(typeof(OtherConverter))]
    public virtual Other Other { get; set; }
    public int? OtherId { get; set; }

    [JsonIgnore]
    public string OtherName
    {
        get
        {
        return "Name = " + this.AccountName;
        }
}
}

I know that I can prevent this from adding settings as shown below, but this creates problems when trying to deserialize.

var settings = new JsonSerializerSettings()
{
    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
    PreserveReferencesHandling = PreserveReferencesHandling.All
};

As you can see, I created a couple of customizable converters. One for the class Otherworks, but one that is Accountnever called for the class . I assume that since by the time the ContractResolver receives DefaultAssignTo, that is Account, the contract already exists for Account, so the user converter is never used.

Here are my questions:

  • ? ( , .)

# 1 :

  1. , ?
  2. ?

: ( , , , )

An Account -. (, a Task). Account Task, DefaultAssignedTo , Task DefaultAssignedTo. DefaultAssignedTo Account Account, DefaultAssignedTo.

, Accounts John and Jane, DefaultAssignedTo John . Task, , , . , , .

: , DefaultAssignedTo . - , , , DefaultAssignedTo.

+4

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


All Articles