您能否根据日期时间值是否为 None 进行条件编码?

Can you have a conditional encoding dependent on whether a date time value is None?

是否只有当日期时间值未设置为None时才可以对approver_time值进行编码/解码?

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclasses.dataclass
class A:
    approver_time: datetime.datetime = field(default=None,
                                             metadata=config(
                                                 encoder=datetime.date.isoformat,
                                                 decoder=datetime.date.fromisoformat,
                                                 mm_field=fields.DateTime(format='iso')
                                             ))

使用 lambdas 函数回答了它。

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass_json
@dataclasses.dataclass
class A(Transfer):
    approver_time: datetime.datetime = field(metadata=config(
                                                 encoder=lambda x: datetime.datetime.isoformat
                                                 if x is not None else None,
                                                 decoder=lambda x: datetime.datetime.fromisoformat
                                                 if x is not None else None,
                                                 mm_field=fields.DateTime(format='iso')
                                             ), default=None)