[Include] DJson 1.6.2 - Fast/Extensive File Reader/Writer (Official DiniІ) (Works in FS)
#1

DJson
Version 1.6.2 (Changelog)
Dini got downloaded nearly 15.000 already! Big thank you to everyone trusting in it!

Today I want to publish the next level of saving data to files. DJson is the library to save more then just little text-strings to files. It contains a cache to make your advanced gamemodes faster, it contains objects/arrays and a transaction based environment (djAutocommit) to override all speed/space/structure limits of dini. With approx 1.700 lines of code its, way bigger then the 250loc dini .

And last but not least: Its published under MIT-License. Use it, love it, hack it, sell it!

I really hope and want to keep what most of you connect with my releases: reliable code! This library walked through massive api tests and technical proofs, so it should work as expectd. Please report if you got any issues, so all others have benefit, too!

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).

Why 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!
    • DJSON_MAX_STRING should be at least 170 + maxfilenamelength + maxkeylength + maxvaluelength
  • 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!
  • 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!
  • 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 (see dmap), with nearly no effort!
References
- Official DJson-Page
- Examples
- Function List
- Tweaking DJson
- DJson-Blog in my DevDiary

Tutorials
- Register/Login System Using DJson
- Why not use ONE big file for all user data

Known Issues
- none

Usage
I added a small test-script, called stats.pwn to the release, which counts server restarts and player-connect/deaths.

And since I know, most of you guys are too lazy to read the entire API-Documentation at http://web.archive.org/web/201404151...ndex.php/DJson , here comes a quick and dirty script:
Includes!
pawn Код:
#include <a_samp>
#include <djson>
main() {
}
Register the callbacks:
pawn Код:
public OnGameModeInit() {
    djson_GameModeInit(); // <-- this must be called _before_ you can use any djson function!!
   
    djSetInt("stats.json","server/started",djInt("stats.json","server/started")+1);

    printf("Server starts: %d",djInt("stats.json","server/started"));
   
    SetGameModeText("Blank Script");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    return 1;
}

public OnGameModeExit() {
    djson_GameModeExit(); // <-- when you exit the gamemode, after that you can't run any djson function anymore!
    return 1;
}
And now some counting:
pawn Код:
public OnPlayerRequestClass(playerid, classid) {
    SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
    return 1;
}

public OnPlayerConnect(playerid) {
    djSetInt("stats.json","players/connected",djInt("stats.json","players/connected")+1);
    return 1;
}

public OnPlayerSpawn(playerid) {
    djSetInt("stats.json","players/spawned",djInt("stats.json","players/spawned")+1);
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason) {
    djSetInt("stats.json","players/died",djInt("stats.json","players/died")+1);
    return 1;
}
Have fun coding,
Jan (DracoBlue)
Reply
#2

Nice one DracoBlue, especially the counting!
[me=Wadabak]is going to convert my codes [/me]
Reply
#3

Oh shit.
Reply
#4

Awesome!!
Reply
#5

well done
Reply
#6

Another nice release Draco! Thanks =)
Reply
#7

Glad if you like it.

I am currently working on a (free) web-based-auto-updating map using that as data-source.

[edit] DJson 1.1 released!
* fixed bug in djSet if setting Array-Elements with children
* added djCreateFile
* added djRemoveFile

Djson 1.2 released!
* fixed crash in djUnset, when unsetting the last array element.

- Draco
Reply
#8

Damn, this is nice. I've just had a better look at it, now I can have all accounts in just one file!

Question: Is it possible to delete 1 string of a line? I'll give you an example.

Код:
"vehicles":["PCJ","Cheetah","Banshee"],
Like deleting 'Cheetah' of that vehicles line so it would become this:

Код:
"vehicles":["PCJ","Banshee"],
Thanks.
Reply
#9

Quote:
Originally Posted by Wadabak
Damn, this is nice. I've just had a better look at it, now I can have all accounts in just one file!

Question: Is it possible to delete 1 string of a line? I'll give you an example.

Код:
"vehicles":["PCJ","Cheetah","Banshee"],
Like deleting 'Cheetah' of that vehicles line so it would become this:

Код:
"vehicles":["PCJ","Banshee"],
Thanks.
yes. djUnset("filename","vehicles/1"); // to delete the 2nd item.

- Draco
Reply
#10

cool, great! Way better as dini
Reply
#11

OMFG, this is great! DracoBlue, you are the best!
Reply
#12

nice,gonna use it
Reply
#13

I am now trying more then 2 hours to get some thing right .

ATM I have this piece of code:

pawn Код:
GetPlayerName(playerid,player,24);
format(str, sizeof(str), "%s/Health", player);
djSetInt("Accounts.json", str, udb_hash(params));
format(str, sizeof(str), "%s/Money", player);
Now this is working good, it will show this in the file:

Код:
{"Antironix":{"Health":100.000000,"Money":0}}
But how can I make it that it is writing to a new line? Like it should show this:

Код:
{"Antironix":{"Health":100.000000,"Money":0}}
{"Player2":{"Health":100.000000,"Money":0}}
..etc...
Thanks It's just a new kind of code for me.
Reply
#14

You are trying already the right thing :). Do not be confused by the way it is displayed!

The problem you are facing is, that the root is always (I do not intend to change that, though) an object (displayed by { ... }) and not an array (displayed by [nobbc][[/nobbc] ... [nobbc]][/nobbc]).

But: what you want to do does not depend on anything like arrays or objects, because it works even better like you already do that with objects and exactly the way you make that :-P!

pawn Код:
djSetInt("Accounts.json", "Draco/Health",100);
djSetInt("Accounts.json", "Draco/Money",10000);
djSetInt("Accounts.json", "Wadabak/Health",30);
djSetInt("Accounts.json", "Wadabak/Money",500);
... the Accounts.json looks (easily) like that:
{"Draco":{"Health":100,"Money":10000},"Wadabak:{"H ealth":30,"Money":500}}

Let's have a close look at this:
{
"Draco":{
"Health":100,
"Money":10000
},
"Wadabak:{
"Health":30,
"Money":500
}
}

So it's already what you wanted to have, just smaller saved (for space/speed reasons).

- Draco
Reply
#15

Ah, I got it.

I've did this:
1) Registered my self, it was writen correctly.
2) Changed my name in the file and saved it.
3) Reregistered myself.

But as the changed name of the file hasn't been reloaded in the cache the system didn't knew it and overwrited the previous one.


A question: Is the long delay normal, like ~1-2 seconds with 8 writings?

This is not a problem because I am going to use this only in register/login/disconnect so the player won't notice it during his gameplay.

Also if you don't mind, can I release the register system I am making? Because this may be confusing for new users.

Thanks again

EDIT: I have forgotten one question.

Is it possible to have a better overlooking code, like you just posted at last? (I guess not xD)
Reply
#16

Omg Nice!
Reply
#17

Quote:
Originally Posted by Wadabak
I've did this:
1) Registered my self, it was writen correctly.
2) Changed my name in the file and saved it.
3) Reregistered myself.

But as the changed name of the file hasn't been reloaded in the cache the system didn't knew it and overwrited the previous one.
So, if you want to do that, better make it different :-P. Changing names is always hard, if you save the data on that name.

If you want to reload the info, better use DJSON_cache_ReloadFile(filename[]) once or use for example dj(file[],path[],use_cached_value) with use_cached_value = false, this will "force" to load from file always. No caching is used here. Even though I do not suggest that for big files.

Quote:
Originally Posted by Wadabak
A question: Is the long delay normal, like ~1-2 seconds with 8 writings?

This is not a problem because I am going to use this only in register/login/disconnect so the player won't notice it during his gameplay.
So here comes the reason, why you should use djAutocommit and djCommit!

In first place do:
pawn Код:
djAutocommit(false); // after that line, it will only flush the data on djCommit
djSetInt("Accounts.json", "Draco/Health",100);
djSetInt("Accounts.json", "Draco/Money",10000);
... more ...
djSetInt("Accounts.json", "Wadabak/Health",30);
djSetInt("Accounts.json", "Wadabak/Money",500);
djCommit("Accounts.json"); // at this point, it will "flush" all the data into the file!
djAutocommit(true); // after that line, it will automactily flush the data on each call
This is way way faster :). Just for your information, if you do djInt("Accounts.json","Draco/Health") while not having commited the data yet, it will show the current value anyways. This is very helpful if you just want to write some data into the cache and are not sure if you want to keep it :).

Quote:
Originally Posted by Wadabak
Also if you don't mind, can I release the register system I am making? Because this may be confusing for new users.
Of course.

- Draco
Reply
#18

just a question what does "db_query" do ? it looked for me as it would be a standart function because I didnt found any stock public
Reply
#19

Quote:
Originally Posted by Nero_3D
just a question what does "db_query" do ? it looked for me as it would be a standart function because I didnt found any stock public
db_query can be found in sampdb.inc and is about database queries to the built in sqlite-database.

- Draco
Reply
#20

I'm lovin it.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)