sim_explorer.utils.json5¶
Python module for working with json5 files.
Functions
|
|
|
Check whether the dict js5 can be interpreted as Json5. |
|
Find the position of the start of the identifier in txt going backwards from pos. |
|
Evaluate a JsonPath expression on the Json5 code and return the result. |
|
Read the Json5 file. |
|
Try to repair the json5 illegal character found at pos in txt. |
|
Append data to the js5 dict at the path pointed to by keys. |
|
Use pyjson5 to print the json5 code to file, optionally using indenting the code to make it human-readable. |
- sim_explorer.utils.json5.get_pos(txt: str, start: str = 'near ', end: str = ',') int¶
- sim_explorer.utils.json5.json5_check(js5: dict[str, Any]) bool¶
Check whether the dict js5 can be interpreted as Json5. Wrapper function.
- sim_explorer.utils.json5.json5_find_identifier_start(txt: str, pos: int) int¶
Find the position of the start of the identifier in txt going backwards from pos.
- sim_explorer.utils.json5.json5_path(js5: dict[str, Any], path: str, typ: type[_VT]) _VT | None¶
- sim_explorer.utils.json5.json5_path(js5: dict[str, Any], path: str, typ: None = None) Any | None
Evaluate a JsonPath expression on the Json5 code and return the result.
Syntax see RFC9535 and jsonpath-ng (used here)
$: root node identifier (Section 2.2)
@: current node identifier (Section 2.3.5) (valid only within filter selectors)
[<selectors>]: child segment (Section 2.5.1): selects zero or more children of a node
.name: shorthand for [‘name’]
.*: shorthand for [*]
..[<selectors>]: descendant segment (Section 2.5.2): selects zero or more descendants of a node
..name: shorthand for ..[‘name’]
..*: shorthand for ..[*]
‘name’: name selector (Section 2.3.1): selects a named child of an object
*: wildcard selector (Section 2.3.2): selects all children of a node
i: (int) index selector (Section 2.3.3): selects an indexed child of an array (from 0)
0:100:5: array slice selector (Section 2.3.4): start:end:step for arrays
?<logical-expr>: filter selector (Section 2.3.5): selects particular children using a logical expression
length(@.foo): function extension (Section 2.4): invokes a function in a filter expression
- Parameters:
js5 (dict) – a Json5 conformant dict
path (str) – path expression as string.
typ (type) – optional specification of the expected type to find
- sim_explorer.utils.json5.json5_read(file: Path | str, *, save: int = 0) dict[str, Any]¶
Read the Json5 file. If key or comment errors are encountered they are tried fixed ‘en route’. save: 0: do not save, 1: save if changed, 2: save in any case. Overwrite file when saving.
- sim_explorer.utils.json5.json5_try_correct(txt: str, pos: int) tuple[bool, str]¶
Try to repair the json5 illegal character found at pos in txt.
Check whether pos points to a key and set the key in quotation marks.
Check whether pos points to an illegal comment marker and replace with //
- sim_explorer.utils.json5.json5_update(js5: dict[str, Any], *, spath: str, data: dict[str, Any] | list[Any] | Any) None¶
- sim_explorer.utils.json5.json5_update(js5: dict[str, Any], *, keys: Sequence[str], data: dict[str, Any] | list[Any] | Any) None
Append data to the js5 dict at the path pointed to by keys. So far this is a minimum implementation for adding data.
- Parameters:
js5 (dict[str, Any]) – A Json5 conformant dict
spath (str) – A JsonPath expression as string. If provided, the keys are extracted from the spath and used to update the dict. If not provided, the keys argument is used.
keys (Sequence[str]) – Sequence of keys. All keys down to the place where to update the dict shall be included
data (dict[str, Any] | list[Any] | Any) – the data to be added/updated. Dicts are updated, lists are appended
- sim_explorer.utils.json5.json5_write(js5: dict[str, Any], file: Path | str, *, indent: int = 3, compact: bool = True) None¶
Use pyjson5 to print the json5 code to file, optionally using indenting the code to make it human-readable.
- Parameters:
file (Path | str) – The file name (as string or Path object). The file is overwritten if it exists.
indent (int) – indentation length. Raw dump if set to -1
compact (bool) – compact file writing, i.e. try to keep keys unquoted and avoid escapes