code: def to_json(self, value):
          
          if not self.is_valid(value):
              raise ex.SerializeException('Invalid value: {}'.format(value))
          return value
docs: Subclasses should override this method for JSON encoding.