import json


def to_json(context) -> str:
    """Convert the selected Selection Sets of the current rig to JSON.

    Selected Sets are the active_selection_set determined by the UIList
    plus any with the is_selected checkbox on."""

    arm = context.object
    active_idx = arm.active_selection_set

    json_obj = {}
    for idx, sel_set in enumerate(context.object.selection_sets):
        if idx == active_idx or sel_set.is_selected:
            bones = [bone_id.name for bone_id in sel_set.bone_ids]
            json_obj[sel_set.name] = bones

    return json.dumps(json_obj)