Raised This Month: $32 Target: $400
 8% 

[INC] JSON


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-10-2012 , 17:54   [INC] JSON
Reply With Quote #1

JSON for Pawn
by Exolent



Introduction
This is pretty much a useless include for 90% or more of you scripters.
I was bored one day and decided to write this up, but never got around to completing it as much as I wanted to or testing fully.
This include allows you to parse a JSON string into an object, read/set data from/to that object, create JSON objects, and encode a JSON object to string.
I didn't have much time to write up the documentation but it should be easy to understand.
This include will most likely only come in handy for sending/receiving data to/from a web server, such as a PHP script.


Include Information
Code:
#define JSON_KEY_SIZE   512
#define JSON_STR_SIZE   512

enum JSON
{
	JSON_INVALID = 0,
	
	JSON_Cell,
	JSON_Float,
	JSON_String,
	JSON_Object
};

// Create functions
JSON:json_create(bool:is_array=false)
JSON:json_from_array(const any:array[], size, JSON:types[]={JSON_INVALID}, JSON:force_type=JSON_INVALID)
JSON:json_from_array2(Array:array, size=-1, {Array,JSON}:types, bool:force_type=false)

// Set functions
bool:json_set_cell(JSON:object, const key[], any:value, JSON:type=JSON_Cell)
bool:json_set_array(JSON:object, const key[], const any:array[], size, JSON:types[]={JSON_INVALID}, JSON:force_type=JSON_INVALID)
bool:json_set_array2(JSON:object, const key[], Array:array, size=-1, bool:json_set_string(JSON:object, const key[], const string[])

// Get functions
bool:json_get_cell(JSON:object, const key[], &any:value, &JSON:type=JSON_Cell)
bool:json_get_array(JSON:object, const key[], any:output[], size, JSON:types[])
bool:json_get_array2(JSON:object, const key[], &Array:output, &size=0, &Array:types)
bool:json_get_string(JSON:object, const key[], output[], len)
JSON:json_get_type(JSON:object, const key[])

// Delete function
bool:json_delete(JSON:object, const key[])

// String functions
JSON:json_decode(const string[], &pos=0, len=0)
json_encode(JSON:object, output[], len)
json_escape(const string[], output[], len)

// Destroy function
bool:json_destroy(&JSON:object)

Notes
This is not fully tested. If you find any issues, please post them and I will get them fixed when I can.
New ideas are welcome if you have any.

More simple utilities like this to come when I get around to posting them.
I just don't want them to sit around and go to waste.
Attached Files
File Type: inc json.inc (21.0 KB, 1461 views)
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
K.K.Lv
Veteran Member
Join Date: Aug 2008
Location: GameFolder
Old 07-10-2012 , 23:09   Re: [INC] JSON
Reply With Quote #2

Wow !!!
nice one Exolent
example will be better.
PS: I'm poor in php
__________________
QQ:116268742
K.K.Lv is offline
Send a message via MSN to K.K.Lv
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 07-11-2012 , 05:03   Re: [INC] JSON
Reply With Quote #3

Looks good, might use it with joropito's threaded http module. But the question is how fast are functions in this include?
__________________
xPaw is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-11-2012 , 09:35   Re: [INC] JSON
Reply With Quote #4

Quote:
Originally Posted by K.K.Lv View Post
Wow !!!
nice one Exolent
example will be better.
PS: I'm poor in php
Code:
new iPlayers[ 32 ], iNum; get_players( iPlayers, iNum ); new iPlayer; new szName[ 32 ], szSteamID[ 35 ]; new JSON:jPlayers[ 32 ]; new JSON:jPlayer; for( new i = 0; i < iNum; i++ ) {     iPlayer = iPlayers[ i ];         get_user_name( iPlayer, szName, charsmax( szName ) );     get_user_authid( iPlayer, szSteamID, charsmax( szSteamID ) );         jPlayer = json_create( );         json_set_cell( jPlayer, "id", iPlayer );     json_set_string( jPlayer, "name", szName );     json_set_string( jPlayer, "steamid", szSteamID );         jPlayers[ i ] = jPlayer; } new JSON:jObject = json_create( ); json_set_array( jObject, "players", jPlayers, iNum, _, JSON_Object ); json_set_cell( jObject, "num_players", iNum ); static szEncoded[ 4096 ]; json_encode( jObject, szEncoded, charsmax( szEncoded ) ); json_destroy( jObject ); // Send szEncoded to PHP script // szEncoded should look like: // {"players":[{"id":"1","name":"Exolent","steamid":"STEAM_0:1:2345"},{"id":"2","name":"xPaw","steamid":"STEAM_0:1:337"}],"num_players":"2"}

PHP Code:
<?php

if(isset($_GET['json']))
{
    
$json json_decode($_GET['json']);
    
    
var_dump($json);
}
?>
Quote:
Originally Posted by xPaw View Post
Looks good, might use it with joropito's threaded http module. But the question is how fast are functions in this include?
Seeing as it's in stock form instead of a module, it is multiple times slower compared to Trie's or Array's get/set/create/delete/destroy functions.
However, this include is not supposed to act as any vault of some sort.

The purpose of this include is to create a JSON and set data to it, and read data from a given JSON string.
The longer the JSON string and larger the JSON object, the slower the encode and decode functions will be.

I can't see speed being a big issue here since the functions here aren't going to be frequently used or anything.
Anyway, a modular solution would most likely be better to reduce native calls for more efficiency.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 07-11-2012 at 09:49.
Exolent[jNr] is offline
K.K.Lv
Veteran Member
Join Date: Aug 2008
Location: GameFolder
Old 07-12-2012 , 06:00   Re: [INC] JSON
Reply With Quote #5

Code:
"[{"id":"1","name":"Exolent","steamid":"STEAM_0:1:2345"},{"id":"2","name":"xPaw","steamid":"STEAM_0:1:337"}]"
I just need this data, how to do it ?
__________________
QQ:116268742

Last edited by K.K.Lv; 07-12-2012 at 06:01.
K.K.Lv is offline
Send a message via MSN to K.K.Lv
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-12-2012 , 09:38   Re: [INC] JSON
Reply With Quote #6

Code:
new JSON:jObject = json_create( ); json_set_array( jObject, "players", jPlayers, iNum, _, JSON_Object ); json_set_cell( jObject, "num_players", iNum );

Code:
new JSON:jObject = json_from_array( jPlayers, iNum, _, JSON_Object );
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 07-12-2012 , 10:39   Re: [INC] JSON
Reply With Quote #7

Holy crap. I remember needing to parse a JSON string the other day because it was being sent by a PHP script but I gave up on it. Thanks, definitely will use this.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
Scherzo
Senior Member
Join Date: Feb 2007
Location: Kwidzyn, Poland
Old 07-12-2012 , 17:19   Re: [INC] JSON
Reply With Quote #8

Great job! Of course we can make own system, but also in web there are some interesting webservices responding in JSON, with socket module it will be powerful tool. I definitelly check it on weekend
Scherzo is offline
holycow
Junior Member
Join Date: Oct 2004
Location: San Francisco, Ca
Old 07-25-2012 , 02:11   Re: [INC] JSON
Reply With Quote #9

Awesome! I can totally use this for a plugin that I have in mind!


Now I just gotta learn how to write plugins! LoL!

Last edited by holycow; 07-25-2012 at 02:11.
holycow is offline
Send a message via AIM to holycow
ProIcons
Senior Member
Join Date: Jan 2009
Location: Greece - Salonica
Old 12-24-2012 , 13:12   Re: [INC] JSON
Reply With Quote #10

What about to encode a new Json of 10 players to this:

Quote:
{"game":{"half":"2","map":"de_dust2","start": "1353160142","warmup":"0","live":"1","tax":"0 ","subon":"1","scorea":"16","scoreb":"10","ro und":"27","gid":"5719"},"players":{"1":{"weap on":"usp","ammoc":"12","ammob":"84","health": "100","kills":"20","deaths":"22","name":"Deez er","steamid":"STEAM_0:1:42725051","ip":"46.1 76.48.106","team":"beta"},"2":{"health":"100" ,"weapon":"ak47","ammoc":"5","ammob":"50","ki lls":"25","name":"s3rb","steamid":"STEAM_0:1: 37857416","ip":"87.202.131.9","team":"alpha", "deaths":"12"},"3":{"health":"100","weapon":" ak47","ammoc":"26","ammob":"90","deaths":"19" ,"name":"xop","steamid":"STEAM_0:13085614", "ip":"79.107.65.3","team":"alpha","kills":"8" },"4":{"weapon":"glock18","ammoc":"17","ammob ":"25","health":"100","name":"HAUS","steamid" :"STEAM_0:0:22337974","ip":"87.203.8.245","te am":"alpha","kills":"19","deaths":"6"},"5":{" weapon":"m4a1","ammoc":"21","ammob":"90","hea lth":"100","deaths":"22","name":"EL_","steami d":"STEAM_0:12035819","ip":"88.218.177.200" ,"team":"beta","kills":"11"},"6":{"health":"1 00","weapon":"knife","ammoc":"-1","ammob":"0","deaths":"10","kills":"23","na me":"Twister_","steamid":"STEAM_0:15641534" ,"ip":"85.73.174.124","team":"alpha"},"7":{"w eapon":"ak47","ammoc":"30","ammob":"61","heal th":"100","name":"zCPa","steamid":"STEAM_0:0: 20542090","ip":"77.49.247.207","team":"alpha" ,"kills":"13","deaths":"19"},"8":{"weapon":"m 4a1","ammoc":"17","ammob":"65","health":"100" ,"deaths":"16","name":"hysteria`","steamid":" STEAM_0:1:20054620","ip":"109.68.150.14","tea m":"beta","kills":"20"},"9":{"weapon":"ak47", "ammoc":"0","ammob":"60","health":"100","kill s":"5","deaths":"8","name":"galat","steamid": "STEAM_0:1:18355489","ip":"178.128.41.14","te am":"beta"},"10":{"weapon":"deagle","ammoc":" 0","ammob":"35","health":"100","deaths":"21", "name":"Jethro","steamid":"STEAM_0:08770723 ","ip":"79.107.140.28","team":"beta","kills": "10"},"11":{"name":"SiLOR","steamid":"STEAM_0 :0:46665629","ip":"79.166.2.207","team":"beta ","weapon":"ak47","ammoc":"14","ammob":"61"," health":"100","deaths":"13","kills":"10"},"12 ":{"weapon":"hegrenade","ammoc":"-1","ammob":"1","health":"100","kills":"6","na me":"nikolakis","steamid":"STEAM_0:0:21886849 ","ip":"85.75.160.114","team":"beta","deaths" :"7"},"13":{"name":"stoNg","steamid":"STEAM_0 :1:21642484","ip":"188.4.24.172","team":"beta ","weapon":"ak47","ammoc":"22","ammob":"90"," health":"100","kills":"9","deaths":"4"}},"inf o":{"players":13}}
On every Event CurWeapon / client_damage

Will be laggy?
__________________
function rb return $regsubex($$1-,/(.)/g,$+($chr(2) $+ $chr(3),$r(2,15),$chr(2),\1))

Last edited by ProIcons; 12-24-2012 at 13:12.
ProIcons 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 18:06.


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