Advertisement
Brodur

jsonutil.lua

Apr 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.73 KB | None | 0 0
  1. --- JsonUtil
  2. -- Provides utility functions for interacting with JSON
  3. -- @Author: Brodur
  4. -- @Version: 1.0
  5. local json = require("json")
  6. local fs = require("filesystem")
  7. local io = require("io")
  8. local ju = {}
  9.  
  10. --- Load Json
  11. -- Loads the JSON database from file
  12. -- @param dir  The directory where the database is located.
  13. -- @return The parsed JSON table.
  14. function ju.load(dir)
  15.   str = ""
  16.   for line in io.lines(dir) do str = str .. line end
  17.   return json.parse(str)
  18. end
  19.  
  20. --- Save Json
  21. -- Save the contents of given table to JSON file.
  22. -- @param tbl  The table to save.
  23. -- @param dir  Where to save the table.
  24. function ju.save(tbl, dir)
  25.   file = io.open(dir, "w")
  26.   file:write(json.stringify(tbl))
  27.   file:close()
  28. end
  29.  
  30. return ju
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement