-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadsave.lua
More file actions
65 lines (50 loc) ยท 1.52 KB
/
Copy pathloadsave.lua
File metadata and controls
65 lines (50 loc) ยท 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--๊ฒ์ ์ ์ฅ๊ณผ ๋ก๋๋ฅผ ์ํ ํ์ผ
-- https://docs.coronalabs.com/tutorial/data/jsonSaveLoad/index.html ํ์ด์ง ์ฐธ๊ณ
local M = {}
local json = require( "json" )
local defaultLocation = system.DocumentsDirectory
function M.saveTable( t, filename, location )
local loc = location
if not location then
loc = defaultLocation
end
-- Path for the file to write
local path = system.pathForFile( filename, loc )
-- Open the file handle
local file, errorString = io.open( path, "w" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
return false
else
-- Write encoded JSON data to file
file:write( json.encode( t ) )
-- Close the file handle
io.close( file )
return true
end
end
function M.loadTable( filename, location )
local loc = location
if not location then
loc = defaultLocation
end
-- Path for the file to read
local path = system.pathForFile( filename, loc )
-- Open the file handle
local file, errorString = io.open( path, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Read data from file
local contents = file:read( "*a" )
-- Decode JSON data into Lua table
local t = json.decode( contents )
-- Close the file handle
io.close( file )
-- Return table
return t
end
end
return M