def _parse_value(value):

    value = value.strip()

    if "|" in value:
        return [_parse_value(v) for v in value.split("|")]

    if value.lower() == "true":
        return True

    if value.lower() == "false":
        return False

    if value.isdigit():
        return int(value)

    try:
        return float(value)
    except:
        pass

    return value


def _set_nested(obj, keys, value):

    for key in keys[:-1]:

        if key not in obj:
            obj[key] = {}

        obj = obj[key]

    obj[keys[-1]] = value


def toon_to_json(text):

    items = []

    lines = text.strip().split("\n")

    for line in lines:

        if not line.strip():
            continue

        obj = {}

        fields = line.split(";")

        for field in fields:

            field = field.strip()

            if "=" not in field:
                raise ValueError(f"Invalid field: {field}")

            key, value = field.split("=", 1)

            keys = key.strip().split(".")

            parsed_value = _parse_value(value)

            _set_nested(obj, keys, parsed_value)

        items.append(obj)

    return {"items": items}