jsonbourne
jsonbourne.core.JsonModule()
⚓︎
JSON class meant to mimic the js/ts-JSON
Source code in libs/jsonbourne/src/jsonbourne/core.py
1165 |
|
Functions⚓︎
jsonbourne.core.JsonModule.binify(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> bytes
staticmethod
⚓︎
Return JSON string bytes for given data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 |
|
jsonbourne.core.JsonModule.dumpb(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> bytes
staticmethod
⚓︎
Return JSON string bytes for given data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 |
|
jsonbourne.core.JsonModule.dumps(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
staticmethod
⚓︎
Return JSON stringified/dumps-ed data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 |
|
jsonbourne.core.JsonModule.json_lib() -> str
staticmethod
⚓︎
Return the name of the JSON library being used as a backend
Source code in libs/jsonbourne/src/jsonbourne/core.py
1384 1385 1386 1387 |
|
jsonbourne.core.JsonModule.jsonify(value: Any) -> Any
staticmethod
⚓︎
Alias for jsonbourne.core.jsonify
Source code in libs/jsonbourne/src/jsonbourne/core.py
1389 1390 1391 1392 |
|
jsonbourne.core.JsonModule.loads(string: Union[bytes, str], obj: bool = False, jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎
Parse JSON string/bytes and return raw representation
Source code in libs/jsonbourne/src/jsonbourne/core.py
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
|
jsonbourne.core.JsonModule.parse(string: Union[bytes, str], obj: bool = False, jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎
Parse JSON string/bytes
Source code in libs/jsonbourne/src/jsonbourne/core.py
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 |
|
jsonbourne.core.JsonModule.rjson(fspath: Union[Path, str], jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎
Read JSON file and return raw representation
Source code in libs/jsonbourne/src/jsonbourne/core.py
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 |
|
jsonbourne.core.JsonModule.stringify(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
staticmethod
⚓︎
Return JSON stringified/dumps-ed data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 |
|
jsonbourne.core.JsonModule.unjsonify(value: Any) -> Any
staticmethod
⚓︎
Alias for jsonbourne.core.unjsonify
Source code in libs/jsonbourne/src/jsonbourne/core.py
1394 1395 1396 1397 |
|
jsonbourne.core.JsonModule.which() -> str
staticmethod
⚓︎
Return the name of the JSON library being used as a backend
Source code in libs/jsonbourne/src/jsonbourne/core.py
1379 1380 1381 1382 |
|
jsonbourne.core.JsonModule.wjson(fspath: Union[Path, str], data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> int
staticmethod
⚓︎
Write JSON file
Source code in libs/jsonbourne/src/jsonbourne/core.py
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 |
|
jsonbourne
⚓︎
jsonbourne the best undercover json lib
Dynamic Graphics Python
Classes⚓︎
jsonbourne.JsonDict(*args: Any, **kwargs: _VT)
⚓︎
JsonDict(*args: Dict[_KT, _VT])
JsonDict(*args: Dict[_KT, _VT], **kwargs: _VT)
JsonDict(*args: Mapping[_KT, _VT])
JsonDict(*args: Mapping[_KT, _VT], **kwargs: _VT)
Bases: JsonObj[_VT]
, Generic[_VT]
Alias for JsonObj
Source code in libs/jsonbourne/src/jsonbourne/core.py
242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
Functions⚓︎
jsonbourne.JsonDict.JSON(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎
Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
|
jsonbourne.JsonDict.__contains__(key: _KT) -> bool
⚓︎
Check if a key or dot-key is contained within the JsonObj object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
root level key or a dot-key |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the key/dot-key is in the JsonObj; False otherwise |
Examples:
>>> d = {"uno": 1, "dos": 2, "tres": 3, "sub": {"a": 1, "b": 2, "c": [3, 4, 5, 6], "d": "a_string"}}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3, 'sub': {'a': 1, 'b': 2, 'c': [3, 4, 5, 6], 'd': 'a_string'}})
>>> 'uno' in d
True
>>> 'this_key_is_not_in_d' in d
False
>>> 'sub.a' in d
True
>>> 'sub.d.a' in d
False
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.__get_type_validator__(source_type: Any, handler: GetCoreSchemaHandler) -> Iterator[Callable[[Any], Any]]
classmethod
⚓︎
Return the JsonObj validator functions
Source code in libs/jsonbourne/src/jsonbourne/core.py
1067 1068 1069 1070 1071 1072 |
|
jsonbourne.JsonDict.__getattr__(item: _KT) -> Any
⚓︎
Return an attr
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> d.__getattr__('b')
2
>>> d.b
2
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.__post_init__() -> Any
⚓︎
Function place holder that is called after object initialization
Source code in libs/jsonbourne/src/jsonbourne/core.py
263 264 |
|
jsonbourne.JsonDict.__repr__() -> str
⚓︎
Return the string representation of the object
Source code in libs/jsonbourne/src/jsonbourne/core.py
788 789 790 |
|
jsonbourne.JsonDict.__setitem__(key: _KT, value: _VT) -> None
⚓︎
Set JsonObj item with 'key' to 'value'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
Key/item to set |
required |
value
|
_VT
|
Value to set |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
If given a key that is not a valid python keyword/identifier |
Examples:
>>> d = JsonObj()
>>> d.a = 123
>>> d['b'] = 321
>>> d
JsonObj(**{'a': 123, 'b': 321})
>>> d[123] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a'})
>>> d['456'] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a', '456': 'a'})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.__str__() -> str
⚓︎
Return the string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
792 793 794 |
|
jsonbourne.JsonDict.asdict() -> Dict[_KT, Any]
⚓︎
Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
893 894 895 |
|
jsonbourne.JsonDict.dot_items() -> Iterator[Tuple[Tuple[str, ...], _VT]]
⚓︎
Yield tuples of the form (dot-key, value)
OG-version
def dot_items(self) -> Iterator[Tuple[str, Any]]: return ((dk, self.dot_lookup(dk)) for dk in self.dot_keys())
Readable-version
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj) or hasattr(value, 'dot_items'): yield from ((f"{k}.{dk}", dv) for dk, dv in value.dot_items()) else: yield k, value
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.dot_items_list() -> List[Tuple[Tuple[str, ...], Any]]
⚓︎
Return list of tuples of the form (dot-key, value)
Source code in libs/jsonbourne/src/jsonbourne/core.py
762 763 764 |
|
jsonbourne.JsonDict.dot_keys() -> Iterable[Tuple[str, ...]]
⚓︎
Yield the JsonObj's dot-notation keys
Returns:
Type | Description |
---|---|
Iterable[Tuple[str, ...]]
|
Iterable[str]: List of the dot-notation friendly keys |
The Non-chain version (shown below) is very slightly slower than the
itertools.chain
version.
NON-CHAIN VERSION:
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj): yield from (f"{k}.{dk}" for dk in value.dot_keys()) else: yield k
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.dot_keys_list(sort_keys: bool = False) -> List[Tuple[str, ...]]
⚓︎
Return a list of the JsonObj's dot-notation friendly keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sort_keys
|
bool
|
Flag to have the dot-keys be returned sorted |
False
|
Returns:
Type | Description |
---|---|
List[Tuple[str, ...]]
|
List[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
660 661 662 663 664 665 666 667 668 669 670 671 672 |
|
jsonbourne.JsonDict.dot_keys_set() -> Set[Tuple[str, ...]]
⚓︎
Return a set of the JsonObj's dot-notation friendly keys
Returns:
Type | Description |
---|---|
Set[Tuple[str, ...]]
|
Set[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
674 675 676 677 678 679 680 681 |
|
jsonbourne.JsonDict.dot_lookup(key: Union[str, Tuple[str, ...], List[str]]) -> Any
⚓︎
Look up JsonObj keys using dot notation as a string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
dot-notation key to look up ('key1.key2.third_key') |
required |
Returns:
Type | Description |
---|---|
Any
|
The result of the dot-notation key look up |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the dot-key is not in in the object |
ValueError
|
Raised if key is not a str/Tuple[str, ...]/List[str] |
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.eject() -> Dict[_KT, _VT]
⚓︎
Eject to python-builtin dictionary object
Examples:
>>> d = JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> d
JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> plain_ol_dict = d.eject()
>>> plain_ol_dict
{'uno': 'ONE', 'tres': 3, 'dos': 2}
>>> type(plain_ol_dict)
<class 'dict'>
Source code in libs/jsonbourne/src/jsonbourne/core.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
|
jsonbourne.JsonDict.entries() -> ItemsView[_KT, _VT]
⚓︎
Alias for items
Source code in libs/jsonbourne/src/jsonbourne/core.py
434 435 436 |
|
jsonbourne.JsonDict.filter_false(recursive: bool = False) -> JsonObj[_VT]
⚓︎
Filter key-values where the value is false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recurse into sub JsonObjs and dictionaries |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_false())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False}
})
>>> print(d.filter_false(recursive=True))
JsonObj(**{
'b': 2, 'c': {'d': 'herm'}
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.filter_none(recursive: bool = False) -> JsonObj[_VT]
⚓︎
Filter key-values where the value is None
but not false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recursively filter out None values |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered of None values |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_none())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> from pprint import pprint
>>> print(d.filter_none(recursive=True))
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonDict.from_dict(data: Dict[_KT, _VT]) -> JsonObj[_VT]
classmethod
⚓︎
Return a JsonObj object from a dictionary of data
Source code in libs/jsonbourne/src/jsonbourne/core.py
897 898 899 900 |
|
jsonbourne.JsonDict.from_json(json_string: Union[bytes, str]) -> JsonObj[_VT]
classmethod
⚓︎
Return a JsonObj object from a json string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_string
|
str
|
JSON string to convert to a JsonObj |
required |
Returns:
Name | Type | Description |
---|---|---|
JsonObjT |
JsonObj[_VT]
|
JsonObj object for the given JSON string |
Source code in libs/jsonbourne/src/jsonbourne/core.py
902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
|
jsonbourne.JsonDict.items() -> ItemsView[_KT, _VT]
⚓︎
Return an items view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
430 431 432 |
|
jsonbourne.JsonDict.keys() -> KeysView[_KT]
⚓︎
Return the keys view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
438 439 440 |
|
jsonbourne.JsonDict.recurse() -> None
⚓︎
Recursively convert all sub dictionaries to JsonObj objects
Source code in libs/jsonbourne/src/jsonbourne/core.py
256 257 258 |
|
jsonbourne.JsonDict.stringify(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎
Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
|
jsonbourne.JsonDict.to_dict() -> Dict[_KT, Any]
⚓︎
Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
889 890 891 |
|
jsonbourne.JsonDict.to_json(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎
Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 |
|
jsonbourne.JsonDict.to_str(minify: bool = False, width: Optional[int] = None) -> str
⚓︎
Return a string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
jsonbourne.JsonDict.validate_type(val: Any) -> JsonObj[_VT]
classmethod
⚓︎
Validate and convert a value to a JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
1062 1063 1064 1065 |
|
jsonbourne.JsonObj(*args: Any, **kwargs: _VT)
⚓︎
JsonObj(*args: Dict[_KT, _VT])
JsonObj(*args: Dict[_KT, _VT], **kwargs: _VT)
JsonObj(*args: Mapping[_KT, _VT])
JsonObj(*args: Mapping[_KT, _VT], **kwargs: _VT)
Bases: MutableMapping[str, _VT]
, Generic[_VT]
JSON friendly python dictionary with dot notation and string only keys
JsonObj(foo='bar')['foo'] == JsonObj(foo='bar').foo
Examples:
>>> print(JsonObj())
JsonObj(**{})
>>> d = {"uno": 1, "dos": 2, "tres": 3}
>>> d
{'uno': 1, 'dos': 2, 'tres': 3}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3})
>>> list(d.keys())
['uno', 'dos', 'tres']
>>> list(d.dot_keys())
[('uno',), ('dos',), ('tres',)]
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3})
>>> d['uno']
1
>>> d.uno
1
>>> d['uno'] == d.uno
True
>>> d.uno = "ONE"
>>> d
JsonObj(**{'uno': 'ONE', 'dos': 2, 'tres': 3})
>>> d['uno'] == d.uno
True
>>> 'uno' in d
True
>>> 'not_in_d' in d
False
>>> d
JsonObj(**{'uno': 'ONE', 'dos': 2, 'tres': 3})
>>> del d['dos']
>>> d
JsonObj(**{'uno': 'ONE', 'tres': 3})
>>> d.tres
3
>>> del d.tres
>>> d
JsonObj(**{'uno': 'ONE'})
>>> d = {"uno": 1, "dos": 2, "tres": {"a": 1, "b": [3, 4, 5, 6]}}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': {'a': 1, 'b': [3, 4, 5, 6]}})
>>> d.tres
JsonObj(**{'a': 1, 'b': [3, 4, 5, 6]})
>>> d.tres.a
1
>>> d.tres.a = "new-val"
>>> d.tres.a
'new-val'
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': {'a': 'new-val', 'b': [3, 4, 5, 6]}})
>>> jd = JsonObj({"a":1, "b": 'herm', 'alist':[{'sub': 123}]})
It does lists!? oh my
>>> jd
JsonObj(**{'a': 1, 'b': 'herm', 'alist': [{'sub': 123}]})
>>> jd.alist[0]
JsonObj(**{'sub': 123})
>>> jd.eject()
{'a': 1, 'b': 'herm', 'alist': [{'sub': 123}]}
Source code in libs/jsonbourne/src/jsonbourne/core.py
242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
Functions⚓︎
jsonbourne.JsonObj.JSON(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎
Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
|
jsonbourne.JsonObj.__contains__(key: _KT) -> bool
⚓︎
Check if a key or dot-key is contained within the JsonObj object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
root level key or a dot-key |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the key/dot-key is in the JsonObj; False otherwise |
Examples:
>>> d = {"uno": 1, "dos": 2, "tres": 3, "sub": {"a": 1, "b": 2, "c": [3, 4, 5, 6], "d": "a_string"}}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3, 'sub': {'a': 1, 'b': 2, 'c': [3, 4, 5, 6], 'd': 'a_string'}})
>>> 'uno' in d
True
>>> 'this_key_is_not_in_d' in d
False
>>> 'sub.a' in d
True
>>> 'sub.d.a' in d
False
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.__get_type_validator__(source_type: Any, handler: GetCoreSchemaHandler) -> Iterator[Callable[[Any], Any]]
classmethod
⚓︎
Return the JsonObj validator functions
Source code in libs/jsonbourne/src/jsonbourne/core.py
1067 1068 1069 1070 1071 1072 |
|
jsonbourne.JsonObj.__getattr__(item: _KT) -> Any
⚓︎
Return an attr
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> d.__getattr__('b')
2
>>> d.b
2
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.__post_init__() -> Any
⚓︎
Function place holder that is called after object initialization
Source code in libs/jsonbourne/src/jsonbourne/core.py
263 264 |
|
jsonbourne.JsonObj.__repr__() -> str
⚓︎
Return the string representation of the object
Source code in libs/jsonbourne/src/jsonbourne/core.py
788 789 790 |
|
jsonbourne.JsonObj.__setitem__(key: _KT, value: _VT) -> None
⚓︎
Set JsonObj item with 'key' to 'value'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
Key/item to set |
required |
value
|
_VT
|
Value to set |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
If given a key that is not a valid python keyword/identifier |
Examples:
>>> d = JsonObj()
>>> d.a = 123
>>> d['b'] = 321
>>> d
JsonObj(**{'a': 123, 'b': 321})
>>> d[123] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a'})
>>> d['456'] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a', '456': 'a'})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.__str__() -> str
⚓︎
Return the string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
792 793 794 |
|
jsonbourne.JsonObj.asdict() -> Dict[_KT, Any]
⚓︎
Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
893 894 895 |
|
jsonbourne.JsonObj.dot_items() -> Iterator[Tuple[Tuple[str, ...], _VT]]
⚓︎
Yield tuples of the form (dot-key, value)
OG-version
def dot_items(self) -> Iterator[Tuple[str, Any]]: return ((dk, self.dot_lookup(dk)) for dk in self.dot_keys())
Readable-version
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj) or hasattr(value, 'dot_items'): yield from ((f"{k}.{dk}", dv) for dk, dv in value.dot_items()) else: yield k, value
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.dot_items_list() -> List[Tuple[Tuple[str, ...], Any]]
⚓︎
Return list of tuples of the form (dot-key, value)
Source code in libs/jsonbourne/src/jsonbourne/core.py
762 763 764 |
|
jsonbourne.JsonObj.dot_keys() -> Iterable[Tuple[str, ...]]
⚓︎
Yield the JsonObj's dot-notation keys
Returns:
Type | Description |
---|---|
Iterable[Tuple[str, ...]]
|
Iterable[str]: List of the dot-notation friendly keys |
The Non-chain version (shown below) is very slightly slower than the
itertools.chain
version.
NON-CHAIN VERSION:
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj): yield from (f"{k}.{dk}" for dk in value.dot_keys()) else: yield k
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.dot_keys_list(sort_keys: bool = False) -> List[Tuple[str, ...]]
⚓︎
Return a list of the JsonObj's dot-notation friendly keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sort_keys
|
bool
|
Flag to have the dot-keys be returned sorted |
False
|
Returns:
Type | Description |
---|---|
List[Tuple[str, ...]]
|
List[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
660 661 662 663 664 665 666 667 668 669 670 671 672 |
|
jsonbourne.JsonObj.dot_keys_set() -> Set[Tuple[str, ...]]
⚓︎
Return a set of the JsonObj's dot-notation friendly keys
Returns:
Type | Description |
---|---|
Set[Tuple[str, ...]]
|
Set[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
674 675 676 677 678 679 680 681 |
|
jsonbourne.JsonObj.dot_lookup(key: Union[str, Tuple[str, ...], List[str]]) -> Any
⚓︎
Look up JsonObj keys using dot notation as a string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
dot-notation key to look up ('key1.key2.third_key') |
required |
Returns:
Type | Description |
---|---|
Any
|
The result of the dot-notation key look up |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the dot-key is not in in the object |
ValueError
|
Raised if key is not a str/Tuple[str, ...]/List[str] |
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.eject() -> Dict[_KT, _VT]
⚓︎
Eject to python-builtin dictionary object
Examples:
>>> d = JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> d
JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> plain_ol_dict = d.eject()
>>> plain_ol_dict
{'uno': 'ONE', 'tres': 3, 'dos': 2}
>>> type(plain_ol_dict)
<class 'dict'>
Source code in libs/jsonbourne/src/jsonbourne/core.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
|
jsonbourne.JsonObj.entries() -> ItemsView[_KT, _VT]
⚓︎
Alias for items
Source code in libs/jsonbourne/src/jsonbourne/core.py
434 435 436 |
|
jsonbourne.JsonObj.filter_false(recursive: bool = False) -> JsonObj[_VT]
⚓︎
Filter key-values where the value is false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recurse into sub JsonObjs and dictionaries |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_false())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False}
})
>>> print(d.filter_false(recursive=True))
JsonObj(**{
'b': 2, 'c': {'d': 'herm'}
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.filter_none(recursive: bool = False) -> JsonObj[_VT]
⚓︎
Filter key-values where the value is None
but not false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recursively filter out None values |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered of None values |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_none())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> from pprint import pprint
>>> print(d.filter_none(recursive=True))
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.JsonObj.from_dict(data: Dict[_KT, _VT]) -> JsonObj[_VT]
classmethod
⚓︎
Return a JsonObj object from a dictionary of data
Source code in libs/jsonbourne/src/jsonbourne/core.py
897 898 899 900 |
|
jsonbourne.JsonObj.from_json(json_string: Union[bytes, str]) -> JsonObj[_VT]
classmethod
⚓︎
Return a JsonObj object from a json string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_string
|
str
|
JSON string to convert to a JsonObj |
required |
Returns:
Name | Type | Description |
---|---|---|
JsonObjT |
JsonObj[_VT]
|
JsonObj object for the given JSON string |
Source code in libs/jsonbourne/src/jsonbourne/core.py
902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
|
jsonbourne.JsonObj.items() -> ItemsView[_KT, _VT]
⚓︎
Return an items view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
430 431 432 |
|
jsonbourne.JsonObj.keys() -> KeysView[_KT]
⚓︎
Return the keys view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
438 439 440 |
|
jsonbourne.JsonObj.recurse() -> None
⚓︎
Recursively convert all sub dictionaries to JsonObj objects
Source code in libs/jsonbourne/src/jsonbourne/core.py
256 257 258 |
|
jsonbourne.JsonObj.stringify(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎
Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
|
jsonbourne.JsonObj.to_dict() -> Dict[_KT, Any]
⚓︎
Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
889 890 891 |
|
jsonbourne.JsonObj.to_json(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎
Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 |
|
jsonbourne.JsonObj.to_str(minify: bool = False, width: Optional[int] = None) -> str
⚓︎
Return a string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
jsonbourne.JsonObj.validate_type(val: Any) -> JsonObj[_VT]
classmethod
⚓︎
Validate and convert a value to a JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
1062 1063 1064 1065 |
|
Functions⚓︎
jsonbourne.rm_js_comments(string: str) -> str
⚓︎
Rejects/regex that removes js/ts/json style comments
Source (stackoverflow): https://stackoverflow.com/a/18381470
Source code in libs/jsonbourne/src/jsonbourne/helpers.py
23 24 25 26 27 28 29 30 31 32 33 |
|
Modules⚓︎
jsonbourne.__about__
⚓︎
Package metadata/info
jsonbourne.__main__
⚓︎
pkg entry ~ python -m jsonbourne
Functions⚓︎
jsonbourne.__main__.main() -> None
⚓︎
Print package metadata
Source code in libs/jsonbourne/src/jsonbourne/__main__.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
jsonbourne.core
⚓︎
Json Bourne -- EZ-PZ-JSON with lots o goodies
Classes⚓︎
jsonbourne.core.JSONMeta
⚓︎
Bases: type
Meta type for use by JSON class to allow for static __call__
method
jsonbourne.core.JSONModuleCls()
⚓︎
Bases: ModuleType
, JsonModule
Source code in libs/jsonbourne/src/jsonbourne/core.py
1165 |
|
jsonbourne.core.JSONModuleCls.__call__(value: Any = None)
staticmethod
⚓︎Jsonify a value
Source code in libs/jsonbourne/src/jsonbourne/core.py
1406 1407 1408 1409 1410 1411 |
|
jsonbourne.core.JSONModuleCls.binify(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> bytes
staticmethod
⚓︎Return JSON string bytes for given data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 |
|
jsonbourne.core.JSONModuleCls.dumpb(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> bytes
staticmethod
⚓︎Return JSON string bytes for given data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 |
|
jsonbourne.core.JSONModuleCls.dumps(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
staticmethod
⚓︎Return JSON stringified/dumps-ed data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 |
|
jsonbourne.core.JSONModuleCls.json_lib() -> str
staticmethod
⚓︎Return the name of the JSON library being used as a backend
Source code in libs/jsonbourne/src/jsonbourne/core.py
1384 1385 1386 1387 |
|
jsonbourne.core.JSONModuleCls.jsonify(value: Any) -> Any
staticmethod
⚓︎Alias for jsonbourne.core.jsonify
Source code in libs/jsonbourne/src/jsonbourne/core.py
1389 1390 1391 1392 |
|
jsonbourne.core.JSONModuleCls.loads(string: Union[bytes, str], obj: bool = False, jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎Parse JSON string/bytes and return raw representation
Source code in libs/jsonbourne/src/jsonbourne/core.py
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
|
jsonbourne.core.JSONModuleCls.parse(string: Union[bytes, str], obj: bool = False, jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎Parse JSON string/bytes
Source code in libs/jsonbourne/src/jsonbourne/core.py
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 |
|
jsonbourne.core.JSONModuleCls.rjson(fspath: Union[Path, str], jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎Read JSON file and return raw representation
Source code in libs/jsonbourne/src/jsonbourne/core.py
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 |
|
jsonbourne.core.JSONModuleCls.stringify(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
staticmethod
⚓︎Return JSON stringified/dumps-ed data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 |
|
jsonbourne.core.JSONModuleCls.unjsonify(value: Any) -> Any
staticmethod
⚓︎Alias for jsonbourne.core.unjsonify
Source code in libs/jsonbourne/src/jsonbourne/core.py
1394 1395 1396 1397 |
|
jsonbourne.core.JSONModuleCls.which() -> str
staticmethod
⚓︎Return the name of the JSON library being used as a backend
Source code in libs/jsonbourne/src/jsonbourne/core.py
1379 1380 1381 1382 |
|
jsonbourne.core.JSONModuleCls.wjson(fspath: Union[Path, str], data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> int
staticmethod
⚓︎Write JSON file
Source code in libs/jsonbourne/src/jsonbourne/core.py
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 |
|
jsonbourne.core.JsonDict(*args: Any, **kwargs: _VT)
⚓︎
JsonDict(*args: Dict[_KT, _VT])
JsonDict(*args: Dict[_KT, _VT], **kwargs: _VT)
JsonDict(*args: Mapping[_KT, _VT])
JsonDict(*args: Mapping[_KT, _VT], **kwargs: _VT)
Bases: JsonObj[_VT]
, Generic[_VT]
Alias for JsonObj
Source code in libs/jsonbourne/src/jsonbourne/core.py
242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
jsonbourne.core.JsonDict.JSON(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
|
jsonbourne.core.JsonDict.__contains__(key: _KT) -> bool
⚓︎Check if a key or dot-key is contained within the JsonObj object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
root level key or a dot-key |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the key/dot-key is in the JsonObj; False otherwise |
Examples:
>>> d = {"uno": 1, "dos": 2, "tres": 3, "sub": {"a": 1, "b": 2, "c": [3, 4, 5, 6], "d": "a_string"}}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3, 'sub': {'a': 1, 'b': 2, 'c': [3, 4, 5, 6], 'd': 'a_string'}})
>>> 'uno' in d
True
>>> 'this_key_is_not_in_d' in d
False
>>> 'sub.a' in d
True
>>> 'sub.d.a' in d
False
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.__get_type_validator__(source_type: Any, handler: GetCoreSchemaHandler) -> Iterator[Callable[[Any], Any]]
classmethod
⚓︎Return the JsonObj validator functions
Source code in libs/jsonbourne/src/jsonbourne/core.py
1067 1068 1069 1070 1071 1072 |
|
jsonbourne.core.JsonDict.__getattr__(item: _KT) -> Any
⚓︎Return an attr
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> d.__getattr__('b')
2
>>> d.b
2
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.__post_init__() -> Any
⚓︎Function place holder that is called after object initialization
Source code in libs/jsonbourne/src/jsonbourne/core.py
263 264 |
|
jsonbourne.core.JsonDict.__repr__() -> str
⚓︎Return the string representation of the object
Source code in libs/jsonbourne/src/jsonbourne/core.py
788 789 790 |
|
jsonbourne.core.JsonDict.__setitem__(key: _KT, value: _VT) -> None
⚓︎Set JsonObj item with 'key' to 'value'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
Key/item to set |
required |
value
|
_VT
|
Value to set |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
If given a key that is not a valid python keyword/identifier |
Examples:
>>> d = JsonObj()
>>> d.a = 123
>>> d['b'] = 321
>>> d
JsonObj(**{'a': 123, 'b': 321})
>>> d[123] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a'})
>>> d['456'] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a', '456': 'a'})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.__str__() -> str
⚓︎Return the string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
792 793 794 |
|
jsonbourne.core.JsonDict.asdict() -> Dict[_KT, Any]
⚓︎Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
893 894 895 |
|
jsonbourne.core.JsonDict.dot_items() -> Iterator[Tuple[Tuple[str, ...], _VT]]
⚓︎Yield tuples of the form (dot-key, value)
OG-version
def dot_items(self) -> Iterator[Tuple[str, Any]]: return ((dk, self.dot_lookup(dk)) for dk in self.dot_keys())
Readable-version
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj) or hasattr(value, 'dot_items'): yield from ((f"{k}.{dk}", dv) for dk, dv in value.dot_items()) else: yield k, value
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.dot_items_list() -> List[Tuple[Tuple[str, ...], Any]]
⚓︎Return list of tuples of the form (dot-key, value)
Source code in libs/jsonbourne/src/jsonbourne/core.py
762 763 764 |
|
jsonbourne.core.JsonDict.dot_keys() -> Iterable[Tuple[str, ...]]
⚓︎Yield the JsonObj's dot-notation keys
Returns:
Type | Description |
---|---|
Iterable[Tuple[str, ...]]
|
Iterable[str]: List of the dot-notation friendly keys |
The Non-chain version (shown below) is very slightly slower than the
itertools.chain
version.
NON-CHAIN VERSION:
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj): yield from (f"{k}.{dk}" for dk in value.dot_keys()) else: yield k
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.dot_keys_list(sort_keys: bool = False) -> List[Tuple[str, ...]]
⚓︎Return a list of the JsonObj's dot-notation friendly keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sort_keys
|
bool
|
Flag to have the dot-keys be returned sorted |
False
|
Returns:
Type | Description |
---|---|
List[Tuple[str, ...]]
|
List[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
660 661 662 663 664 665 666 667 668 669 670 671 672 |
|
jsonbourne.core.JsonDict.dot_keys_set() -> Set[Tuple[str, ...]]
⚓︎Return a set of the JsonObj's dot-notation friendly keys
Returns:
Type | Description |
---|---|
Set[Tuple[str, ...]]
|
Set[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
674 675 676 677 678 679 680 681 |
|
jsonbourne.core.JsonDict.dot_lookup(key: Union[str, Tuple[str, ...], List[str]]) -> Any
⚓︎Look up JsonObj keys using dot notation as a string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
dot-notation key to look up ('key1.key2.third_key') |
required |
Returns:
Type | Description |
---|---|
Any
|
The result of the dot-notation key look up |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the dot-key is not in in the object |
ValueError
|
Raised if key is not a str/Tuple[str, ...]/List[str] |
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.eject() -> Dict[_KT, _VT]
⚓︎Eject to python-builtin dictionary object
Examples:
>>> d = JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> d
JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> plain_ol_dict = d.eject()
>>> plain_ol_dict
{'uno': 'ONE', 'tres': 3, 'dos': 2}
>>> type(plain_ol_dict)
<class 'dict'>
Source code in libs/jsonbourne/src/jsonbourne/core.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
|
jsonbourne.core.JsonDict.entries() -> ItemsView[_KT, _VT]
⚓︎Alias for items
Source code in libs/jsonbourne/src/jsonbourne/core.py
434 435 436 |
|
jsonbourne.core.JsonDict.filter_false(recursive: bool = False) -> JsonObj[_VT]
⚓︎Filter key-values where the value is false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recurse into sub JsonObjs and dictionaries |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_false())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False}
})
>>> print(d.filter_false(recursive=True))
JsonObj(**{
'b': 2, 'c': {'d': 'herm'}
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.filter_none(recursive: bool = False) -> JsonObj[_VT]
⚓︎Filter key-values where the value is None
but not false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recursively filter out None values |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered of None values |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_none())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> from pprint import pprint
>>> print(d.filter_none(recursive=True))
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonDict.from_dict(data: Dict[_KT, _VT]) -> JsonObj[_VT]
classmethod
⚓︎Return a JsonObj object from a dictionary of data
Source code in libs/jsonbourne/src/jsonbourne/core.py
897 898 899 900 |
|
jsonbourne.core.JsonDict.from_json(json_string: Union[bytes, str]) -> JsonObj[_VT]
classmethod
⚓︎Return a JsonObj object from a json string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_string
|
str
|
JSON string to convert to a JsonObj |
required |
Returns:
Name | Type | Description |
---|---|---|
JsonObjT |
JsonObj[_VT]
|
JsonObj object for the given JSON string |
Source code in libs/jsonbourne/src/jsonbourne/core.py
902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
|
jsonbourne.core.JsonDict.items() -> ItemsView[_KT, _VT]
⚓︎Return an items view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
430 431 432 |
|
jsonbourne.core.JsonDict.keys() -> KeysView[_KT]
⚓︎Return the keys view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
438 439 440 |
|
jsonbourne.core.JsonDict.recurse() -> None
⚓︎Recursively convert all sub dictionaries to JsonObj objects
Source code in libs/jsonbourne/src/jsonbourne/core.py
256 257 258 |
|
jsonbourne.core.JsonDict.stringify(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
|
jsonbourne.core.JsonDict.to_dict() -> Dict[_KT, Any]
⚓︎Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
889 890 891 |
|
jsonbourne.core.JsonDict.to_json(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 |
|
jsonbourne.core.JsonDict.to_str(minify: bool = False, width: Optional[int] = None) -> str
⚓︎Return a string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
jsonbourne.core.JsonDict.validate_type(val: Any) -> JsonObj[_VT]
classmethod
⚓︎Validate and convert a value to a JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
1062 1063 1064 1065 |
|
jsonbourne.core.JsonModule()
⚓︎
JSON class meant to mimic the js/ts-JSON
Source code in libs/jsonbourne/src/jsonbourne/core.py
1165 |
|
jsonbourne.core.JsonModule.binify(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> bytes
staticmethod
⚓︎Return JSON string bytes for given data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 |
|
jsonbourne.core.JsonModule.dumpb(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> bytes
staticmethod
⚓︎Return JSON string bytes for given data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 |
|
jsonbourne.core.JsonModule.dumps(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
staticmethod
⚓︎Return JSON stringified/dumps-ed data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 |
|
jsonbourne.core.JsonModule.json_lib() -> str
staticmethod
⚓︎Return the name of the JSON library being used as a backend
Source code in libs/jsonbourne/src/jsonbourne/core.py
1384 1385 1386 1387 |
|
jsonbourne.core.JsonModule.jsonify(value: Any) -> Any
staticmethod
⚓︎Alias for jsonbourne.core.jsonify
Source code in libs/jsonbourne/src/jsonbourne/core.py
1389 1390 1391 1392 |
|
jsonbourne.core.JsonModule.loads(string: Union[bytes, str], obj: bool = False, jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎Parse JSON string/bytes and return raw representation
Source code in libs/jsonbourne/src/jsonbourne/core.py
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
|
jsonbourne.core.JsonModule.parse(string: Union[bytes, str], obj: bool = False, jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎Parse JSON string/bytes
Source code in libs/jsonbourne/src/jsonbourne/core.py
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 |
|
jsonbourne.core.JsonModule.rjson(fspath: Union[Path, str], jsonc: bool = False, jsonl: bool = False, ndjson: bool = False, **kwargs: Any) -> Any
staticmethod
⚓︎Read JSON file and return raw representation
Source code in libs/jsonbourne/src/jsonbourne/core.py
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 |
|
jsonbourne.core.JsonModule.stringify(data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
staticmethod
⚓︎Return JSON stringified/dumps-ed data
Source code in libs/jsonbourne/src/jsonbourne/core.py
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 |
|
jsonbourne.core.JsonModule.unjsonify(value: Any) -> Any
staticmethod
⚓︎Alias for jsonbourne.core.unjsonify
Source code in libs/jsonbourne/src/jsonbourne/core.py
1394 1395 1396 1397 |
|
jsonbourne.core.JsonModule.which() -> str
staticmethod
⚓︎Return the name of the JSON library being used as a backend
Source code in libs/jsonbourne/src/jsonbourne/core.py
1379 1380 1381 1382 |
|
jsonbourne.core.JsonModule.wjson(fspath: Union[Path, str], data: Any, fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> int
staticmethod
⚓︎Write JSON file
Source code in libs/jsonbourne/src/jsonbourne/core.py
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 |
|
jsonbourne.core.JsonObj(*args: Any, **kwargs: _VT)
⚓︎
JsonObj(*args: Dict[_KT, _VT])
JsonObj(*args: Dict[_KT, _VT], **kwargs: _VT)
JsonObj(*args: Mapping[_KT, _VT])
JsonObj(*args: Mapping[_KT, _VT], **kwargs: _VT)
Bases: MutableMapping[str, _VT]
, Generic[_VT]
JSON friendly python dictionary with dot notation and string only keys
JsonObj(foo='bar')['foo'] == JsonObj(foo='bar').foo
Examples:
>>> print(JsonObj())
JsonObj(**{})
>>> d = {"uno": 1, "dos": 2, "tres": 3}
>>> d
{'uno': 1, 'dos': 2, 'tres': 3}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3})
>>> list(d.keys())
['uno', 'dos', 'tres']
>>> list(d.dot_keys())
[('uno',), ('dos',), ('tres',)]
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3})
>>> d['uno']
1
>>> d.uno
1
>>> d['uno'] == d.uno
True
>>> d.uno = "ONE"
>>> d
JsonObj(**{'uno': 'ONE', 'dos': 2, 'tres': 3})
>>> d['uno'] == d.uno
True
>>> 'uno' in d
True
>>> 'not_in_d' in d
False
>>> d
JsonObj(**{'uno': 'ONE', 'dos': 2, 'tres': 3})
>>> del d['dos']
>>> d
JsonObj(**{'uno': 'ONE', 'tres': 3})
>>> d.tres
3
>>> del d.tres
>>> d
JsonObj(**{'uno': 'ONE'})
>>> d = {"uno": 1, "dos": 2, "tres": {"a": 1, "b": [3, 4, 5, 6]}}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': {'a': 1, 'b': [3, 4, 5, 6]}})
>>> d.tres
JsonObj(**{'a': 1, 'b': [3, 4, 5, 6]})
>>> d.tres.a
1
>>> d.tres.a = "new-val"
>>> d.tres.a
'new-val'
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': {'a': 'new-val', 'b': [3, 4, 5, 6]}})
>>> jd = JsonObj({"a":1, "b": 'herm', 'alist':[{'sub': 123}]})
It does lists!? oh my
>>> jd
JsonObj(**{'a': 1, 'b': 'herm', 'alist': [{'sub': 123}]})
>>> jd.alist[0]
JsonObj(**{'sub': 123})
>>> jd.eject()
{'a': 1, 'b': 'herm', 'alist': [{'sub': 123}]}
Source code in libs/jsonbourne/src/jsonbourne/core.py
242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
jsonbourne.core.JsonObj.JSON(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
|
jsonbourne.core.JsonObj.__contains__(key: _KT) -> bool
⚓︎Check if a key or dot-key is contained within the JsonObj object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
root level key or a dot-key |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the key/dot-key is in the JsonObj; False otherwise |
Examples:
>>> d = {"uno": 1, "dos": 2, "tres": 3, "sub": {"a": 1, "b": 2, "c": [3, 4, 5, 6], "d": "a_string"}}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3, 'sub': {'a': 1, 'b': 2, 'c': [3, 4, 5, 6], 'd': 'a_string'}})
>>> 'uno' in d
True
>>> 'this_key_is_not_in_d' in d
False
>>> 'sub.a' in d
True
>>> 'sub.d.a' in d
False
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.__get_type_validator__(source_type: Any, handler: GetCoreSchemaHandler) -> Iterator[Callable[[Any], Any]]
classmethod
⚓︎Return the JsonObj validator functions
Source code in libs/jsonbourne/src/jsonbourne/core.py
1067 1068 1069 1070 1071 1072 |
|
jsonbourne.core.JsonObj.__getattr__(item: _KT) -> Any
⚓︎Return an attr
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> d.__getattr__('b')
2
>>> d.b
2
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.__post_init__() -> Any
⚓︎Function place holder that is called after object initialization
Source code in libs/jsonbourne/src/jsonbourne/core.py
263 264 |
|
jsonbourne.core.JsonObj.__repr__() -> str
⚓︎Return the string representation of the object
Source code in libs/jsonbourne/src/jsonbourne/core.py
788 789 790 |
|
jsonbourne.core.JsonObj.__setitem__(key: _KT, value: _VT) -> None
⚓︎Set JsonObj item with 'key' to 'value'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
Key/item to set |
required |
value
|
_VT
|
Value to set |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
If given a key that is not a valid python keyword/identifier |
Examples:
>>> d = JsonObj()
>>> d.a = 123
>>> d['b'] = 321
>>> d
JsonObj(**{'a': 123, 'b': 321})
>>> d[123] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a'})
>>> d['456'] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a', '456': 'a'})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.__str__() -> str
⚓︎Return the string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
792 793 794 |
|
jsonbourne.core.JsonObj.asdict() -> Dict[_KT, Any]
⚓︎Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
893 894 895 |
|
jsonbourne.core.JsonObj.dot_items() -> Iterator[Tuple[Tuple[str, ...], _VT]]
⚓︎Yield tuples of the form (dot-key, value)
OG-version
def dot_items(self) -> Iterator[Tuple[str, Any]]: return ((dk, self.dot_lookup(dk)) for dk in self.dot_keys())
Readable-version
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj) or hasattr(value, 'dot_items'): yield from ((f"{k}.{dk}", dv) for dk, dv in value.dot_items()) else: yield k, value
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.dot_items_list() -> List[Tuple[Tuple[str, ...], Any]]
⚓︎Return list of tuples of the form (dot-key, value)
Source code in libs/jsonbourne/src/jsonbourne/core.py
762 763 764 |
|
jsonbourne.core.JsonObj.dot_keys() -> Iterable[Tuple[str, ...]]
⚓︎Yield the JsonObj's dot-notation keys
Returns:
Type | Description |
---|---|
Iterable[Tuple[str, ...]]
|
Iterable[str]: List of the dot-notation friendly keys |
The Non-chain version (shown below) is very slightly slower than the
itertools.chain
version.
NON-CHAIN VERSION:
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj): yield from (f"{k}.{dk}" for dk in value.dot_keys()) else: yield k
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.dot_keys_list(sort_keys: bool = False) -> List[Tuple[str, ...]]
⚓︎Return a list of the JsonObj's dot-notation friendly keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sort_keys
|
bool
|
Flag to have the dot-keys be returned sorted |
False
|
Returns:
Type | Description |
---|---|
List[Tuple[str, ...]]
|
List[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
660 661 662 663 664 665 666 667 668 669 670 671 672 |
|
jsonbourne.core.JsonObj.dot_keys_set() -> Set[Tuple[str, ...]]
⚓︎Return a set of the JsonObj's dot-notation friendly keys
Returns:
Type | Description |
---|---|
Set[Tuple[str, ...]]
|
Set[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
674 675 676 677 678 679 680 681 |
|
jsonbourne.core.JsonObj.dot_lookup(key: Union[str, Tuple[str, ...], List[str]]) -> Any
⚓︎Look up JsonObj keys using dot notation as a string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
dot-notation key to look up ('key1.key2.third_key') |
required |
Returns:
Type | Description |
---|---|
Any
|
The result of the dot-notation key look up |
Raises:
Type | Description |
---|---|
KeyError
|
Raised if the dot-key is not in in the object |
ValueError
|
Raised if key is not a str/Tuple[str, ...]/List[str] |
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.eject() -> Dict[_KT, _VT]
⚓︎Eject to python-builtin dictionary object
Examples:
>>> d = JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> d
JsonObj(**{'uno': 'ONE', 'tres': 3, 'dos': 2})
>>> plain_ol_dict = d.eject()
>>> plain_ol_dict
{'uno': 'ONE', 'tres': 3, 'dos': 2}
>>> type(plain_ol_dict)
<class 'dict'>
Source code in libs/jsonbourne/src/jsonbourne/core.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
|
jsonbourne.core.JsonObj.entries() -> ItemsView[_KT, _VT]
⚓︎Alias for items
Source code in libs/jsonbourne/src/jsonbourne/core.py
434 435 436 |
|
jsonbourne.core.JsonObj.filter_false(recursive: bool = False) -> JsonObj[_VT]
⚓︎Filter key-values where the value is false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recurse into sub JsonObjs and dictionaries |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_false())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False}
})
>>> print(d.filter_false(recursive=True))
JsonObj(**{
'b': 2, 'c': {'d': 'herm'}
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.filter_none(recursive: bool = False) -> JsonObj[_VT]
⚓︎Filter key-values where the value is None
but not false-y
Parameters:
Name | Type | Description | Default |
---|---|---|---|
recursive
|
bool
|
Recursively filter out None values |
False
|
Returns:
Type | Description |
---|---|
JsonObj[_VT]
|
JsonObj that has been filtered of None values |
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> print(d)
JsonObj(**{
'a': None,
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> print(d.filter_none())
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'e': None,
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
>>> from pprint import pprint
>>> print(d.filter_none(recursive=True))
JsonObj(**{
'b': 2,
'c': {'d': 'herm',
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False},
'falsey_dict': {},
'falsey_list': [],
'falsey_string': '',
'is_false': False
})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.JsonObj.from_dict(data: Dict[_KT, _VT]) -> JsonObj[_VT]
classmethod
⚓︎Return a JsonObj object from a dictionary of data
Source code in libs/jsonbourne/src/jsonbourne/core.py
897 898 899 900 |
|
jsonbourne.core.JsonObj.from_json(json_string: Union[bytes, str]) -> JsonObj[_VT]
classmethod
⚓︎Return a JsonObj object from a json string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_string
|
str
|
JSON string to convert to a JsonObj |
required |
Returns:
Name | Type | Description |
---|---|---|
JsonObjT |
JsonObj[_VT]
|
JsonObj object for the given JSON string |
Source code in libs/jsonbourne/src/jsonbourne/core.py
902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
|
jsonbourne.core.JsonObj.items() -> ItemsView[_KT, _VT]
⚓︎Return an items view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
430 431 432 |
|
jsonbourne.core.JsonObj.keys() -> KeysView[_KT]
⚓︎Return the keys view of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
438 439 440 |
|
jsonbourne.core.JsonObj.recurse() -> None
⚓︎Recursively convert all sub dictionaries to JsonObj objects
Source code in libs/jsonbourne/src/jsonbourne/core.py
256 257 258 |
|
jsonbourne.core.JsonObj.stringify(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
|
jsonbourne.core.JsonObj.to_dict() -> Dict[_KT, Any]
⚓︎Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
889 890 891 |
|
jsonbourne.core.JsonObj.to_json(fmt: bool = False, pretty: bool = False, sort_keys: bool = False, append_newline: bool = False, default: Optional[Callable[[Any], Any]] = None, **kwargs: Any) -> str
⚓︎Return JSON string of the JsonObj object (and children)
Args:
fmt (bool): If True, return a JSON string with newlines and indentation
pretty (bool): If True, return a JSON string with newlines and indentation
sort_keys (bool): Sort dictionary keys if True
append_newline (bool): Append a newline '
' to JSON string if True default: default function hook for JSON serialization **kwargs (Any): additional kwargs to be passed down to jsonlib.dumps
Returns:
str: JSON string of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 |
|
jsonbourne.core.JsonObj.to_str(minify: bool = False, width: Optional[int] = None) -> str
⚓︎Return a string representation of the JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
jsonbourne.core.JsonObj.validate_type(val: Any) -> JsonObj[_VT]
classmethod
⚓︎Validate and convert a value to a JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/core.py
1062 1063 1064 1065 |
|
Functions⚓︎
jsonbourne.core.is_float(value: Any) -> bool
⚓︎
Return True if value is a float
Source code in libs/jsonbourne/src/jsonbourne/core.py
111 112 113 114 115 116 117 118 |
|
jsonbourne.core.is_identifier(string: str) -> bool
⚓︎
Return True if a string is a valid python identifier; False otherwise
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
String (likely to be used as a key) to check |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if is an identifier |
Examples:
>>> is_identifier("herm")
True
>>> is_identifier("something that contains spaces")
False
>>> is_identifier("import")
False
>>> is_identifier("something.with.periods")
False
>>> is_identifier("astring-with-dashes")
False
>>> is_identifier("astring_with_underscores")
True
>>> is_identifier(123)
False
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.core.is_int(value: Any) -> bool
⚓︎
Return True if value is a int
Source code in libs/jsonbourne/src/jsonbourne/core.py
121 122 123 124 125 126 127 128 129 130 |
|
jsonbourne.core.is_number(value: Any) -> bool
⚓︎
Return True if value is a number
Source code in libs/jsonbourne/src/jsonbourne/core.py
133 134 135 |
|
jsonbourne.core.is_pydantic_model(cls: Any) -> bool
⚓︎
Return True if cls is a pydantic model
Source code in libs/jsonbourne/src/jsonbourne/core.py
138 139 140 |
|
jsonbourne.core.jsonify(value: Any) -> Any
⚓︎
jsonify(value: JsonPrimitiveT) -> JsonPrimitiveT
jsonify(
value: List[JsonPrimitiveT],
) -> List[JsonPrimitiveT]
jsonify(
value: Tuple[JsonPrimitiveT, ...],
) -> Tuple[JsonPrimitiveT, ...]
jsonify(value: _VT) -> _VT
Convert and return a value to a JsonObj if the value is a dict
Source code in libs/jsonbourne/src/jsonbourne/core.py
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
|
jsonbourne.core.unjsonify(value: Any) -> Any
⚓︎
Recursively eject a JsonDit object
Source code in libs/jsonbourne/src/jsonbourne/core.py
1127 1128 1129 1130 1131 1132 1133 1134 1135 |
|
Modules⚓︎
jsonbourne.dev
⚓︎
jsonbourne.dev
jsonbourne.fastapi
⚓︎
Classes⚓︎
jsonbourne.fastapi.JSONBOURNEResponse
⚓︎
Bases: Response
FastAPI/starlette json response to auto use jsonbourne
jsonbourne.fastapi.JSONBOURNEResponse.render(content: Any) -> bytes
⚓︎Return JSON string for content as bytes
Source code in libs/jsonbourne/src/jsonbourne/fastapi.py
28 29 30 |
|
jsonbourne.helpers
⚓︎
JSONBourne helper funks and utils
Functions⚓︎
jsonbourne.helpers.rm_js_comments(string: str) -> str
⚓︎
Rejects/regex that removes js/ts/json style comments
Source (stackoverflow): https://stackoverflow.com/a/18381470
Source code in libs/jsonbourne/src/jsonbourne/helpers.py
23 24 25 26 27 28 29 30 31 32 33 |
|
jsonbourne.httpx
⚓︎
Jsonbourne wrapper around httpx clients -- lets you do response.JSON()
jsonbourne.json_arr
⚓︎
Classes⚓︎
jsonbourne.json_arr.JsonArr(iterable: Optional[Iterable[_T]] = None)
⚓︎
Bases: MutableSequence[_T]
, Generic[_T]
Source code in libs/jsonbourne/src/jsonbourne/json_arr.py
69 70 |
|
jsonbourne.json_arr.JsonArr.__get_type_validator__(source_type: Any, handler: GetCoreSchemaHandler) -> Iterator[Callable[[Any], Any]]
classmethod
⚓︎Return the JsonObj validator functions
Source code in libs/jsonbourne/src/jsonbourne/json_arr.py
218 219 220 221 222 223 |
|
jsonbourne.json_arr.JsonArr.__post_init__() -> Any
⚓︎Function place holder that is called after object initialization
Source code in libs/jsonbourne/src/jsonbourne/json_arr.py
72 73 |
|
jsonbourne.json_arr.JsonArr.slice(start: Optional[int] = None, end: Optional[int] = None) -> JsonArr[_T]
⚓︎Return a slice as new jsonarray
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start
|
Optional[int]
|
start index. Defaults to None. |
None
|
end
|
Optional[int]
|
end index. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
JsonArr[_T]
|
JsonArr[_T]: new JsonArr |
Examples:
>>> arr = JsonArr([1, 2, 3, 4, 5])
>>> arr.slice()
JsonArr([1, 2, 3, 4, 5])
>>> arr.slice(1, 3)
JsonArr([2, 3])
Source code in libs/jsonbourne/src/jsonbourne/json_arr.py
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 |
|
jsonbourne.json_arr.JsonArr.validate_type(val: Any) -> JsonArr[_T]
classmethod
⚓︎Validate and convert a value to a JsonObj object
Source code in libs/jsonbourne/src/jsonbourne/json_arr.py
213 214 215 216 |
|
jsonbourne.pydantic
⚓︎
JSONBourne + Pydantic
Classes⚓︎
jsonbourne.pydantic.JsonBaseConfig
⚓︎
Pydantic v1 model config class for JsonBaseModel; can be overridden
jsonbourne.pydantic.JsonBaseModel(*args: Any, **kwargs: _VT)
⚓︎
JsonBaseModel(*args: Dict[_KT, _VT])
JsonBaseModel(*args: Dict[_KT, _VT], **kwargs: _VT)
JsonBaseModel(*args: Mapping[_KT, _VT])
JsonBaseModel(*args: Mapping[_KT, _VT], **kwargs: _VT)
Bases: BaseModel
, JsonObj
, MutableMapping
Hybrid pydantic.BaseModel
and jsonbourne.JsonObj
Source code in libs/jsonbourne/src/jsonbourne/core.py
242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
jsonbourne.pydantic.JsonBaseModel.__property_fields__: Set[str]
property
⚓︎Returns a set of property names for the class that have a setter
jsonbourne.pydantic.JsonBaseModel.__contains__(key: _KT) -> bool
⚓︎Check if a key or dot-key is contained within the JsonObj object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
root level key or a dot-key |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the key/dot-key is in the JsonObj; False otherwise |
Examples:
>>> d = {"uno": 1, "dos": 2, "tres": 3, "sub": {"a": 1, "b": 2, "c": [3, 4, 5, 6], "d": "a_string"}}
>>> d = JsonObj(d)
>>> d
JsonObj(**{'uno': 1, 'dos': 2, 'tres': 3, 'sub': {'a': 1, 'b': 2, 'c': [3, 4, 5, 6], 'd': 'a_string'}})
>>> 'uno' in d
True
>>> 'this_key_is_not_in_d' in d
False
>>> 'sub.a' in d
True
>>> 'sub.d.a' in d
False
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.pydantic.JsonBaseModel.__get_type_validator__(source_type: Any, handler: GetCoreSchemaHandler) -> Iterator[Callable[[Any], Any]]
classmethod
⚓︎Return the JsonObj validator functions
Source code in libs/jsonbourne/src/jsonbourne/core.py
1067 1068 1069 1070 1071 1072 |
|
jsonbourne.pydantic.JsonBaseModel.__getattr__(item: _KT) -> Any
⚓︎Return an attr
Examples:
>>> d = {
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... 'a': None,
... 'b': 2,
... 'c': {
... 'd': 'herm',
... 'e': None,
... 'falsey_dict': {},
... 'falsey_list': [],
... 'falsey_string': '',
... 'is_false': False,
... },
... }
...
>>> d = JsonObj(d)
>>> d.__getattr__('b')
2
>>> d.b
2
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.pydantic.JsonBaseModel.__post_init__() -> Any
⚓︎Function place holder that is called after object initialization
Source code in libs/jsonbourne/src/jsonbourne/pydantic.py
98 99 |
|
jsonbourne.pydantic.JsonBaseModel.__setitem__(key: _KT, value: _VT) -> None
⚓︎Set JsonObj item with 'key' to 'value'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
_KT
|
Key/item to set |
required |
value
|
_VT
|
Value to set |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
If given a key that is not a valid python keyword/identifier |
Examples:
>>> d = JsonObj()
>>> d.a = 123
>>> d['b'] = 321
>>> d
JsonObj(**{'a': 123, 'b': 321})
>>> d[123] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a'})
>>> d['456'] = 'a'
>>> d
JsonObj(**{'a': 123, 'b': 321, '123': 'a', '456': 'a'})
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.pydantic.JsonBaseModel.asdict() -> Dict[_KT, Any]
⚓︎Return the JsonObj object (and children) as a python dictionary
Source code in libs/jsonbourne/src/jsonbourne/core.py
893 894 895 |
|
jsonbourne.pydantic.JsonBaseModel.defaults_dict() -> Dict[str, Any]
classmethod
⚓︎Return a dictionary of non-required keys -> default value(s)
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary of non-required keys -> default value |
Examples:
>>> class Thing(JsonBaseModel):
... a: int = 1
... b: str = "herm"
...
>>> t = Thing()
>>> t
Thing(a=1, b='herm')
>>> t.to_dict_filter_defaults()
{}
>>> t.to_json_obj_filter_defaults()
JsonObj(**{})
>>> t = Thing(a=123)
>>> t
Thing(a=123, b='herm')
>>> t.to_dict_filter_defaults()
{'a': 123}
>>> t.to_json_obj_filter_defaults()
JsonObj(**{'a': 123})
>>> t.defaults_dict()
{'a': 1, 'b': 'herm'}
Source code in libs/jsonbourne/src/jsonbourne/pydantic.py
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 |
|
jsonbourne.pydantic.JsonBaseModel.dict(*args: Any, **kwargs: Any) -> Dict[str, Any]
⚓︎Alias for model_dump
Source code in libs/jsonbourne/src/jsonbourne/pydantic.py
118 119 120 |
|
jsonbourne.pydantic.JsonBaseModel.dot_items() -> Iterator[Tuple[Tuple[str, ...], _VT]]
⚓︎Yield tuples of the form (dot-key, value)
OG-version
def dot_items(self) -> Iterator[Tuple[str, Any]]: return ((dk, self.dot_lookup(dk)) for dk in self.dot_keys())
Readable-version
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj) or hasattr(value, 'dot_items'): yield from ((f"{k}.{dk}", dv) for dk, dv in value.dot_items()) else: yield k, value
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.pydantic.JsonBaseModel.dot_items_list() -> List[Tuple[Tuple[str, ...], Any]]
⚓︎Return list of tuples of the form (dot-key, value)
Source code in libs/jsonbourne/src/jsonbourne/core.py
762 763 764 |
|
jsonbourne.pydantic.JsonBaseModel.dot_keys() -> Iterable[Tuple[str, ...]]
⚓︎Yield the JsonObj's dot-notation keys
Returns:
Type | Description |
---|---|
Iterable[Tuple[str, ...]]
|
Iterable[str]: List of the dot-notation friendly keys |
The Non-chain version (shown below) is very slightly slower than the
itertools.chain
version.
NON-CHAIN VERSION:
for k, value in self.items(): value = jsonify(value) if isinstance(value, JsonObj): yield from (f"{k}.{dk}" for dk in value.dot_keys()) else: yield k
Source code in libs/jsonbourne/src/jsonbourne/core.py
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 |
|
jsonbourne.pydantic.JsonBaseModel.dot_keys_list(sort_keys: bool = False) -> List[Tuple[str, ...]]
⚓︎Return a list of the JsonObj's dot-notation friendly keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sort_keys
|
bool
|
Flag to have the dot-keys be returned sorted |
False
|
Returns:
Type | Description |
---|---|
List[Tuple[str, ...]]
|
List[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
660 661 662 663 664 665 666 667 668 669 670 671 672 |
|
jsonbourne.pydantic.JsonBaseModel.dot_keys_set() -> Set[Tuple[str, ...]]
⚓︎Return a set of the JsonObj's dot-notation friendly keys
Returns:
Type | Description |
---|---|
Set[Tuple[str, ...]]
|
Set[str]: List of the dot-notation friendly keys |
Source code in libs/jsonbourne/src/jsonbourne/core.py
674 675 676 677 678 679 680 681 |
|