In the codebase I am woking on, the most common object that is serialized in json has 3 fields. The value of one of these fields is often, but not always, null.
I was asked to avoid sending the field in the json build if its value is null. And I don't see how to do it. The JsonFX Annotations I know only allow to ignore a field whatever its value (JsonIgnore), or to transform the value of a field (using JsonName and properties )
If you want to skip nulls unconditionally for all properties and fields of all types, you can subclass your
IResolverStrategy
(which is likelyJsonResolverStrategy
orPocoResolverStrategy
), overrideGetValueIgnoredCallback(MemberInfo member)
, and return a delegate that skips null values:Then use it like:
If you only want to skip nulls on selected properties, you need to use
JsonResolverStrategy
(or a subclass), and then eitherSet
[DefaultValue(null)]
on the property, ORSet
[JsonSpecifiedProperty(string methodName)]
on the property to specify the name of another property that returnsfalse
when the the property is to be skipped.For instance: