Decoder¶
Note
Checkout the getting started guide for more details on encoding.
Implementation of the JSON5 decoder.
ObjectHookArg
module-attribute
¶
Type hint for the argument of the object_hook
function.
ObjectHook
module-attribute
¶
ObjectHook = Callable[[ObjectHookArg], Any]
Type hint for the object_hook
function signature.
ObjectPairsHookArg
module-attribute
¶
Type hint for the argument of the object_pairs_hook
function.
ObjectPairsHook
module-attribute
¶
ObjectPairsHook = Callable[[ObjectPairsHookArg], Any]
Type hint for the object_pairs_hook
function signature.
Json5Decoder ¶
Json5Decoder(
*,
parse_float: Callable[[str], Any] | None = None,
parse_int: Callable[[str], Any] | None = None,
parse_constant: Callable[[str], Any] | None = None,
strict: bool = True,
allow_reserved_words: bool = True,
object_hook: ObjectHook | None = None,
object_pairs_hook: ObjectPairsHook | None = None,
)
JSON5 decoder
Performs the following translations in decoding by default:
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
It also understands NaN
, Infinity
, and -Infinity
as
their corresponding float
values, which is outside the JSON spec.
Example:
import ujson5
json5_str = '{"key": "value"}'
obj = ujson5.Json5Decoder().decode(json5_str)
# obj == {'key': 'value'}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parse_float
|
Callable[[str], Any] | None
|
if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal). |
None
|
parse_int
|
Callable[[str], Any] | None
|
if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float). |
None
|
parse_constant
|
Callable[[str], Any] | None
|
if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered. |
None
|
strict
|
bool
|
control characters will be allowed inside strings if |
True
|
allow_reserved_words
|
bool
|
if |
True
|
object_hook
|
ObjectHook | None
|
an optional function that will be called with the result of any object
literal decode (a |
None
|
object_pairs_hook
|
ObjectPairsHook | None
|
if specified will be called with the result of every JSON object
decoded with an ordered list of pairs. The return value of |
None
|
Raises:
Type | Description |
---|---|
JSON5DecodeError
|
If the JSON5 string is invalid. |
Source code in src/ujson5/decoder.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
decode ¶
Deserialize a JSON5 string to a Python object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json5_str
|
str
|
The JSON5 string to be deserialized. |
required |
Returns:
Type | Description |
---|---|
Any
|
The Python object represented by the JSON5 string. |
Raises:
Type | Description |
---|---|
JSON5DecodeError
|
If the JSON5 string is invalid. |
Source code in src/ujson5/decoder.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
raw_decode ¶
Deserialize a JSON5 string to a Python object and return the index of the last character parsed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json5_str
|
str
|
The JSON5 string to be deserialized. |
required |
Returns:
Type | Description |
---|---|
tuple[Any, int]
|
A tuple of the Python object represented by the JSON5 string and the index of the last character parsed. |
Raises:
Type | Description |
---|---|
JSON5DecodeError
|
If the JSON5 string is invalid. |
Source code in src/ujson5/decoder.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
load ¶
load(
input_file: TextIO,
*,
cls: type[Json5Decoder] | None = None,
parse_float: Callable[[str], Any] | None = None,
parse_int: Callable[[str], Any] | None = None,
parse_constant: Callable[[str], Any] | None = None,
strict: bool = True,
allow_reserved_words: bool = True,
object_hook: ObjectHook | None = None,
object_pairs_hook: ObjectPairsHook | None = None,
) -> Any
Deserialize fp
(a .read()
-supporting file-like object containing
a JSON document) to a Python object.
Example:
import ujson5
with open('file.json5', 'r') as f:
obj = ujson5.load(f)
All arguments except file
are keyword-only.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
A file-like object containing a JSON document. |
required | |
cls
|
type[Json5Decoder] | None
|
If specified, must be a |
None
|
parse_float
|
Callable[[str], Any] | None
|
if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal). |
None
|
parse_int
|
Callable[[str], Any] | None
|
if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float). |
None
|
parse_constant
|
Callable[[str], Any] | None
|
if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered. |
None
|
strict
|
bool
|
control characters will be allowed inside strings if |
True
|
allow_reserved_words
|
bool
|
if |
True
|
object_hook
|
ObjectHook | None
|
an optional function that will be called with the result of any object
literal decode (a |
None
|
object_pairs_hook
|
ObjectPairsHook | None
|
if specified will be called with the result of every JSON object
decoded with an ordered list of pairs. The return value of |
None
|
Source code in src/ujson5/decoder.py
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 |
|
loads ¶
loads(
json5_str: str,
*,
cls: type[Json5Decoder] | None = None,
parse_float: Callable[[str], Any] | None = None,
parse_int: Callable[[str], Any] | None = None,
parse_constant: Callable[[str], Any] | None = None,
strict: bool = True,
allow_reserved_words: bool = True,
object_hook: ObjectHook | None = None,
object_pairs_hook: ObjectPairsHook | None = None,
) -> Any
Deserialize json5_str
(a str
, bytes
or bytearray
instance
containing a JSON document) to a Python object.
Example:
import ujson5
json5_str = '{"key": "value"}'
obj = ujson5.loads(json5_str)
# obj == {'key': 'value'}
All arguments except json5_str
are keyword-only.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json5_str
|
str
|
The JSON5 string to be deserialized. |
required |
cls
|
type[Json5Decoder] | None
|
If specified, must be a |
None
|
parse_float
|
Callable[[str], Any] | None
|
if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal). |
None
|
parse_int
|
Callable[[str], Any] | None
|
if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float). |
None
|
parse_constant
|
Callable[[str], Any] | None
|
if specified, will be called with one of the following strings: '-Infinity', 'Infinity', 'NaN'. This can be used to raise an exception if invalid JSON numbers are encountered. |
None
|
strict
|
bool
|
control characters will be allowed inside strings if |
True
|
allow_reserved_words
|
bool
|
if |
True
|
object_hook
|
ObjectHook | None
|
an optional function that will be called with the result of any object
literal decode (a |
None
|
object_pairs_hook
|
ObjectPairsHook | None
|
if specified will be called with the result of every JSON object
decoded with an ordered list of pairs. The return value of |
None
|
Source code in src/ujson5/decoder.py
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 |
|