[ad_1]
This resource makes it possible to manage JSON files through simple exports.
Github:
Usage
This is the same ReadMe.md that is on the github page.
Json handler is a resource which is capable of writing and getting data from json files.
Usage
There are 2 methods for calling the functions in this resource from anywhere.
-
Using events. There are 3 server events declared in the resourece which are:
-
json:getFile Expects 1 variable. The
filename
(without extension) and it returns acb
value.Examples:
TriggerEvent("json:getFile", "users", function(cb) print(cb[steamid].name) end)
This example returns the name according to the steamid from the users table.
steamid
is a variable. -
json:getItem Expects 2 variables. The
filename
(without extension), thename of the item
you want and it returns acb
value.Examples:
TriggerEvent("json:getItem", "users", steamid, function(cb) print(cb.name) end)
This example print out the same as the last one, but it only returns the item that is called
steamid
.steamid
is a variable -
json:addItem Expects 3 variables. The
filename
(without extension), thename of the item
you want to create and alua table
with the data of your item, and their value.Examples:
TriggerEvent("json:addItem", "users", steamid, { name = "example", example = 5 })
This example creates an item in
users.json
called steamid with the givem parameters.steamid
is a variable -
json:deleteItem Expects 2 variables. The
filename
(without extension) and thename of the item
you want to delete.Examples:
TriggerEvent("json:deleteItem", "users", steamid)
This example deletes the
steamid
item fromusers.json
. -
json:replaceItem Expects 3 variables. The
filename
(without extension), thename of the item
you want to replace and alua table
with the data of your item, and their value.Examples:
TriggerEvent("json:replaceItem", "users", steamid, { name = "example", example = 6 })
This example replaces an item in
users.json
calledsteamid
with the givem parameters.steamid
is a variable -
json:replaceData Expects 3 variables. The
filename
(without extension), thename of the item
you want to replace in and alua table
with another lua table inside it. The second table has 2 variables. name and content. You will understand it form the example.Examples:
TriggerEvent("json:replaceData", "users", steamid, { { name = "name", content = "bence" }, { name = "money", content= 10 } })
This example replaces the
name
and themoney
inusers.json
steamid
is a variable-
json:deleteData Expects 3 variables. The
filename
(without extension), thename of the item
you want to do the delete in and the name of the data you want to delete.Examples:
TriggerEvent("json:deleteData", "users", steamid, "money")
This example deletes the data named
money
from thesteamid
item inusers.json
in the
steamid
item with the corresponding content.
-
-
Using exports. There is an option to import a class using exports.
The class work like this:
local file = exports.json_handler:InitiateFile(filename)
Then if you copied in this line, you can access 7 functions. These are:
-
file.GetFile()
-
file.GetItem(itemname)
-
file.AddItem(itemname, content)
-
file.DeleteItem(itemname)
-
file.ReplaceItem(itemname, itemcontent)
-
file.ReplaceData(itemname, table)
-
file.DeleteData(itemname, dataname)
Note that you can use whatever variable name you want
Note that these functions do the same, as the events mentioned above. If you do not understand what they do, have a look at the corresponding event.
I suggest using exports if you executing the request on the server side, becouse they are more resource friendly.
-
Note that files need to be created by the developer, who plans to use this, and it can contain anything that json can. I used users in the examples, but you might not have that file.
copyright © 0324bence
[ad_2]
Original source: https://forum.cfx.re/t/json-handler-for-fivem/1974389