How to Ignore JSONproperty(Propertyname = "Somename") When Serializing JSON

How can I ignore the property name that I use in the jsonproperty attribute, provided that it is only once

you can add a constructor to your class

public class DocumentDetail
{
public string qrCode { get; set; }

public string gtinNumber { get; set; }

[JsonProperty("lotNumber")] // optional, you can assign any name for serialization
public string lotNumber { get; set; }

[Newtonsoft.Json.JsonConstructor]
public DocumentDetail( string KAREKOD,string GTIN, string LOTNUMBER)
{
qrCode=KAREKOD;
gtinNumber=GTIN;
lotNumber=LOTNUMBER;
}
public DocumentDetail() {}
}

and you don't need to include all properties in the constructor, just include the properties that need different names for a serialization and a deserialiazation.

Ignore some of the properties during Json.NET serializing but required on others

Try the below code

    [JsonProperty(PropertyName = "componentMainVersion", DefaultValueHandling = DefaultValueHandling.Include)] 
public ushort Version { get; set; }

Required is a different property which makes sure that value for the property is required always

How to ignore @JsonProperty when Serialization in Java

Use simple access methods annotated with @JsonGetter or @JsonSetter respectively, each configured with required name of json property.
In your case the code could be something like this:

public class Account {

private String reasonCode;

@JsonGetter("reasonCode")
public String getReasonCode() {
return reasonCode;
}

@JsonSetter("rCode")
public void setReasonCode(String reasonCode) {
this.reasonCode = reasonCode;
}
}

Using JsonProperty for Deserializing but NOT serializing

You could use a set-only property that points to the 'good' property.

public class Item
{
public int Id { get; set; }

[JsonProperty("pkID")]
public int BackwardCompatibleId { set => Id = value; }
}

// test
var x = new Item { Id = 88 };
var json = JsonConvert.SerializeObject(x); // {"Id":88}
var clone = JsonConvert.DeserializeObject<Item>("{\"pkId\":99}");


Related Topics



Leave a reply



Submit