Raised This Month: $12 Target: $400
 3% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
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, 87 views)

Last edited by wilian159; 03-26-2024 at 11:35. Reason: news natives
wilian159 is offline
wilian159
Member
Join Date: Dec 2013
Old 03-26-2024 , 11:35   Re: Json Settings API (load/save data to JSON files)
Reply With Quote #2

news natives!
__________________
wilian159 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 04-20-2024 , 05:25   Re: Json Settings API (load/save data to JSON files)
Reply With Quote #3

I hate to bother you but JSON parsing already exist in the latest amxmodx version, just #include <json>
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
wilian159
Member
Join Date: Dec 2013
Old 05-03-2024 , 05:41   Re: Json Settings API (load/save data to JSON files)
Reply With Quote #4

Quote:
Originally Posted by Natsheh View Post
I hate to bother you but JSON parsing already exist in the latest amxmodx version, just #include <json>
You certainly didn't understand the use of this.
__________________
wilian159 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 05-03-2024 , 07:25   Re: Json Settings API (load/save data to JSON files)
Reply With Quote #5

Quote:
Originally Posted by wilian159 View Post
You certainly didn't understand the use of this.
Oh my bad, i just noticed it edit the json files...

Sometimes my brain is not functioning properly, exe crash

Good work l...!!
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 05-03-2024 at 07:27.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 05-05-2024 , 20:01   Re: Json Settings API (load/save data to JSON files)
Reply With Quote #6

theres is a lot of repetitive code. you can avoid this creating a separate function then distributing it where its meant to be used
__________________
bigdaddy424 is offline
rtxa
Senior Member
Join Date: Mar 2018
Location: Argentina
Old 05-09-2024 , 11:20   Re: Json Settings API (load/save data to JSON files)
Reply With Quote #7

Not sure to understand, but why it would require recompiling when you change something? Isn't that the point of using a config file (in this case a JSON file) to avoid doing that? Why can't this be done with the JSON library that comes with the newer versions of AMXX? (1.9 and newer) Unless you are doing this for older versions.
__________________
rtxa is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:54.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode