clugg / sm-json

A pure SourcePawn JSON encoder/decoder.
GNU General Public License v3.0
82 stars 8 forks source link

Prettify / Pretty printing #2

Closed chte closed 5 years ago

chte commented 5 years ago

Hi,

first of all great job. I truly appreciate the work you have done!

I would like to request that json_encode includes a "prettify" parameter for pretty printing. Can be quite useful for debugging purposes.

clugg commented 5 years ago

I like the idea - I initially did not implement this because encoding was purely for sending data to APIs, so pretty did not matter to me. However, I have found myself time and time again dumping data to the console so it would definitely be a nice-to-have.

clugg commented 5 years ago

As per the README, you can now pretty print output by passing true to json_encode. For example:

json_encode(object, output, sizeof(output), true);
// OR
object.Encode(output, sizeof(output), true);

By default it will put 1 space after colons, 4 spaces for indentation and \n for new lines. You can modify the constants defined in addons/sourcemod/scripting/include/json/definitions.inc if you wish to modify this behaviour. Let me know if you have any trouble with this - it is not heavily tested but from what I can see it works fine.