Skip to content

omnipy.components.json.helpers

Utility predicates and parsers for JSON-compatible values.

FUNCTION DESCRIPTION
is_json_dict

Return whether a JSON value is an object.

is_json_list

Return whether a JSON value is an array.

is_json_scalar

Return whether a JSON value is a scalar.

parse_line_as_elements_of_dict

Parse a line as the comma-separated contents of a JSON object.

parse_line_as_elements_of_list

Parse a line as the comma-separated contents of a JSON array.

parse_str_as_json

Parse a string as JSON, returning Undefined on failure.

is_json_dict

is_json_dict(json_val: Json) -> TypeIs[JsonDict]

Return whether a JSON value is an object.

Source code in src/omnipy/components/json/helpers.py
def is_json_dict(json_val: Json) -> TypeIs[JsonDict]:
    """Return whether a JSON value is an object."""

    match json_val:
        case dict():
            return True
        case _:
            return False

is_json_list

is_json_list(json_val: Json) -> TypeIs[JsonList]

Return whether a JSON value is an array.

Source code in src/omnipy/components/json/helpers.py
def is_json_list(json_val: Json) -> TypeIs[JsonList]:
    """Return whether a JSON value is an array."""

    match json_val:
        case list():
            return True
        case _:
            return False

is_json_scalar

is_json_scalar(json_val: Json) -> TypeIs[JsonScalar]

Return whether a JSON value is a scalar.

Source code in src/omnipy/components/json/helpers.py
def is_json_scalar(json_val: Json) -> TypeIs[JsonScalar]:
    """Return whether a JSON value is a scalar."""

    match json_val:
        case str() | int() | float() | bool() | None:
            return True
        case _:
            return False

parse_line_as_elements_of_dict

parse_line_as_elements_of_dict(_line: str) -> JsonDict | pyd.UndefinedType

Parse a line as the comma-separated contents of a JSON object.

Source code in src/omnipy/components/json/helpers.py
def parse_line_as_elements_of_dict(_line: str) -> JsonDict | pyd.UndefinedType:
    """Parse a line as the comma-separated contents of a JSON object."""

    if _line.startswith('"'):
        try:
            return json.loads(f'{{{_line}}}')
        except json.JSONDecodeError:
            pass
    return pyd.Undefined

parse_line_as_elements_of_list

parse_line_as_elements_of_list(_line: str) -> JsonList | pyd.UndefinedType

Parse a line as the comma-separated contents of a JSON array.

Source code in src/omnipy/components/json/helpers.py
def parse_line_as_elements_of_list(_line: str) -> JsonList | pyd.UndefinedType:
    """Parse a line as the comma-separated contents of a JSON array."""

    try:
        return json.loads(f'[{_line}]')
    except json.JSONDecodeError:
        return pyd.Undefined

parse_str_as_json

parse_str_as_json(_line: str) -> Json | pyd.UndefinedType

Parse a string as JSON, returning Undefined on failure.

Source code in src/omnipy/components/json/helpers.py
def parse_str_as_json(_line: str) -> Json | pyd.UndefinedType:
    """Parse a string as JSON, returning ``Undefined`` on failure."""

    try:
        return json.loads(_line)
    except json.JSONDecodeError:
        return pyd.Undefined