[Plugin] SAMPSON - A JSON plugin for SA-MP
#1

Current version: v0.1.1
Last updated: 30/10/2014

Why?
I decided to build this plugin as a fellow member of the SA-MP community was having issues with DJSON. DJSON is also outdated and does not conform to various JSON standards. It caches to disk, has a large memory footprint, and uses SQL for... JSON storage. It seemed wrong to me, so I decided to build my own plugin.


Features:
The plugin currently features a JSON parser for files located in your 'scriptfiles' directory. It's compliant to the JSON standards and can parse strings, numbers, booleans and arrays.


Documentation:
I'll be doing tutorials on the usage of this plugin, and a Wiki for all the functions. But for now, I'll just dump the functions with basic information about them here:

You can define a node delimiter by defining JSON_DEFAULT_DELIMITER to a char; Default delimiter is
pawn Код:
#define JSON_DEFAULT_DELIMITER '/'

pawn Код:
native JSONNode:json_parse_file(const file_path[])
Function: Reads a file, parses it, and stores it in memory for further parsing.
Returns: A handle to the root node.


pawn Код:
native JSONNode:json_parse_string(const string[])
Function: Parses a string and stores it in memory for further parsing.
Returns: A handle to the root node.


pawn Код:
native json_close(JSONNode:node)
Function: Closes a JSON file with root node 'node' and frees all memory associated with it.
Returns: 1 on success, 0 on failure.


pawn Код:
native json_get_type(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)
Function: Gets the type of a node's value.
Returns: An integer representing the node's value type. See below for type definitions.

JSON type definitions:
pawn Код:
JSON_NULL
JSON_STRING
JSON_NUMBER
JSON_BOOL
JSON_ARRAY
JSON_NODE

pawn Код:
native JSONNode:json_get_node(JSONNode:node, const path[], const path_delim = JSON_DEFAULT_DELIMITER)
Function: Gets the node at some path using 'node' as a root node.
Returns: The node on success, 0 if the node doesn't exist.


pawn Код:
native bool:json_get_bool(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)
Function: Gets the value of some path using 'node' as a root node as a boolean.
Returns: The value of the specified path's key as a boolean.


pawn Код:
native json_get_int(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)
Function: Gets the value of some path using 'node' as a root node as an integer.
Returns: The value of the specified path's key as an integer.


pawn Код:
native Float:json_get_float(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)
Function: Gets the value of some path using 'node' as a root node as a float.
Returns: The value of the specified path's key as a float.


pawn Код:
native json_get_string(JSONNode:node, dst[], const len = sizeof(dst), const path[] = '\0', const bool:packed = false, const path_delim = JSON_DEFAULT_DELIMITER)
Function: Gets the value of some path using 'node' as a root node as a string and writes it to 'dst'.
Returns: 1.


pawn Код:
native json_get_name(JSONNode:node, dst[], const len = sizeof(dst), const bool:packed = false)
Function: Gets the name (key) of some path using 'node' as a root node and writes it to 'dst'.
Returns: 1 if the node has a name (key) associated with it, 0 if it doesn't.


pawn Код:
native JSONArray:json_get_array(JSONNode:node, const path[] = '\0', const path_delim = JSON_DEFAULT_DELIMITER)
Function: Gets the value of some path using 'node' as a root node as an array.
Returns: A JSON array type which is used for array functions.


pawn Код:
native json_array_count(JSONArray:array)
Function: Gets the amount of elements 'array' contains.
Returns: An integer representing the amount of elements 'array' contains.


pawn Код:
native JSONNode:json_array_at(JSONArray:array, const index)
Function: Gets the element at 'index' in 'array' as a node.
Returns: The element as a JSON node.


Downloads:

Releases: GitHub
Source code: GitHub



Installation:
Download the release archive from the link provided above and place its contents into your server folder. Open your configuration file and add 'SAMPSON' ('SAMPSON.so' on Linux) to your 'plugins' line.


Building from source:
Download the source code from the link provided above.
On Windows, open the 'SAMPSON.sln' file in Visual Studio, select 'Release' build target and then build it.
On Linux, run 'make' in the 'src' folder. The built plugin should be found in the 'build' folder.


Credits:
  • The SA-MP team - SA-MP, SA-MP plugin SDK
  • Djole1337 - Testing support, Linux build support
Reply
#2

What are the advantages of this versus any other file methods / plugin(s)?
Reply
#3

Seems nice, though I think I'll get stick with MySQL.

Nice job anyway.
Reply
#4

Congratulations, King_Hual
Reply
#5

Quote:
Originally Posted by Fool
Посмотреть сообщение
Congratulations, King_Hual
Reply
#6

Quote:
Originally Posted by Abagail
Посмотреть сообщение
What are the advantages of this versus any other file methods / plugin(s)?
For example, JSON supports arrays/vectors, keys and values can be nested and it's generally lightweight. I won't go into detail, but you could visit http://www.json.org/ for more information.
Reply
#7

dam son good work
Reply
#8

I agree with DJSON include dose slow down alot of thing's nice work Kings.
Reply
#9

Are you planning to implement a "json_parse_string" function? In my case I receive json strings via socket data, so storing the string in a file isn't a performatic option.


I want to do something like:
pawn Код:
public onSocketReceiveData(id, remote_clientid, data, data_len) {
    new action[16], token[64], JSONNode:jsonData;

    // Data is a json string (Ex: {token: 'd7asd78g', action: 'kickPlayer'})
    json_parse_string(jsonData, data);

    json_get_string(jsonData, action, sizeof(action), 'action');
    json_get_string(jsonData, token, sizeof(token), 'token');

    printf("Action received: %s, Security Token: %s", action, token);
}
Reply
#10

Quote:
Originally Posted by Mandrakke
Посмотреть сообщение
Are you planning to implement a "json_parse_string" function? In my case I receive json strings via socket data, so storing the string in a file isn't a performatic option.


I want to do something like:
pawn Код:
public onSocketReceiveData(id, remote_clientid, data, data_len) {
    new action[16], token[64], JSONNode:jsonData;

    // Data is a json string (Ex: {token: 'd7asd78g', action: 'kickPlayer'})
    json_parse_string(jsonData, data);

    json_get_string(jsonData, action, sizeof(action), 'action');
    json_get_string(jsonData, token, sizeof(token), 'token');

    printf("Action received: %s, Security Token: %s", action, token);
}
Sure, I'll add that to the to-do list.
Reply
#11

pawn Code:
json_open
crashes the server if the file doesn't exist... It would be great if you could add checks and return -1 if the given file doesn't exist.
Reply
#12

Version 0.1.1 released:

* Added new function json_parse_string
* Renamed json_open to json_parse_file
* Fixed memory leak in json_get_string
* Fixed crash if invalid file path is passed to json_parse_file
Reply
#13

Quote:
Originally Posted by KingHual
View Post
* Added new function json_parse_string
Thank you, I'll try it out.
Reply
#14

How about setting values?
Reply
#15

Quote:
Originally Posted by theYiin
View Post
How about setting values?
That's being worked on. As I mentioned already, it's a parser at this stage.
Reply
#16

My server is hosted at a company and they don't have version GLIBCXX_3.4.15 of libstdc++.so.6. What I can do?

EDIT: does not matter.
Reply
#17

I was trying to convince whitetiger some time ago to do it as I didn't have time myself, but wow. I just realized you made it, so thank you! This will be ultimately helpful when I make my geolocation API :P

Once again, excellent work KingHaul!
Reply
#18

Just perfectly nice great work KingHual.
Reply
#19

Oh cool
Reply
#20

Not Bad
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)