For the complete documentation index, see llms.txt. This page is also available as Markdown.

JSON Serialization

For saving more advanced data structures, you can serialize them to JSON and store them in the Save system.

CSL can serialize certain data structures into JSON for you. This is most useful when you want to save a “bundle” of related fields (like player progress, upgrades, unlocks, etc) without managing lots of separate save keys.

Under the hood, this uses the Save system's JSON APIs:

  • Save.set_json(player, key, value)

  • Save.try_get_json(player, key, out) -> bool

JSON save/load is per-player (it takes a player). Game-wide save currently supports strings + ints only.

What gets saved?

Only fields marked with @ao_serialize are included in the JSON.

Player_Progress :: class {
    @ao_serialize version: s64 = 1;

    @ao_serialize xp: s64;
    @ao_serialize level: s64 = 1;

    @ao_serialize unlocked_skins: [..]string;
    @ao_serialize equipped_weapon: Item_Instance;
}

Saving JSON

Save the whole structure under one key:

In Save JSON, Item_Instance fields and derived item fields persist as selections from any of that player's persistent inventories. Transferring an item to another inventory invalidates its references, even for the same player. Saving an item outside that player's persistent inventories raises an error. Missing or invalidated references and derived-type mismatches load as null. Create named inventories before loading the record. Standalone JSON.serialize does not provide this item-reference behavior.

Loading JSON (with defaults)

Save.try_get_json returns false if the key is missing or the JSON string is malformed. Allocate class data before loading because the false path does not allocate it. Missing fields keep their class defaults, and unknown fields are ignored.

Versioning & migrations

If you expect your JSON schema to change, include a version field in the structure and migrate after load.

Best practices for staying compatible:

  • Prefer adding new fields with sensible class defaults.

  • Keep a version field when a new value must be derived from old data.

  • If you need a hard break, consider saving under a new key (e.g. "progress_v2") and keeping a fallback loader.

Migration Example:

Fixed arrays

Fixed-array input is bounded by the destination:

  • Extra JSON items are ignored.

  • Missing items leave the remaining destination elements unchanged.

When to use JSON vs simple keys

  • Use simple keys (Save.set_int, Save.set_string, etc) for a handful of values you read/write often.

  • Use JSON when you want to store a cohesive structure (progress, loadouts, unlocks) and keep your save logic centralized.

Standalone JSON API

You can also serialize/deserialize JSON independently of the Save system (e.g. for logging, networking, or custom storage):

Last updated