# Callback

### Signature

```csharp
table onSave(string filepath)
```

### Description

The callback function that is responsible for storing the data in the JSON file when save data is saved.

Returning an empty table (`{}`) will delete the save data for the identifier whereas returning `nil` or nothing will keep it.

### Arguments

#### File Path

The path of the save game.

### Example

#### Callback

```lua
local function onSave()
  return {
    name2 = {11, 22, 33}, --array
    name1 = "value1", --string
    name3 = { --table
      [1] = "aa",
      nn = "bb", --makes it a table rather than an array
      [3] = "cc"
    }
  }
end

event.registerSaveHandler("test", onSave)
```

#### Output

{% code title="test.json" fullWidth="false" %}

```json
{
    "name1": "value1",
    "name2": [
        11,
        22,
        33
    ],
    "name3": {
        "1": "aa",
        "3": "cc",
        "nn": "bb"
    }
}
```

{% endcode %}

### Notes

{% hint style="warning" %}
Only the following value types are supported: `table`, `string`, `double`, `integer`, `bool`.
{% endhint %}

{% hint style="info" %}
Table keys are sorted alphabetically before saving data.
{% endhint %}

{% hint style="info" %}
If a table starts at 1, increases sequentially without gaps, and contains only integer keys, it will be serialized as an array. However, if explicit key notations (`[n] = value`) are used, it will be serialized as a table with string keys, since Lua internally uses a hash table implementation for all keys and the C++ interface provided by Sol2 cannot traverse these tables in the proper order.
{% endhint %}
