Skip to content

The encoder is difficult to customize #41

@Changaco

Description

@Changaco

Right now the only simple way to avoid #37 is to modify a global variable (see code below), which could theoretically break another module that uses cbor2 in the same process and expects the encoding of date objects to work.

import cbor2
cbor2.encoder.default_encoders.pop(date)

It could be nice to have a CBOR class that would hold configuration variables and implement the dump, dumps, load and loads functions. That would make it possible to create a custom CBOR encoder/decoder that could be used exactly like the global cbor2.* functions but would encode/decode slightly differently. For example:

from datetime import date
from cbor2 import CBOR
dateless_cbor = CBOR()
dateless_cbor.encoders.pop(date, None)
dateless_cbor.dumps(date(2019, 2, 20))  # raises CBOREncodeError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions