简体   繁体   中英

Property not getting serialized in Json via newtonsoft

There are many properties defined in class but one of the property is not getting serialized(using newtonsoft) and that property is mentioned below:

public override string Value
{​​​​
  set    
  {
​​​​
    if (value == null)    
    {    ​​​​
       this.Null = true;    
    }​​ ​ ​
    else if (value == string.Empty)
    {    ​​​​
       _A = string.Empty;    
       _P = string.Empty;    
       _S = string.Empty;    
    }​​​ ​
    else
    {​​​ ​

         value = _A + _P + _S;    
     }​​​​

   }​​​​    
}

but when we define getter in the property then it is getting serialized so please suggest; we do not want to place the getter in the property.

Set [JsonProperty] attribute to your property. But overall, you could have your getter as private - so you'll not expose data to other classes, but it could help to solve your problem.

Well, to start, you have no Get how will json now to serialize it, perhaps make it a method looks funny to have only a set on a property. If you like to populate your fields just tell json to do that using annotations

[JsonProperty("_A")]
private string _A ;

[JsonProperty("_P")]
private string _P ;

[JsonProperty("_S")]
private string _S;

[JsonIgnore]
public override string Value
{​​​​
  set    
  {
​​​​
    if (value == null)    
    {    ​​​​
       this.Null = true;    
    }​​ ​ ​
    else if (value == string.Empty)
    {    ​​​​
       _A = string.Empty;    
       _P = string.Empty;    
       _S = string.Empty;    
    }​​​ ​
    else
    {​​​ ​

         value = _A + _P + _S;    
     }​​​​

   }​​​​    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM