Examples

Object parsing

The following code parses a simple JSON object (dictionary) with two attributes: name is mandatory, while height defaults to 100 if missing. The context manager makes sure that the object contains only keys referenced by the parser.

from forestwalker import Walker, WalkerError

tree = {
    'name': 'Robin Hood',
    'height': 184,
}

try:
    root = Walker(tree)
    with root.enter_object() as obj:
        name = obj['name'].as_str()
        height = obj['height'].as_float(100)
        print(name, height)
except WalkerError as err:
    print(f'Parse error: {err}')