Json Settings API (load/save data to JSON files)

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1189
Gender:
United States of America

Json Settings API (load/save data to JSON files)

Post by Archive-Poster »

I was finishing my free panel for servers, and I felt the need for an api in json, so I decided to do it.

This api was based on the Settings API (load/save data to INI files), but completely redone in json. Quick and easy

Use Case:

You want your plugin to use custom sounds and models, then allow users to change them. Previously it would require coding a section into the .sma file, making recompile necessary every time you want to change something. This API allows resources to be edited "externally" through the JSON file instead. You can also save player data easily.

Example:

Code: Select all

#include <amxmodx>
#include <api_json_settings>

#define PLUGIN  "Example JSON Settings API"
#define VERSION "1.1"
#define AUTHOR  "Wilian M."

new const PATH[] = { "my_folder/configs.json" } // this will be located at: addons/amxmodx/configs/my_folder/configs.json

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    register_concmd("rsec", "rsec")
    register_concmd("rkey", "rkey")
}

public rsec()
{
    json_setting_remove_section(PATH, "section_int_arr")
}

public rkey()
{
    json_setting_remove_key(PATH, "section_int", "another key int haha")
}

public plugin_precache()
{
    new int
    if(!json_setting_get_int(PATH, "section_int", "my_key", int)) // section and key does not exist, so create it.
    {
        json_setting_set_int(PATH, "section_int", "my_key", 777)
        json_setting_set_int(PATH, "section_int", "another key int haha", 666)
    }
    else // exists
        server_print("exists, return my_key int value: %d", int)

    new Float:float
    if(!json_setting_get_float(PATH, "section_float", "my_key", float)) // section and key does not exist, so create it.
    {
        json_setting_set_float(PATH, "section_float", "my_key", 7.77)
        json_setting_set_float(PATH, "section_float", "another key float", 6.66)
    }
    else // exists
        server_print("exists, return my_key float value: %f", float)

    new Array:int_arr = ArrayCreate()
    if(!json_setting_get_int_arr(PATH, "section_int_arr", "my_key", int_arr)) // section and key does not exist, so create it.
    {
        ArrayPushCell(int_arr, 7)
        ArrayPushCell(int_arr, 777)
        ArrayPushCell(int_arr, 666)

        json_setting_set_int_arr(PATH, "section_int_arr", "my_key", int_arr)
        ArrayDestroy(int_arr)

        int_arr = ArrayCreate()
        ArrayPushCell(int_arr, 1)
        ArrayPushCell(int_arr, 2)
        ArrayPushCell(int_arr, 3)

        json_setting_set_int_arr(PATH, "section_int_arr", "another int array", int_arr)
        ArrayDestroy(int_arr)
    }
    else // exists
    {
        for(new i = 0; i < ArraySize(int_arr); i++)
            server_print("exists, return my_key int array value: %d", ArrayGetCell(int_arr, i))
    }
} 
File syntax:

Code: Select all

{
    "section_int": {
        "my_key": 777,
        "another key int haha": 666
    },
    "section_float": {
        "my_key": 7.7699999809265137,
        "another key float": 6.6599998474121094
    },
    "section_int_arr": {
        "my_key": [
            7,
            777,
            666
        ],
        "another int array": [
            1,
            2,
            3
        ]
    }
}
Attachments
amx_api_json_settings_v1.1.zip
(10.96 KiB) Downloaded 6 times

Who is online

Users browsing this forum: No registered users and 1 guest