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

Solved How does the JSON module work?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ricaz
New Member
Join Date: Aug 2021
Old 08-02-2021 , 16:32   How does the JSON module work?
Reply With Quote #1

Hi.

I want to send JSON events from a mod. I upgraded to v. 1.10 to get the JSON include that was added a while ago.
I can't really find any examples of its use, so I tried going off the documentation on amxmodx.org/api/json, but I can't really seem to make it work:

Code:
stock send_players()
{
    new Players[32]
    new playerCount, i
    new JSON:playersJson = json_init_object();
    new JSON:argsJson = json_init_array();
    json_object_set_string(playersJson, "command", "players");
    get_players(Players, playerCount, "c")
    for (i=0; i<playerCount; i++)
    {
        new JSON:playerObject = json_init_object();
        json_object_set_string(playerObject, "steamid", "1234");
        json_object_set_string(playerObject, "name", "plaf");
        json_array_append_value(argsJson, playerObject);
    }

    json_object_set_value(playersJson, "args", argsJson);

    new buf[3072]
    json_serial_to_string(playersJson, buf, strlen(buf));

    log_amx("PLAYERS: %s", buf);

    json_free(argsJson);
    json_free(playersJson);
}
After this, 'buf' is just an empty string. What am I doing wrong?

Thanks.

Last edited by Ricaz; 08-09-2021 at 08:00.
Ricaz is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 08-02-2021 , 18:24   Re: How does the JSON module work?
Reply With Quote #2

The problem is that you're using strlen instead of charsmax.

Code:
json_serial_to_string(playersJson, buf, strlen(buf))
Since you didn't assign any string to the buf variable, strlen will return 0. The third parameter of json_serial_to_string is:

Code:
maxlen - Maximum size of the buffer
You're literally doing this:
Code:
json_serial_to_string(playersJson, buf, 0);
// copy up to 0 characters
charsmax will return the size of the buffer - 1 for the null terminator, in your case 3072 - 1 (3071) and will copy up to 3070 characters.

By the way, you should probably call json_free() on the playerObject variable after each iteration.

Code:
new JSON:playerObject = json_init_object();
json_object_set_string(playerObject, "steamid", "1234");
json_object_set_string(playerObject, "name", "plaf");
json_array_append_value(argsJson, playerObject);
json_free(playerObject)
__________________









Last edited by CrazY.; 08-02-2021 at 18:29.
CrazY. is offline
Ricaz
New Member
Join Date: Aug 2021
Old 08-09-2021 , 07:59   Re: How does the JSON module work?
Reply With Quote #3

Ah, my SourcePawn is a bit rusty. It works now. Thanks!
Ricaz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-09-2021 , 22:13   Re: How does the JSON module work?
Reply With Quote #4

To be clear, this isn't SourcePawn. SourcePawn is only used with SourceMod.
__________________
fysiks is offline
Reply



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 03:09.


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