In my DocumentDb documents, I don't want to include properties with NULL values. For example, I have the following POCO class.
public class Person
{
[JsonProperty(PropertyName="id")]
public int PersonId {get; set;}
[JsonProperty(PropertyName="firstName")]
public string FirstName {get; set;}
[JsonProperty(PropertyName="middleName")]
public string MiddleName {get; set;}
[JsonProperty(PropertyName="lastName")]
public string LastName {get; set;}
}
Some people don't have middle names and when I save a person's document in my collection, I don't want the middle name to be included. Currently, a person without a middle name is saved as:
{
"id": 1234,
"firstName": "John",
"middleName": null,
"lastName": "Smith"
}
Is this normal behavior? If not, how do I NOT include the middle name property with a NULL value in my document?
P.S. All serialization/deserialization is handled by JSON.NET
You can do that when you initialize the Cosmos Client, there's a serialization option which is similar to the JSON.Net.
CosmosClient client = new CosmosClient(yourConnectionString, new CosmosClientOptions()
{
SerializerOptions = new CosmosSerializationOptions()
{
IgnoreNullValues = true,
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With