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
What Is the Use of "Ref" for Reference-Type Variables in C#
Using Xmlserializer to Serialize Derived Classes
How to Store User Settings for a .Net Application
Xaml Binding Not Working on Dependency Property
How to Read the Color of a Screen Pixel
Why Can't I Have Abstract Static Methods in C#
View/Edit Id3 Data for Mp3 Files
Convert an Image (Selected by Path) to Base64 String
C# Sort and Orderby Comparison
Correct Use of Multimapping in Dapper
Programmatically Determine a Duration of a Locked Workstation
Dependency Injection in Attributes
Why Is a Dictionary "Not Ordered"
What Does Null! Statement Mean
How to Compile and Execute New Code at Runtime in .Net
Calculating Distance Between Two Latitude and Longitude Geocoordinates