Json5

class sim_explorer.json5.Json5(js5: str | PathLike[str] | dict, auto: bool | int = True, comments_eol: tuple[str, ...] = ('//', '#'), comments_ml: tuple[str, ...] = ('/*', "'" * 3, '"' * 3), keys_unique: bool = True)

Bases: object

Work with json5 files (e.g. cases specification and results).

  • Read Json5 code from file, string or dict, representing the result internally as Python code (dict of dicts,lists,values)

  • Searching for elements using JsonPath expressions

  • Some Json manipulation methods

  • Write Json5 code to file

Parameters:
  • js5 (Path,str) – Path to json5 file or json5 string

  • auto (bool) – Determine whether running to_py automatically

  • comments_eol (tuple)= ('//', '#') – tuple of end-of-line comment strings which shall be recognised

  • comments_ml (tuple)= (‘/*’, “’’’”) – tuple of multi-line comment strings which shall be recognised. End of comment is always the reversed of the start of comment. Double-quote ml comments are also supported per default

__init__(js5: str | PathLike[str] | dict, auto: bool | int = True, comments_eol: tuple[str, ...] = ('//', '#'), comments_ml: tuple[str, ...] = ('/*', "'" * 3, '"' * 3), keys_unique: bool = True)

Methods

__init__(js5[, auto, comments_eol, ...])

check_valid_js(js_py[, print_msg])

Check whether the dict js_py is a valid Json dict.

jspath(path[, typ, errorMsg])

Evaluate a JsonPath expression on the Json5 code and return the result.

line(num)

Return the raw json5 line 'num'.

to_py()

Translate json5 code 'self.js5' to a python dict and store as self.js_py.

update(spath, data)

Append data to the js_py dict at the path pointed to by keys.

write([file, pretty_print])

Write a Json(5) tree to string or file.

Attributes

static check_valid_js(js_py, print_msg=False)

Check whether the dict js_py is a valid Json dict.

jspath(path: str, typ: type | None = None, errorMsg: bool = False)

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:
  • path (str) – path expression as string.

  • typ (type) – optional specification of the expected type to find

  • errMsg (bool) – specify whether an error should be raised, or None returned (default)

line(num: int) str

Return the raw json5 line ‘num’.

Parameters:

num (int) – the line number of the line to retrieve (zero-based). ‘num’ works here like python indexes, starting from 0 and also working with negative numbers!

to_py() dict[str, Any]

Translate json5 code ‘self.js5’ to a python dict and store as self.js_py.

update(spath: str, data: Any)

Append data to the js_py dict at the path pointed to by keys. So far this is a minimum implementation for adding data. Probably this could be done using jysonpath-ng.

write(file: str | PathLike[str] | None = None, pretty_print: bool = True)

Write a Json(5) tree to string or file.

Parameters:
  • file (str, Path) – The file name (as string or Path object) or None. If None, a string is returned.

  • pretty_print (bool) – Denote whether the string/file should be pretty printed (LF,indents).

Returns: The serialized Json(5) object as string. This string is optionally written to file.

comments
comments_eol
comments_ml
js5
js_py
lines
pos