Functions for serializing a type that is an instance of ToJSON