Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The encoder is difficult to customize #41

Open
Changaco opened this issue Feb 20, 2019 · 1 comment
Open

The encoder is difficult to customize #41

Changaco opened this issue Feb 20, 2019 · 1 comment

Comments

@Changaco
Copy link
Contributor

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
@Sekenre
Copy link
Collaborator

Sekenre commented Feb 21, 2019

dumps is just a wrapper around the CBOREncoder object which has a private _encoders dictionary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants