Raised This Month: $7 Target: $400
 1% 

[EXTENSION] SMJansson 2.3.1/3 (2012-05-07)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 05-06-2012 , 19:32   [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
Reply With Quote #1

This extension wraps Jansson, a C library for encoding, decoding and manipulating JSON data. Jansson is licensed under the MIT license.
As this is based off the 2.3.1 sources currently, I cherry picked some of the younger commits to allow building on windows.
The original sources can be found here.

I've adapted parts of the original documentation for the pawn language and recommend referring to smjansson.inc for SourceMod specific details. Still pretty much of it is copy'n'pasted.
Thanks to akheron for providing such detailed documentation and a nice library in the first place.

The source code is hosted on github.
Changelog can be found here.
Latest binaries here.

Output of the test plugin can be found here.
Attached Files
File Type: zip smjansson_2.3.1.3.zip (66.9 KB, 27302 views)
File Type: zip smjansson_2.3.1.3_binaries.zip (86.3 KB, 25743 views)
__________________
einmal mit profis arbeiten. einmal.

Last edited by Thrawn2; 06-24-2012 at 12:10. Reason: Updated to 2.3.1/3
Thrawn2 is offline
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 05-06-2012 , 19:33   Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
Reply With Quote #2

Creating basic JSON

By using reference stealing methods.
These methods automatically close the Handle of the value-object you are setting/adding/inserting, making your code much cleaner.
Code




Iterating over JSON
Code
__________________
einmal mit profis arbeiten. einmal.

Last edited by Thrawn2; 06-24-2012 at 11:51.
Thrawn2 is offline
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 05-11-2012 , 12:28   Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
Reply With Quote #3

Converting JSON to KeyValues

This is a lossy conversion as KeyValues don't support arrays as JSON does and therefore this example sets the index of the value as the key in the resulting KeyValues structure.
CODE

Converting KeyValues to JSON
CODE



Using json_pack to create JSON

Pack String Rules
  • n Output a JSON null value. No argument is consumed.
  • s Output a JSON string, consuming one argument.
  • b Output a JSON bool value, consuming one argument.
  • i Output a JSON integer value, consuming one argument.
  • f Output a JSON real value, consuming one argument.
  • r Output a JSON real value, consuming one argument.
  • [<packstring>]
    • Build an array with contents from the inner format string.
    • Recursive value building is supported.
    • No argument is consumed.
  • {<packstring>}
    • Build an array with contents from the inner format string.
    • The first, third, etc. format character represent a key, and must be s (as object keys are always strings).
    • The second, fourth, etc. format character represent a value.
    • Recursive value building is supported.
    • No argument is consumed.
Examples
__________________
einmal mit profis arbeiten. einmal.

Last edited by Thrawn2; 06-24-2012 at 11:50.
Thrawn2 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 05-15-2012 , 12:15   Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
Reply With Quote #4

Good job, this can be useful for reading data object via HTTP from sourcemod plugins.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Mikeyfin
Zero Posts
Join Date: May 2012
Old 05-17-2012 , 12:26   Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
Reply With Quote #5

This is awesome man
__________________
Mikey
Mikeyfin is offline
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 06-24-2012 , 12:16   Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
Reply With Quote #6

Updated to 2.3.1/3
  • Implemented json_pack() as stock.
  • Added remaining json types (Boolean & NULL)
  • Added detailed error message when a json string or file could not be loaded
  • Added stocks to create JSON strings according to the formatting rules (json_string_format() & json_string_format_ex()).
  • Added stocks to get values directly from arrays or objects, instead of having to fetch the JSON representation of it first. (json_array_get_* & json_object_get_*)
  • Also fixed json_string() to allow const Strings as parameter.
  • Added the 100th test.
__________________
einmal mit profis arbeiten. einmal.

Last edited by Thrawn2; 06-24-2012 at 12:17.
Thrawn2 is offline
alongub
Veteran Member
Join Date: Aug 2009
Location: Israel
Old 06-28-2012 , 17:28   Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
Reply With Quote #7

If json_load returns INVALID_HANDLE, is there any way to get the error message?
alongub is offline
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 06-28-2012 , 17:47   Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
Reply With Quote #8

assuming you're using 2.3.1/3 it should be in your error log.
i'll add a native returning the errormsg in the next update.
__________________
einmal mit profis arbeiten. einmal.
Thrawn2 is offline
alongub
Veteran Member
Join Date: Aug 2009
Location: Israel
Old 06-28-2012 , 17:50   Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
Reply With Quote #9

Quote:
Originally Posted by Thrawn2 View Post
assuming you're using 2.3.1/3 it should be in your error log.
i'll add a native returning the errormsg in the next update.
Can you also add an optional parameter for json_load that decides whether or not to report to sourcemod error log?

Last edited by alongub; 06-28-2012 at 17:52.
alongub is offline
API
Veteran Member
Join Date: May 2006
Old 07-05-2012 , 14:06   Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
Reply With Quote #10

Great extension. Kind of silly there wasn't a way to parse JSON before.
__________________
API is offline
Send a message via AIM to API
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 03:33.


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