How to correctly use serialize when column type is unknown

I’m currently trying to fix dependencies which are connecting to the database during boot, eg. because they are trying to determine if a given column is json / jsonb or text, and whether they have to call serialize on it. A common way of doing this looks like this:

Is there a “proper” way to do this? So the model does not have to access the database to decide whether to call serialize? Or is this whole approach a mistake?