Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

python - How can I replace Zeep's default boolean serializer?

When I serialize a boolean value Zeep returns "true"/"false" (as per the xsd.types.Boolean class).

I would like to change this to return "1"/"0" instead because the API I'm working with only accepts these values.

How can I replace the default boolean serializer?

question from:https://stackoverflow.com/questions/65851740/how-can-i-replace-zeeps-default-boolean-serializer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ended up monkey patching the xmlvalue function.

class MyBoolean(Boolean):
    @check_no_collection
    def xmlvalue(self, value):
        return "1" if value and value not in ("false", "0") else "0"

Boolean.xmlvalue = MyBoolean.xmlvalue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...