View Single Post
Author Message
wilian159
Member
Join Date: Dec 2013
Old 03-20-2024 , 10:00   Json Settings API (load/save data to JSON files)
Reply With Quote #1

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:

PHP Code:

#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(PLUGINVERSIONAUTHOR)

    
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_arr7)
        
ArrayPushCell(int_arr777)
        
ArrayPushCell(int_arr666)

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

        
int_arr ArrayCreate()
        
ArrayPushCell(int_arr1)
        
ArrayPushCell(int_arr2)
        
ArrayPushCell(int_arr3)

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

File syntax:

Code:
{
    "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
        ]
    }
}
Any problem please comment.
Attached Files
File Type: zip amx_api_json_settings_v1.1.zip (11.0 KB, 86 views)

Last edited by wilian159; 03-26-2024 at 11:35. Reason: news natives
wilian159 is offline