When I deserialize the JSON string to a class, I need some of the properties to get an empty format, not null.
Today:
{
"OrderResponse": {
"ID": {
"Content": "xxx"
},
"IssueDate": {
"Content": "2022-07-07"
},
"IssueTime": {
"Content": "14:42:07"
},
"OrderReference": {
"ID": {
"Content": "xxx"
}
},
"SellerSupplierParty": {
"PartyIdentification": null
},
"BuyerCustomerParty": {
"PartyIdentification": null
},...
As you can see both PartyIdentification
is null.
I need it to be like this:
{
"OrderResponse": {
"ID": {
"Content": "xxxx"
},
"IssueDate": {
"Content": "2022-07-06"
},
"IssueTime": {
"Content": "14:27:35"
},
"OrderReference": {
"ID": {
"Content": "OKNqIBEo"
}
},
"BuyerCustomerParty": {
"Party": {
"PartyIdentification": {
"ID": {}
}
}
},
"SellerSupplierParty": {
"Party": {
"PartyIdentification": {
"ID": {}
}
}
},...
The class definition looks like this:
public class SftiOrderResponse
{
public OrderResponse OrderResponse { get; set; }
}
public class OrderResponse
{
[JsonProperty(PropertyName = "ID")]
public OrderId Id { get; set; }
public IssueDate IssueDate { get; set; }
public IssueTime IssueTime { get; set; }
public OrderReference OrderReference { get; set; }
public Party SellerSupplierParty { get; set; }
public Party BuyerCustomerParty { get; set; }
public IList<OrderLine> OrderLine { get; set; }
}
public class Party
{
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public PartyIdentification PartyIdentification { get; set; }
}
public class PartyIdentification
{
[JsonProperty(PropertyName = "ID")]
public PartyIdentificationId Id { get; set; }
}
public class PartyIdentificationId : StringContent { }
public class StringContent
{
public string Content { get; set; }
}
What is the best way to do it?
CodePudding user response:
The simplest way is to include a default value in your class. You can also create a custom JsonConverter.
CodePudding user response:
Newtonsoft.Json has a class called "JsonConvert"
If you call
JsonConvert.SerializeObject(data, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
}
Then it will not serialize out null values as shown
You can also set the option
DefaultValueHandling = DefaultValueHandling.Ignore
to not serialize defaults as well.
Using JsonSerializerSettings both works for serialization and deserialization
Update
If you initialize your strings to empty string -- then on deserialize, it will ignore the null values in the json file, and leave the strings as empty string
Answer 2:
You could also write a custom String converter inheriting off of JsonConverter
JsonConvert.SerializeObject(data, new JsonSerializerSettings()
{
Converters = new List<JsonConverter> { new PartyIdentification() }
}
public class PartyIdentificationConverter: JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(PartyIdentificationConverter).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
reader.Read();
var p = (PartyIdentificationConverter)reader.Value;
return p!= null ? p: new PartyIdentificationConverter();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
}