Altered json-encode to be a generic method that will encode based on type (and can be overridden).