Setting two fields with the same JsonProperty

1

I would like to set two fields with the same JsonProperty value. If I try to have both of the fields with the same JsonEntity value I will get an error that I can't have two fields with the same JsonProperty.

public class Entity
{
    [JsonProperty("code")]
    public String Id
    {
        get
        {
            return Id;
        }
        set 
        {
            Id = value;
            IdDuplicate = value;
        }
    }

    public String IdDuplicate { get; set; }
}

Serialize it like this:

JsonConvert.DeserializeObject<JsonEntitys>("{ \"code\": \"test\" }");

But I get

(0xc0000005) 'Access violation'
c#
asp.net-core
asked on Stack Overflow Nov 18, 2019 by SwagiWagi • edited Feb 8, 2020 by SwagiWagi

2 Answers

0

I've managed to solve it using this way:

public class Entity
{
        public String id { private get; set; }

        [JsonProperty("code")]
        public String Id
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
                this.IdDuplicate = value;
            }
        }

    public String IdDuplicate { get; set; }
}
answered on Stack Overflow Feb 8, 2020 by SwagiWagi
0

try this:

 public class Entity
 {
    public String Id { get; set; }
    public String code
    {
        get
        {
            return Id; 
        }
        set
        {
            Id = value;
            IdDuplicate = value;
        }
    }

    public String IdDuplicate { get; set; }
}
answered on Stack Overflow Feb 8, 2020 by P├ęter

User contributions licensed under CC BY-SA 3.0