DJson

From DevDracoBlue

Jump to: navigation, search
DJson

- Mainpage
- Changelog (Version 1.6.2)
- Tweaking

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

dj (file[],path[],use_cached_value=true) : string
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
Reads a specific value from a json-File by path Example: dj("draco.json","player/name") will return "DracoBlue" if the draco.json looks like that: {"player":{"name":"DracoBlue"}}


djAppend (file[],path[],value[],use_cached_value=true) : bool
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
Append an item to a djson-Array


djAutocommit (toggle)
toggle bool Set this true to enable autocommit, set it to false to disable!
Activates and deactivates djson autocommit


djCommit (file[]) : bool
file string Name of the json file
Commit changes to a json file to the filesystem


djCount (file[],path[],use_cached_value=true) : int
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
Calculate the Items of a djson-Array


djCreateFile (file[]) : bool
file string Name of the json file
Creates a new dj-File


djFloat (file[],path[],use_cached_value=true) : float
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
Reads a specific value from a json-File by path Example: djInt("draco.json","player/posx") will return 15.5 if the draco.json looks like that: {"player":{"name":"DracoBlue","pos":{"x":15.5,"y":20.0"}}}


djInt (file[],path[],use_cached_value=true) : int
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
Reads a specific value from a json-File by path Example: djInt("draco.json","player/id") will return 2 if the draco.json looks like that: {"player":{"name":"DracoBlue","id":2}}


djIsSet (file[],path[],use_cached_value=true) : bool
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
Checks if a specific path in the json-file exists Example: djIsSet("draco.json","player/name") will return true if the draco.json looks like that: {"player":{"name":"DracoBlue"}}


djRemoveFile (file[]) : bool
file string Name of the json file
Removes a dj-File


djRevert (file[]) : bool
file string Name of the json file
Revert changes from a json file and reload from disk


djSet (file[],path[],new_value[],use_cached_value=true) : bool
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
Set a specific path string value Example: djSet("draco.json","player/name","Hans") will set {"player":{"name":"Hans"}}


djSetFloat (file[],path[],Float:new_value,use_cached_value=true) : bool
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
Set a specific path integer value Example: djSetFloat("draco.json","player/pos/x",15.5) will set {"player":{"name":"Hans","pos":{"x":15.5}}}


djSetInt (file[],path[],new_value,use_cached_value=true) : bool
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
Set a specific path integer value Example: djSetInt("draco.json","player/id",5) will set {"player":{"name":"Hans","id":5}}


djStyled (toggle)
toggle bool Set this true to enable indented json file output
Activates and deactivates djson styled output


djUnset (file[],path[],use_cached_value=true) : bool
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
Unsets a specific path in a json-file Example: djUnset("draco.json","player/pos") will unset {"player":{"name":"DracoBlue"}}

Callback

Destroys the djson-Engine, _must_ be executed on gamemode exit!


Initializes the djson-Engine, _must_ be executed on gamemode init!



Private





DJSON_cache_createArrayNode (file[],key[],parent,pos=-1)


DJSON_cache_createNode (file[],key[],type_id,value[],parent,pos=-1)


DJSON_cache_createNumber (file[],key[],value,parent,pos=-1)


DJSON_cache_createNumberFromStr (file[],key[],value[],parent,pos=-1)


DJSON_cache_createObjectNode (file[],key[],parent,pos=-1)


DJSON_cache_createString (file[],key[],value[],parent,pos=-1)



DJSON_cache_debug_save_file (file[],tofile[])



DJSON_cache_putchartofile (File:fhnd,stream[DJSON_MAX_STRING],&stream_pos,c)


DJSON_cache_putstringtofile (File:fhnd,stream[DJSON_MAX_STRING],&stream_pos,str[])




DJSON_cache_updateNode (file[],key[],type_id,value[])


DJSON_cache_updateNumber (file[],key[],value)


DJSON_cache_updateNumberFromStr (file[],key[],value[])


DJSON_cache_updateString (file[],key[],value[])


DJSON_implode (c,path_pos)



DJSON_prepareTreeForPath (file[],path[]) : int


DJSON_ret_memcpy (source[],index=0,numbytes)



DJSON_strreplace (trg[],newstr[],src[])
Personal tools
In other languages