Entity objects needs converted to string to store them in log file for human reading. Properties may added to derived entities at runtime. Serialize is defined in entity base class. I triesd code below but it returns properties with null values also. Since many properties have null values, it makes logs difficult to read.
Hoq to serialize only not-null properties ? I tried also
var jsonSettings = new JsonSerializerSettings()
{
DefaultValueHandling = DefaultValueHandling.Ignore,
Formatting = Formatting.Indented,
TypeNameHandling= TypeNameHandling.All
};
return JsonConvert.SerializeObject(this, GetType(), jsonSettings);
this serializes only EntityBase class properties as confirmed in newtonsoft json does not serialize derived class properties
public class EntityBase {
public string Serialize()
{
var s = new System.Web.Script.Serialization.JavaScriptSerializer();
s.Serialize(this);
}
}
public class Customer: EntityBase {
public string Name, Address;
}
testcase:
var cust= new Customer() { Name="Test"};
Debug.Writeline( cust.Serialize());
Observed: result contains "Address": null
Expected: result shoud not contain Address property.
ASP.NET/Mono MVC4, .NET 4.6 are used