Skip to content

CharacterIO

character_io

load_character(path)

Load a Character from a JSON file.

Parameters:

Name Type Description Default
path str | Path

Path to JSON file.

required

Returns:

Type Description
Character

Character instance.

Source code in src/rpgleveler/io/character_io.py
19
20
21
22
23
24
25
26
27
28
29
30
def load_character(path: str | Path) -> Character:
    """
    Load a Character from a JSON file.

    Args:
        path: Path to JSON file.

    Returns:
        Character instance.
    """
    data = json.loads(Path(path).read_text())
    return Character.from_dict(data)

save_character(character, path)

Save a Character to a JSON file.

Parameters:

Name Type Description Default
character Character

Character to serialize.

required
path str | Path

Output file path.

required
Source code in src/rpgleveler/io/character_io.py
 7
 8
 9
10
11
12
13
14
15
16
def save_character(character: Character, path: str | Path) -> None:
    """
    Save a Character to a JSON file.

    Args:
        character: Character to serialize.
        path: Output file path.
    """
    data = character.to_dict()
    Path(path).write_text(json.dumps(data, indent=2))