I had to generate some classes from an xsd file. The classes and properties are generated correct with xml serialization annotation. The problem is that the decimal properties of a class are serialized with Newtonsoft.Json even are not populated. I would like to serialize only the decimal properties that are properly populated. Amount is part of SaleMessage For example:
class Amount
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal RequestedAmount;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CashBackAmount;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal TipAmount;
}
//Usage
var amount = new Amount()
{
RequestedAmount = 12.0
}
Using this structure it will always serialize all the properties
like this
{"RequestedAmount":12.0,"CashBackAmount":0.0,"TipAmount":0.0}
Which is not the expected behaviour.
The question is how can I modify the serialization to not parse the not set properties
static string Serialize(SaleMessage saleMessage)
{
var serialize= JsonConvert.SerializeObject(saleToPoiMessage,
new StringEnumConverter(),
new IsoDateTimeConverter() { DateTimeFormat = DateTimeFormat });
return serialize;
}
Any help is appreciated :)