Parse provided binary field value and return parsed value (dictionary). For example: - (n, o, ...) - no compression, data is serialized using orjson - (z, o, ...) - zstandard compression, data is serialized using orjson
(self, value: Optional[Union[bytes, dict]])
| 401 | return super(JSONDictField, self).validate(value) |
| 402 | |
| 403 | def parse_field_value(self, value: Optional[Union[bytes, dict]]) -> dict: |
| 404 | """ |
| 405 | Parse provided binary field value and return parsed value (dictionary). |
| 406 | |
| 407 | For example: |
| 408 | |
| 409 | - (n, o, ...) - no compression, data is serialized using orjson |
| 410 | - (z, o, ...) - zstandard compression, data is serialized using orjson |
| 411 | """ |
| 412 | if not value: |
| 413 | return self.default |
| 414 | |
| 415 | if isinstance(value, dict): |
| 416 | # Already deserializaed |
| 417 | return value |
| 418 | |
| 419 | if not self.use_header: |
| 420 | return orjson.loads(value) |
| 421 | |
| 422 | split = value.split(JSON_DICT_FIELD_DELIMITER, 2) |
| 423 | |
| 424 | if len(split) != 3: |
| 425 | raise ValueError( |
| 426 | "Expected 3 values when splitting field value, got %s" % (len(split)) |
| 427 | ) |
| 428 | |
| 429 | compression_algorithm = split[0] |
| 430 | serialization_format = split[1] |
| 431 | data = split[2] |
| 432 | |
| 433 | if compression_algorithm not in VALID_JSON_DICT_COMPRESSION_ALGORITHMS: |
| 434 | raise ValueError( |
| 435 | "Invalid or unsupported value for compression algorithm header " |
| 436 | "value: %s" % (compression_algorithm) |
| 437 | ) |
| 438 | |
| 439 | if serialization_format not in VALID_JSON_DICT_SERIALIZATION_FORMATS: |
| 440 | raise ValueError( |
| 441 | "Invalid or unsupported value for serialization format header " |
| 442 | "value: %s" % (serialization_format) |
| 443 | ) |
| 444 | |
| 445 | if ( |
| 446 | compression_algorithm |
| 447 | == JSONDictFieldCompressionAlgorithmEnum.ZSTANDARD.value |
| 448 | ): |
| 449 | # NOTE: At this point zstandard is only test dependency |
| 450 | import zstandard |
| 451 | |
| 452 | data = zstandard.ZstdDecompressor().decompress(data) |
| 453 | |
| 454 | data = orjson.loads(data) |
| 455 | return data |
| 456 | |
| 457 | def _serialize_field_value(self, value: dict) -> bytes: |
| 458 | """ |
no outgoing calls