AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   [EXTENSION] SMJansson 2.3.1/3 (2012-05-07) (https://forums.alliedmods.net/showthread.php?t=184604)

Thrawn2 05-06-2012 19:32

[EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
 
2 Attachment(s)
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.

Thrawn2 05-06-2012 19:33

Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
 
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

Thrawn2 05-11-2012 12:28

Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
 
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

berni 05-15-2012 12:15

Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
 
Good job, this can be useful for reading data object via HTTP from sourcemod plugins.

Mikeyfin 05-17-2012 12:26

Re: [EXTENSION] SMJansson 2.3.1/1 (2012-05-07)
 
This is awesome man

Thrawn2 06-24-2012 12:16

Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
 
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.

alongub 06-28-2012 17:28

Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
 
If json_load returns INVALID_HANDLE, is there any way to get the error message?

Thrawn2 06-28-2012 17:47

Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
 
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.

alongub 06-28-2012 17:50

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

Originally Posted by Thrawn2 (Post 1738442)
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?

API 07-05-2012 14:06

Re: [EXTENSION] SMJansson 2.3.1/3 (2012-05-07)
 
Great extension. Kind of silly there wasn't a way to parse JSON before.


All times are GMT -4. The time now is 11:53.

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