Skip to content

Json utils

💻 API reference

parse(string, *, object_hook=None, **kwargs)

Parse a JSON string into a JsObject or JsArray or a sequence of arbitrary objects.

Source code in fateful/json.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def parse(
    string: str | bytes | bytearray,
    *,
    object_hook: type[T] | None = None,
    **kwargs: t.Any,
) -> js_array | js_object | list[T] | T:
    """Parse a JSON string into a JsObject or JsArray or
    a sequence of arbitrary objects."""
    value = json.loads(string, **kwargs)
    is_list = isinstance(value, t.Sequence)
    if object_hook is None:
        if is_list:
            return js_array(value)
        return js_object(value)
    if is_list:
        return [object_hook(**val) for val in value]
    return object_hook(**value)