DJson
From DevDracoBlue
DracoBlue's DJson is a dynamic file based reader/writer for the pawn language. Why Djson as name? Because the saved files are encoded with the JSON-Standard (JavaScriptObjectNotation), which is a very light weight encoding for data structures and originally used to describe javascript objects.
Why use DJson?
- Fast
- DJson uses a database for caching purpose. So you can easily access the whole filesystem, read/write data, because they are already in the memory! Take a look at djAutocommit if you want to use the full power!
- Secure
- You can use anything but "/" parts of the keys, so save whatever you want - no incompatibilites here!
- Files are saved, as soon as the whole data is in the cache, so you won't lose any data, even if you turn of the computer while adding data! (Att: This does not count for the final commit, but it's pretty fast, though :-) )
- Unlimited (512 chars) structured items for paths and content
- In djson you can use pathes, like this/is/a/very/long/......./path/up/to/512/characters.
- In djson you can use so much content as you want, just properly update the DJSON_MAX_STRING, if 512 is still not enough! And of course this limit counts for every single item, so player/vehicle can have unlimited amount of subitems, with again 512 characters!
- Files
- A reason why to use djson is of course, that the data is saved to files! Since djson uses the JSON-standard, you may use JavaScript, PHP, Flash, Java, ... and so on, to read the data!
- Example in Javascript:
player = eval('{"player":{"name":"DracoBlue"}}'); alert(player.name); // "DracoBlue"!
- Arrays/Objects
- In DJson you can use objects (hashtables) and arrays (lists) with values, which can be nested as deep as you want.
- The arrays have some sweet extra functionality, like for example the djCount and the djAppend, which allows you to work with that items, like a general list in any other programming language - and it's all saved!
- Example in Pawn:
djAppend("draco.json","player/vehicles",12); djAppend("draco.json","player/vehicles",13); djAppend("draco.json","player/vehicles",14); print(djCount("draco.json","player/vehicles")) // tell's me that I have 3 vehicles! print(dj("draco.json","player/vehicles/0")) // tell's me that the first (0) is a 12!
- Compatible
- Since JSON is a standard, you may use (edit/read) the produced files with PHP/Flash/JavaScript/Java/C# and create for example an online map, with nearly no effort!
Download
Latest Version of DJson is 1.6.2 and can be found at the Download-Area of DracoBlue.net (djson_1_6_2.zip).
Saving is different to DINI
If you are fimilar with ini-files and used for example dini in the past, you remember the file-content, like:
var1=value var2=value2 spawn.x=val4
... and so on.
Actually creating lists of elements (arrays) is difficult and ini files are not made for that.
In djson files you can write the following:
{ "name":"DracoBlue", "vehicles":["PCJ","Cheetah","Banshee"], "position":{"x":1.2,"y":1.3,"z":10} }
As you can see, we have an object, with the name DracoBlue, a position with x,y and z coordinates and a list of favourite vehicles.
Example
Before we can start, be sure that you have
#include djson
at the top of your script. And added:
djson_OnGameModeInit(); djson_OnGameModeExit();
right in your gamemodeinit or exit function.
General
Use to write anything you want, into the file:
djSet("test.json","player/name","DracoBlue");
And use this to get the data back
print(dj("test.json","player/name")); // "DracoBlue"
Or use this to check if it exists:
if (djIsset("test.json","player") { // we have a player object in there! }
Arrays
Create a file:
words.json
and put the content:
{"words":["hi","hello","hoho"]}
in it.
So let's begin:
new tmp[255]; djSet("words.json","words/1","my Word!"); for (i=0;i<djCount("words.json","words");i++) { format(tmp,255,"words/%d",i); print(dj("words.json",tmp)); } // wil print: hi my Word! hoho
Functions
Djson
use_cached_value bool | Set this to false, if you want to reload the file everytime |
file string | Name of the json file |
path string | Name of the path in the file |
value string | String you want to append |
file string | Name of the file |
use_cached_value bool | Set this to false, if you want to reload the file every time |
path string | Path you want to append to |
toggle bool | Set this true to enable autocommit, set it to false to disable! |
file string | Name of the json file |
use_cached_value bool | Set this to false, if you want to reload the file everytime |
file string | Name of the file |
path string | Path you want to count |
file string | Name of the json file |
use_cached_value bool | Set this to false, if you want to reload the file everytime |
file string | Name of the json file |
path string | Name of the path in the file |
use_cached_value bool | Set this to false, if you want to reload the file everytime |
file string | Name of the json file |
path string | Name of the path in the file |
use_cached_value bool | Set this to false, if you want to reload the file everytime |
file string | Name of the json file |
path string | Name of the path in the file |
file string | Name of the json file |
file string | Name of the json file |
use_cached_value bool | Set this to false, if you want to reload the file every time |
file string | Name of the json file |
new_value string | The new value |
path string | Name of the path in the file |
use_cached_value bool | Set this to false, if you want to reload the file every time |
file string | Name of the json file |
new_value float | The new value |
path string | Name of the path in the file |
use_cached_value bool | Set this to false, if you want to reload the file every time |
file string | Name of the json file |
new_value int | The new value |
path string | Name of the path in the file |
toggle bool | Set this true to enable indented json file output |
use_cached_value bool | Set this to false, if you want to reload the file everytime |
file string | Name of the json file |
path string | Name of the path in the file |
Callback
Private