File size: 31,267 Bytes
48e124a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 |
from __future__ import annotations
import math
import re
import struct
import sys
from collections import OrderedDict, defaultdict
from collections.abc import Callable, Generator, Mapping, Sequence, Set
from contextlib import contextmanager
from datetime import date, datetime, time, tzinfo
from functools import wraps
from io import BytesIO
from sys import modules
from typing import IO, TYPE_CHECKING, Any, cast
from ._types import (
CBOREncodeTypeError,
CBOREncodeValueError,
CBORSimpleValue,
CBORTag,
FrozenDict,
UndefinedType,
undefined,
)
if TYPE_CHECKING:
from decimal import Decimal
from email.message import Message
from fractions import Fraction
from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network
from uuid import UUID
if sys.version_info >= (3, 12):
from collections.abc import Buffer
else:
from typing_extensions import Buffer
def shareable_encoder(
func: Callable[[CBOREncoder, Any], None],
) -> Callable[[CBOREncoder, Any], None]:
"""
Wrap the given encoder function to gracefully handle cyclic data
structures.
If value sharing is enabled, this marks the given value shared in the
datastream on the first call. If the value has already been passed to this
method, a reference marker is instead written to the data stream and the
wrapped function is not called.
If value sharing is disabled, only infinite recursion protection is done.
:rtype: Callable[[cbor2.CBOREncoder, Any], None]
"""
@wraps(func)
def wrapper(encoder: CBOREncoder, value: Any) -> None:
encoder.encode_shared(func, value)
return wrapper
def container_encoder(
func: Callable[[CBOREncoder, Any], Any],
) -> Callable[[CBOREncoder, Any], Any]:
"""
The given encoder is a container with child values. Handle cyclic or
duplicate references to the value and strings within the value
efficiently.
Containers may contain cyclic data structures or may contain values
or themselves by referenced multiple times throughout the greater
encoded value and could thus be more efficiently encoded with shared
value references and string references where duplication occurs.
If value sharing is enabled, this marks the given value shared in the
datastream on the first call. If the value has already been passed to this
method, a reference marker is instead written to the data stream and the
wrapped function is not called.
If value sharing is disabled, only infinite recursion protection is done.
If string referencing is enabled and this is the first use of this
method in encoding a value, all repeated references to long strings
and bytearrays will be replaced with references to the first
occurrence of those arrays.
If string referencing is disabled, all strings and bytearrays will
be encoded directly.
"""
@wraps(func)
def wrapper(encoder: CBOREncoder, value: Any) -> None:
encoder.encode_container(func, value)
return wrapper
class CBOREncoder:
"""
The CBOREncoder class implements a fully featured `CBOR`_ encoder with
several extensions for handling shared references, big integers, rational
numbers and so on. Typically the class is not used directly, but the
:func:`dump` and :func:`dumps` functions are called to indirectly construct
and use the class.
When the class is constructed manually, the main entry points are
:meth:`encode` and :meth:`encode_to_bytes`.
.. _CBOR: https://cbor.io/
"""
__slots__ = (
"datetime_as_timestamp",
"date_as_datetime",
"_timezone",
"_default",
"value_sharing",
"_fp",
"_fp_write",
"_shared_containers",
"_encoders",
"_canonical",
"string_referencing",
"string_namespacing",
"_string_references",
"indefinite_containers",
)
_fp: IO[bytes]
_fp_write: Callable[[Buffer], int]
def __init__(
self,
fp: IO[bytes],
datetime_as_timestamp: bool = False,
timezone: tzinfo | None = None,
value_sharing: bool = False,
default: Callable[[CBOREncoder, Any], Any] | None = None,
canonical: bool = False,
date_as_datetime: bool = False,
string_referencing: bool = False,
indefinite_containers: bool = False,
):
"""
:param fp:
the file to write to (any file-like object opened for writing in binary
mode)
:param datetime_as_timestamp:
set to ``True`` to serialize datetimes as UNIX timestamps (this makes
datetimes more concise on the wire, but loses the timezone information)
:param timezone:
the default timezone to use for serializing naive datetimes; if this is not
specified naive datetimes will throw a :exc:`ValueError` when encoding is
attempted
:param value_sharing:
set to ``True`` to allow more efficient serializing of repeated values and,
more importantly, cyclic data structures, at the cost of extra line overhead
:param default:
a callable that is called by the encoder with two arguments (the encoder
instance and the value being encoded) when no suitable encoder has been
found, and should use the methods on the encoder to encode any objects it
wants to add to the data stream
:param canonical:
when ``True``, use "canonical" CBOR representation; this typically involves
sorting maps, sets, etc. into a pre-determined order ensuring that
serializations are comparable without decoding
:param date_as_datetime:
set to ``True`` to serialize date objects as datetimes (CBOR tag 0), which
was the default behavior in previous releases (cbor2 <= 4.1.2).
:param string_referencing:
set to ``True`` to allow more efficient serializing of repeated string
values
:param indefinite_containers:
encode containers as indefinite (use stop code instead of specifying length)
"""
self.fp = fp
self.datetime_as_timestamp = datetime_as_timestamp
self.date_as_datetime = date_as_datetime
self.timezone = timezone
self.value_sharing = value_sharing
self.string_referencing = string_referencing
self.string_namespacing = string_referencing
self.indefinite_containers = indefinite_containers
self.default = default
self._canonical = canonical
self._shared_containers: dict[
int, tuple[object, int | None]
] = {} # indexes used for value sharing
self._string_references: dict[str | bytes, int] = {} # indexes used for string references
self._encoders = default_encoders.copy()
if canonical:
self._encoders.update(canonical_encoders)
def _find_encoder(self, obj_type: type) -> Callable[[CBOREncoder, Any], None] | None:
for type_or_tuple, enc in list(self._encoders.items()):
if type(type_or_tuple) is tuple:
try:
modname, typename = type_or_tuple
except (TypeError, ValueError):
raise CBOREncodeValueError(
f"invalid deferred encoder type {type_or_tuple!r} (must be a "
"2-tuple of module name and type name, e.g. "
"('collections', 'defaultdict'))"
)
imported_type = getattr(modules.get(modname), typename, None)
if imported_type is not None:
del self._encoders[type_or_tuple]
self._encoders[imported_type] = enc
type_ = imported_type
else: # pragma: nocover
continue
else:
type_ = type_or_tuple
if issubclass(obj_type, type_):
self._encoders[obj_type] = enc
return enc
return None
@property
def fp(self) -> IO[bytes]:
return self._fp
@fp.setter
def fp(self, value: IO[bytes]) -> None:
try:
if not callable(value.write):
raise ValueError("fp.write is not callable")
except AttributeError:
raise ValueError("fp object has no write method")
else:
self._fp = value
self._fp_write = value.write
@property
def timezone(self) -> tzinfo | None:
return self._timezone
@timezone.setter
def timezone(self, value: tzinfo | None) -> None:
if value is None or isinstance(value, tzinfo):
self._timezone = value
else:
raise ValueError("timezone must be None or a tzinfo instance")
@property
def default(self) -> Callable[[CBOREncoder, Any], Any] | None:
return self._default
@default.setter
def default(self, value: Callable[[CBOREncoder, Any], Any] | None) -> None:
if value is None or callable(value):
self._default = value
else:
raise ValueError("default must be None or a callable")
@property
def canonical(self) -> bool:
return self._canonical
@contextmanager
def disable_value_sharing(self) -> Generator[None]:
"""
Disable value sharing in the encoder for the duration of the context
block.
"""
old_value_sharing = self.value_sharing
self.value_sharing = False
yield
self.value_sharing = old_value_sharing
@contextmanager
def disable_string_referencing(self) -> Generator[None]:
"""
Disable tracking of string references for the duration of the
context block.
"""
old_string_referencing = self.string_referencing
self.string_referencing = False
yield
self.string_referencing = old_string_referencing
@contextmanager
def disable_string_namespacing(self) -> Generator[None]:
"""
Disable generation of new string namespaces for the duration of the
context block.
"""
old_string_namespacing = self.string_namespacing
self.string_namespacing = False
yield
self.string_namespacing = old_string_namespacing
def write(self, data: bytes) -> None:
"""
Write bytes to the data stream.
:param bytes data:
the bytes to write
"""
self._fp_write(data)
def encode(self, obj: Any) -> None:
"""
Encode the given object using CBOR.
:param obj:
the object to encode
"""
obj_type = obj.__class__
encoder = self._encoders.get(obj_type) or self._find_encoder(obj_type) or self._default
if not encoder:
raise CBOREncodeTypeError(f"cannot serialize type {obj_type.__name__}")
encoder(self, obj)
def encode_to_bytes(self, obj: Any) -> bytes:
"""
Encode the given object to a byte buffer and return its value as bytes.
This method was intended to be used from the ``default`` hook when an
object needs to be encoded separately from the rest but while still
taking advantage of the shared value registry.
"""
with BytesIO() as fp:
old_fp = self.fp
self.fp = fp
self.encode(obj)
self.fp = old_fp
return fp.getvalue()
def encode_container(self, encoder: Callable[[CBOREncoder, Any], Any], value: Any) -> None:
if self.string_namespacing:
# Create a new string reference domain
self.encode_length(6, 256)
with self.disable_string_namespacing():
self.encode_shared(encoder, value)
def encode_shared(self, encoder: Callable[[CBOREncoder, Any], Any], value: Any) -> None:
value_id = id(value)
try:
index = self._shared_containers[id(value)][1]
except KeyError:
if self.value_sharing:
# Mark the container as shareable
self._shared_containers[value_id] = (
value,
len(self._shared_containers),
)
self.encode_length(6, 0x1C)
encoder(self, value)
else:
self._shared_containers[value_id] = (value, None)
try:
encoder(self, value)
finally:
del self._shared_containers[value_id]
else:
if self.value_sharing:
# Generate a reference to the previous index instead of
# encoding this again
self.encode_length(6, 0x1D)
self.encode_int(cast(int, index))
else:
raise CBOREncodeValueError(
"cyclic data structure detected but value sharing is disabled"
)
def _stringref(self, value: str | bytes) -> bool:
"""
Try to encode the string or bytestring as a reference.
Returns True if a reference was generated, False if the string
must still be emitted.
"""
try:
index = self._string_references[value]
self.encode_semantic(CBORTag(25, index))
return True
except KeyError:
length = len(value)
next_index = len(self._string_references)
if next_index < 24:
is_referenced = length >= 3
elif next_index < 256:
is_referenced = length >= 4
elif next_index < 65536:
is_referenced = length >= 5
elif next_index < 4294967296:
is_referenced = length >= 7
else:
is_referenced = length >= 11
if is_referenced:
self._string_references[value] = next_index
return False
def encode_length(self, major_tag: int, length: int | None) -> None:
major_tag <<= 5
if length is None: # Indefinite
self._fp_write(struct.pack(">B", major_tag | 31))
elif length < 24:
self._fp_write(struct.pack(">B", major_tag | length))
elif length < 256:
self._fp_write(struct.pack(">BB", major_tag | 24, length))
elif length < 65536:
self._fp_write(struct.pack(">BH", major_tag | 25, length))
elif length < 4294967296:
self._fp_write(struct.pack(">BL", major_tag | 26, length))
else:
self._fp_write(struct.pack(">BQ", major_tag | 27, length))
def encode_break(self) -> None:
# Break stop code for indefinite containers
self._fp_write(struct.pack(">B", (7 << 5) | 31))
def encode_int(self, value: int) -> None:
# Big integers (2 ** 64 and over)
if value >= 18446744073709551616 or value < -18446744073709551616:
if value >= 0:
major_type = 0x02
else:
major_type = 0x03
value = -value - 1
payload = value.to_bytes((value.bit_length() + 7) // 8, "big")
self.encode_semantic(CBORTag(major_type, payload))
elif value >= 0:
self.encode_length(0, value)
else:
self.encode_length(1, -(value + 1))
def encode_bytestring(self, value: bytes) -> None:
if self.string_referencing:
if self._stringref(value):
return
self.encode_length(2, len(value))
self._fp_write(value)
def encode_bytearray(self, value: bytearray) -> None:
self.encode_bytestring(bytes(value))
def encode_string(self, value: str) -> None:
if self.string_referencing:
if self._stringref(value):
return
encoded = value.encode("utf-8")
self.encode_length(3, len(encoded))
self._fp_write(encoded)
@container_encoder
def encode_array(self, value: Sequence[Any]) -> None:
self.encode_length(4, len(value) if not self.indefinite_containers else None)
for item in value:
self.encode(item)
if self.indefinite_containers:
self.encode_break()
@container_encoder
def encode_map(self, value: Mapping[Any, Any]) -> None:
self.encode_length(5, len(value) if not self.indefinite_containers else None)
for key, val in value.items():
self.encode(key)
self.encode(val)
if self.indefinite_containers:
self.encode_break()
def encode_sortable_key(self, value: Any) -> tuple[int, bytes]:
"""
Takes a key and calculates the length of its optimal byte
representation, along with the representation itself. This is used as
the sorting key in CBOR's canonical representations.
"""
with self.disable_string_referencing():
encoded = self.encode_to_bytes(value)
return len(encoded), encoded
@container_encoder
def encode_canonical_map(self, value: Mapping[Any, Any]) -> None:
"""Reorder keys according to Canonical CBOR specification"""
keyed_keys = ((self.encode_sortable_key(key), key, value) for key, value in value.items())
self.encode_length(5, len(value) if not self.indefinite_containers else None)
for sortkey, realkey, value in sorted(keyed_keys):
if self.string_referencing:
# String referencing requires that the order encoded is
# the same as the order emitted so string references are
# generated after an order is determined
self.encode(realkey)
else:
self._fp_write(sortkey[1])
self.encode(value)
if self.indefinite_containers:
self.encode_break()
def encode_semantic(self, value: CBORTag) -> None:
# Nested string reference domains are distinct
old_string_referencing = self.string_referencing
old_string_references = self._string_references
if value.tag == 256:
self.string_referencing = True
self._string_references = {}
self.encode_length(6, value.tag)
self.encode(value.value)
self.string_referencing = old_string_referencing
self._string_references = old_string_references
#
# Semantic decoders (major tag 6)
#
def encode_datetime(self, value: datetime) -> None:
# Semantic tag 0
if not value.tzinfo:
if self._timezone:
value = value.replace(tzinfo=self._timezone)
else:
raise CBOREncodeValueError(
f"naive datetime {value!r} encountered and no default timezone " "has been set"
)
if self.datetime_as_timestamp:
from calendar import timegm
if not value.microsecond:
timestamp: float = timegm(value.utctimetuple())
else:
timestamp = timegm(value.utctimetuple()) + value.microsecond / 1000000
self.encode_semantic(CBORTag(1, timestamp))
else:
datestring = value.isoformat().replace("+00:00", "Z")
self.encode_semantic(CBORTag(0, datestring))
def encode_date(self, value: date) -> None:
# Semantic tag 100
if self.date_as_datetime:
value = datetime.combine(value, time()).replace(tzinfo=self._timezone)
self.encode_datetime(value)
elif self.datetime_as_timestamp:
days_since_epoch = value.toordinal() - 719163
self.encode_semantic(CBORTag(100, days_since_epoch))
else:
datestring = value.isoformat()
self.encode_semantic(CBORTag(1004, datestring))
def encode_decimal(self, value: Decimal) -> None:
# Semantic tag 4
if value.is_nan():
self._fp_write(b"\xf9\x7e\x00")
elif value.is_infinite():
self._fp_write(b"\xf9\x7c\x00" if value > 0 else b"\xf9\xfc\x00")
else:
dt = value.as_tuple()
sig = 0
for digit in dt.digits:
sig = (sig * 10) + digit
if dt.sign:
sig = -sig
with self.disable_value_sharing():
self.encode_semantic(CBORTag(4, [dt.exponent, sig]))
def encode_stringref(self, value: str | bytes) -> None:
# Semantic tag 25
if not self._stringref(value):
self.encode(value)
def encode_rational(self, value: Fraction) -> None:
# Semantic tag 30
with self.disable_value_sharing():
self.encode_semantic(CBORTag(30, [value.numerator, value.denominator]))
def encode_regexp(self, value: re.Pattern[str]) -> None:
# Semantic tag 35
self.encode_semantic(CBORTag(35, str(value.pattern)))
def encode_mime(self, value: Message) -> None:
# Semantic tag 36
self.encode_semantic(CBORTag(36, value.as_string()))
def encode_uuid(self, value: UUID) -> None:
# Semantic tag 37
self.encode_semantic(CBORTag(37, value.bytes))
def encode_stringref_namespace(self, value: Any) -> None:
# Semantic tag 256
with self.disable_string_namespacing():
self.encode_semantic(CBORTag(256, value))
def encode_set(self, value: Set[Any]) -> None:
# Semantic tag 258
self.encode_semantic(CBORTag(258, tuple(value)))
def encode_canonical_set(self, value: Set[Any]) -> None:
# Semantic tag 258
values = sorted((self.encode_sortable_key(key), key) for key in value)
self.encode_semantic(CBORTag(258, [key[1] for key in values]))
def encode_ipaddress(self, value: IPv4Address | IPv6Address) -> None:
# Semantic tag 260
self.encode_semantic(CBORTag(260, value.packed))
def encode_ipnetwork(self, value: IPv4Network | IPv6Network) -> None:
# Semantic tag 261
self.encode_semantic(CBORTag(261, {value.network_address.packed: value.prefixlen}))
#
# Special encoders (major tag 7)
#
def encode_simple_value(self, value: CBORSimpleValue) -> None:
if value.value < 24:
self._fp_write(struct.pack(">B", 0xE0 | value.value))
else:
self._fp_write(struct.pack(">BB", 0xF8, value.value))
def encode_float(self, value: float) -> None:
# Handle special values efficiently
if math.isnan(value):
self._fp_write(b"\xf9\x7e\x00")
elif math.isinf(value):
self._fp_write(b"\xf9\x7c\x00" if value > 0 else b"\xf9\xfc\x00")
else:
self._fp_write(struct.pack(">Bd", 0xFB, value))
def encode_complex(self, value: complex) -> None:
# Semantic tag 43000
with self.disable_value_sharing():
self.encode_semantic(CBORTag(43000, [value.real, value.imag]))
def encode_minimal_float(self, value: float) -> None:
# Handle special values efficiently
if math.isnan(value):
self._fp_write(b"\xf9\x7e\x00")
elif math.isinf(value):
self._fp_write(b"\xf9\x7c\x00" if value > 0 else b"\xf9\xfc\x00")
else:
# Try each encoding in turn from longest to shortest
encoded = struct.pack(">Bd", 0xFB, value)
for format, tag in [(">Bf", 0xFA), (">Be", 0xF9)]:
try:
new_encoded = struct.pack(format, tag, value)
# Check if encoding as low-byte float loses precision
if struct.unpack(format, new_encoded)[1] == value:
encoded = new_encoded
else:
break
except OverflowError:
break
self._fp_write(encoded)
def encode_boolean(self, value: bool) -> None:
self._fp_write(b"\xf5" if value else b"\xf4")
def encode_none(self, value: None) -> None:
self._fp_write(b"\xf6")
def encode_undefined(self, value: UndefinedType) -> None:
self._fp_write(b"\xf7")
default_encoders: dict[type | tuple[str, str], Callable[[CBOREncoder, Any], None]] = {
bytes: CBOREncoder.encode_bytestring,
bytearray: CBOREncoder.encode_bytearray,
str: CBOREncoder.encode_string,
int: CBOREncoder.encode_int,
float: CBOREncoder.encode_float,
complex: CBOREncoder.encode_complex,
("decimal", "Decimal"): CBOREncoder.encode_decimal,
bool: CBOREncoder.encode_boolean,
type(None): CBOREncoder.encode_none,
tuple: CBOREncoder.encode_array,
list: CBOREncoder.encode_array,
dict: CBOREncoder.encode_map,
defaultdict: CBOREncoder.encode_map,
OrderedDict: CBOREncoder.encode_map,
FrozenDict: CBOREncoder.encode_map,
type(undefined): CBOREncoder.encode_undefined,
datetime: CBOREncoder.encode_datetime,
date: CBOREncoder.encode_date,
re.Pattern: CBOREncoder.encode_regexp,
("fractions", "Fraction"): CBOREncoder.encode_rational,
("email.message", "Message"): CBOREncoder.encode_mime,
("uuid", "UUID"): CBOREncoder.encode_uuid,
("ipaddress", "IPv4Address"): CBOREncoder.encode_ipaddress,
("ipaddress", "IPv6Address"): CBOREncoder.encode_ipaddress,
("ipaddress", "IPv4Network"): CBOREncoder.encode_ipnetwork,
("ipaddress", "IPv6Network"): CBOREncoder.encode_ipnetwork,
CBORSimpleValue: CBOREncoder.encode_simple_value,
CBORTag: CBOREncoder.encode_semantic,
set: CBOREncoder.encode_set,
frozenset: CBOREncoder.encode_set,
}
canonical_encoders: dict[type | tuple[str, str], Callable[[CBOREncoder, Any], None]] = {
float: CBOREncoder.encode_minimal_float,
dict: CBOREncoder.encode_canonical_map,
defaultdict: CBOREncoder.encode_canonical_map,
OrderedDict: CBOREncoder.encode_canonical_map,
FrozenDict: CBOREncoder.encode_canonical_map,
set: CBOREncoder.encode_canonical_set,
frozenset: CBOREncoder.encode_canonical_set,
}
def dumps(
obj: object,
datetime_as_timestamp: bool = False,
timezone: tzinfo | None = None,
value_sharing: bool = False,
default: Callable[[CBOREncoder, Any], None] | None = None,
canonical: bool = False,
date_as_datetime: bool = False,
string_referencing: bool = False,
indefinite_containers: bool = False,
) -> bytes:
"""
Serialize an object to a bytestring.
:param obj:
the object to serialize
:param datetime_as_timestamp:
set to ``True`` to serialize datetimes as UNIX timestamps (this makes datetimes
more concise on the wire, but loses the timezone information)
:param timezone:
the default timezone to use for serializing naive datetimes; if this is not
specified naive datetimes will throw a :exc:`ValueError` when encoding is
attempted
:param value_sharing:
set to ``True`` to allow more efficient serializing of repeated values
and, more importantly, cyclic data structures, at the cost of extra
line overhead
:param default:
a callable that is called by the encoder with two arguments (the encoder
instance and the value being encoded) when no suitable encoder has been found,
and should use the methods on the encoder to encode any objects it wants to add
to the data stream
:param canonical:
when ``True``, use "canonical" CBOR representation; this typically involves
sorting maps, sets, etc. into a pre-determined order ensuring that
serializations are comparable without decoding
:param date_as_datetime:
set to ``True`` to serialize date objects as datetimes (CBOR tag 0), which was
the default behavior in previous releases (cbor2 <= 4.1.2).
:param string_referencing:
set to ``True`` to allow more efficient serializing of repeated string values
:param indefinite_containers:
encode containers as indefinite (use stop code instead of specifying length)
:return: the serialized output
"""
with BytesIO() as fp:
CBOREncoder(
fp,
datetime_as_timestamp=datetime_as_timestamp,
timezone=timezone,
value_sharing=value_sharing,
default=default,
canonical=canonical,
date_as_datetime=date_as_datetime,
string_referencing=string_referencing,
indefinite_containers=indefinite_containers,
).encode(obj)
return fp.getvalue()
def dump(
obj: object,
fp: IO[bytes],
datetime_as_timestamp: bool = False,
timezone: tzinfo | None = None,
value_sharing: bool = False,
default: Callable[[CBOREncoder, Any], None] | None = None,
canonical: bool = False,
date_as_datetime: bool = False,
string_referencing: bool = False,
indefinite_containers: bool = False,
) -> None:
"""
Serialize an object to a file.
:param obj:
the object to serialize
:param fp:
the file to write to (any file-like object opened for writing in binary mode)
:param datetime_as_timestamp:
set to ``True`` to serialize datetimes as UNIX timestamps (this makes datetimes
more concise on the wire, but loses the timezone information)
:param timezone:
the default timezone to use for serializing naive datetimes; if this is not
specified naive datetimes will throw a :exc:`ValueError` when encoding is
attempted
:param value_sharing:
set to ``True`` to allow more efficient serializing of repeated values
and, more importantly, cyclic data structures, at the cost of extra
line overhead
:param default:
a callable that is called by the encoder with two arguments (the encoder
instance and the value being encoded) when no suitable encoder has been found,
and should use the methods on the encoder to encode any objects it wants to add
to the data stream
:param canonical:
when ``True``, use "canonical" CBOR representation; this typically involves
sorting maps, sets, etc. into a pre-determined order ensuring that
serializations are comparable without decoding
:param date_as_datetime:
set to ``True`` to serialize date objects as datetimes (CBOR tag 0), which was
the default behavior in previous releases (cbor2 <= 4.1.2).
:param indefinite_containers:
encode containers as indefinite (use stop code instead of specifying length)
:param string_referencing:
set to ``True`` to allow more efficient serializing of repeated string values
"""
CBOREncoder(
fp,
datetime_as_timestamp=datetime_as_timestamp,
timezone=timezone,
value_sharing=value_sharing,
default=default,
canonical=canonical,
date_as_datetime=date_as_datetime,
string_referencing=string_referencing,
indefinite_containers=indefinite_containers,
).encode(obj)
|