Consider the F# fragment below:
type MyType = {
CrucialProperty: int
OptionalProperty: string option
}
let first = { CrucialProperty = 500; OptionalProperty = Some("Hello")}
let second = { CrucialProperty = 500; OptionalProperty = Some(null)}
let third = { CrucialProperty = 500; OptionalProperty = None}
I wish to do serialize this type using JSON.NET so I get the following strings respectively for the cases described above:
{"CrucialProperty":500,"OptionalProperty":"Hello"}
{"CrucialProperty":500,"OptionalProperty":null}
{"CrucialProperty":500}
Essentially, the problem boils down to being able to include/exclude a property in the serialized output based on the value of that property.
I've managed to find a few "OptionConverters" out there (e.g here), but they don't quite seem to do what I'm looking for.