Another very simple solution is to implement a ShouldSerialize* method in the type being serialized as outline here.
This might be the preferred way if you're in control of the type being serialized and if it is not a general behavior you want to introduce.