Raised This Month: $202 Target: $400
 50% 

[INC] JSON in pure SP - Port from AMXX pawn


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
psychonic

BAFFLED
Join Date: May 2008
Old 09-08-2012 , 23:13   [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #1

JSON encoding and decoding in pure sourcepawn, directly ported from Exolent[jNr]'s pawn version.

See link for documentation.
Attached Files
File Type: inc json.inc (21.9 KB, 1344 views)
psychonic is offline
FaTony
Veteran Member
Join Date: Aug 2008
Old 09-08-2012 , 23:28   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #2

Next: JVM in pure SP. Oh yeah!
__________________
FaTony is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 09-10-2012 , 01:08   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #3

Do you mean that if I take a plugin from AMXX that use JSON I can use it to get the same results in SourceMod?
__________________
retired
shavit is offline
DarthNinja
SourceMod Plugin Approver
Join Date: Mar 2009
Location: PreThinkHook()
Old 09-10-2012 , 01:30   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #4

Quote:
Originally Posted by psychonic View Post
JSON encoding and decoding in pure sourcepawn.
Delicious!
__________________
DarthNinja is offline
alongub
Veteran Member
Join Date: Aug 2009
Location: Israel
Old 09-10-2012 , 13:02   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #5

I've been searching this for a while. Thanks! Is it faster than the C++ extension? Any benchmarks?
alongub is offline
psychonic

BAFFLED
Join Date: May 2008
Old 09-10-2012 , 18:32   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #6

Quote:
Originally Posted by alongub View Post
I've been searching this for a while. Thanks! Is it faster than the C++ extension? Any benchmarks?
No, it's probably much slower, though possibly not enough to matter.

You'd have to use the SM profiler.
psychonic is offline
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 03-16-2013 , 11:27   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #7

Huhm, error or on purpose?

JSON_FUNC bool:json_get_string(JSONbject, const String:key[], output[], len)
should be
JSON_FUNC bool:json_get_string(JSONbject, const String:key[], Stringutput[], len)

edit: also glad that the extension and this include have no clashing names.
edit2: looks like this does not work at all.

PHP Code:
#pragma semicolon 1
#include <sourcemod>
#include <json>

public OnPluginStart() {
    
TestJsonDecode("{\"integer\": 1}");
    
TestJsonDecode("{\"string\": \"Hello World\"}");
    
TestJsonDecode("{\"bool\": true}");
    
TestJsonDecode("{\"null\": null}");
}

public 
TestJsonDecode(const String:sJSON[]) {
    
LogMessage("Trying to decode: %s"sJSON);

    new 
JSON:hJson json_decode(sJSON);
    if(
hJson == JSON_INVALID) {
        
LogMessage("--> Failed");
    } else {
        
LogMessage("--> Passed");
    }

gives me:
Code:
L 03/16/2013 - 16:48:50: [tJsonTest.smx] Trying to decode: {"integer": 1}
L 03/16/2013 - 16:48:50: [tJsonTest.smx] --> Failed
L 03/16/2013 - 16:48:50: [tJsonTest.smx] Trying to decode: {"string": "Hello World"}
L 03/16/2013 - 16:48:50: [tJsonTest.smx] --> Failed
L 03/16/2013 - 16:48:50: [tJsonTest.smx] Trying to decode: {"bool": true}
L 03/16/2013 - 16:48:50: [tJsonTest.smx] --> Failed
L 03/16/2013 - 16:48:50: [tJsonTest.smx] Trying to decode: {"null": null}
L 03/16/2013 - 16:48:50: [tJsonTest.smx] --> Failed
[SM] Plugin tJsonTest.smx reloaded successfully.
__________________
einmal mit profis arbeiten. einmal.

Last edited by Thrawn2; 03-16-2013 at 11:51.
Thrawn2 is offline
Nikkii
Member
Join Date: Feb 2012
Old 03-16-2013 , 18:19   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #8

A little debugging shows that the decoding isn't working right

Code:
L 03/16/2013 - 18:16:03: [testjson.smx] Trying to decode: {"string": "Hello World"}
L 03/16/2013 - 18:16:03: [testjson.smx] Created trie
L 03/16/2013 - 18:16:03: [testjson.smx] Decoding dictionary, json is 
L 03/16/2013 - 18:16:03: [testjson.smx] Decode string HloWrd}
So my best guess is that this wasn't really tested/it broke with a sourcemod update :p

Fixing string reading:
Code:
while( ++pos <= len )
In that same statement at the bottom is pos++, remove either the ++ in the while or remove the whole pos++ in the end (2 places, array and json_decode)

Still broken:
Integer/Boolean/Float (Something to do with either reading too much or not enough in the while checking for spaces)
__________________
Owner of ProbablyAServer, a server without game changing mods and donation benefits

RCON Helper | [TF2] LogUpload | CCC Donator Tags | PHP Steam API Wrapper

Last edited by Nikkii; 03-16-2013 at 19:12.
Nikkii is offline
psychonic

BAFFLED
Join Date: May 2008
Old 03-16-2013 , 19:12   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #9

Quote:
Originally Posted by Thrawn2 View Post
Huhm, error or on purpose?

JSON_FUNC bool:json_get_string(JSONbject, const String:key[], output[], len)
should be
JSON_FUNC bool:json_get_string(JSONbject, const String:key[], Stringutput[], len)
Typo when porting from the AMXX one.

Regarding decoding, I've been told before that it has issues (as would the original).

I've only used the encoding functions which seemed to work fine.
psychonic is offline
Nikkii
Member
Join Date: Feb 2012
Old 03-18-2013 , 01:39   Re: [INC] JSON in pure SP - Port from AMXX pawn
Reply With Quote #10

A little treat for everyone, now with working decoding

https://raw.github.com/nikkiii/logup...clude/json.inc (Previous paste.ee link had a bug with string lengths, this implementation will be fixed a lot more often if broken)

Testing:
http://paste.ee/r/yqhgL

Results:
Code:
L 03/18/2013 - 01:38:30: [testjson.smx] Trying to decode: {"integer": 1, "string": "Hello World", "integer2": 2}
L 03/18/2013 - 01:38:30: [testjson.smx] --> Passed
L 03/18/2013 - 01:38:30: [testjson.smx] Trying to decode: {"string": "Hello World"}
L 03/18/2013 - 01:38:30: [testjson.smx] --> Passed
L 03/18/2013 - 01:38:30: [testjson.smx] Trying to decode: {"bool": true}
L 03/18/2013 - 01:38:30: [testjson.smx] --> Passed
L 03/18/2013 - 01:38:30: [testjson.smx] Trying to decode: {"null": null}
L 03/18/2013 - 01:38:30: [testjson.smx] --> Failed
L 03/18/2013 - 01:38:30: [testjson.smx] Running self tests on data...
L 03/18/2013 - 01:38:30: [testjson.smx] --> String: Passed
L 03/18/2013 - 01:38:30: [testjson.smx] --> Boolean: Passed
L 03/18/2013 - 01:38:30: [testjson.smx] --> Integer: Passed
L 03/18/2013 - 01:38:30: [testjson.smx] --> Data tests Passed
'null' was not implemented in the original parser, so I didn't change a thing about that. The important part is that it decodes at least basic JSON just fine.
__________________
Owner of ProbablyAServer, a server without game changing mods and donation benefits

RCON Helper | [TF2] LogUpload | CCC Donator Tags | PHP Steam API Wrapper

Last edited by Nikkii; 06-08-2013 at 08:12.
Nikkii 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 18:40.


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