Saving reading data files issue.

Thanks Rob

I deleted the other thread, my questions are getting answered here.

You’ve explained what I didn’t know about the sandbox not being included in the apk, now it makes sense why my files can’t be read.

I’m curious as to why there is a sandbox if it is not used in the final build?

Are my databases secure in the system.ResourceDirectory folder? I wouldn’t want people stealing thousands of multiple choice questions that took me years to make.

Should I copy my databases in main.lua, write them to another more secure directory and then delete them from the resource directory when the app starts first time round? It’s just something that sprang to mind when I read your explanation.

Thanks again.

The sandbox rules are put in place by Apple and Android.  They are read only to prevent people from writing to your app.  If writable folders were included in the app bundles, then it wouldn’t be as secure.

As for protecting your data, you can encrypt the files but that’s about the limit of the protections. 

There really isn’t a more secure location to put your files in that your resource directory.

Hi Rob

I understand what you explained about only the system.ResourceDirectory folder being included in the apk so I moved my json files there.

Here’s where I get stumped again, excuse my ignorance.

I use this code:

local gameData = {}
    local gameData = loadsave.loadTable(“gamedata.json”, system.ResourceDirectory)
    loadsave.saveTable(gameData, “gamedata.json”, system.DocumentsDirectory)

The problem is that your loadsave module doesn’t allow the location system.ResourceDirectory so how am I supposed to load the json file that’s included in my apk?

I’m so confused.

Hi,

I’m also having problems reading json files when I put it in system.ResourceDirectory. The files loads fine when I put it in the sandbox folder however, How do I read json files in the system.ResourceDirectory. Heres my code for loading:

function DataManager.loadTable(filename,directory) local path=system.pathForFile(filename,directory) local contents="" local myTable={} print(file) local file=io.open(path,"r") print(file) if(file) then print("loading file") local contents=file:read("\*a") --print(contents) myTable=json.decode(contents) io.close(file) return myTable end return nil end

Thanks,

-Tristan

Hi Tristan

It took me many hours to figure out how to load and save data using json but I cracked it thanks to some good advice from Rob Miracle.

What I did first was to create a text file in notepad on my computer (players.txt) with all the basic data to be written to later, basically just a list of generic scores and names, and saved that text file into the same folder where all my lua modules and images are.

The text file looks like this…

10000

9000

8000

…               --and so on

Mr Jones

Mrs Jones

Mr Smith

…               --and so on

10

9

8

…               --and so on

Then read that text file and put the contents into a table ( which I called playerData{} ) like so…

 local \_path = system.pathForFile( "players.txt", system.ResourceDirectory ) local \_file = io.open( \_path, "r" ) local counter = 1 for line in \_file:lines() do if counter \< 11 then playerData.HighScores[counter] = tonumber( line ) end if counter == 11 then playerData.storedPlayers[1] = tostring( line ) end if counter \> 11 and counter \< 16 then playerData.currentPlayers[counter-11] = tostring( line ) end if counter \> 15 and counter \< 26 then playerData.HighScoreLevels[counter-15] = tonumber( line ) end if counter \> 25 then playerData.HighScoreNames[counter-25] = tostring( line ) end counter = counter + 1 end playerData.storedPlayersCount = 1 playerData.NumberOfHighScores = 10 io.close( \_file ) \_file = nil \_path = nil counter = nil line = nil

As you can see at the bottom of my code I also added more data to my playerData table.

Then I saved my table to a json file in the Documents Directory (Sandbox)

loadsave.saveTable(playerData, "players.json", system.DocumentsDirectory )

I have not ever needed to decode the contents by the way ( myTable=json.decode(contents) )

I just load them into a table using Rob Miracle’s “loadsave” module.

local loadsave = require("loadsave") gameData = {} gameData = loadsave.loadTable("gamedata.json", system.DocumentsDirectory )

The thing to note here is that your sandbox (Documents directory) should be empty when you build for device.

I delete the created files in the sandbox folder before I build because they will be created when your app is on the actual device.

Even though you can’t see them they will be there to read from and write to just like in the simulator.

I hope this helps.

Hi,

I’m also having problems reading json files when I put it in system.ResourceDirectory. The files loads fine when I put it in the sandbox folder however, How do I read json files in the system.ResourceDirectory. Heres my code for loading:

function DataManager.loadTable(filename,directory) local path=system.pathForFile(filename,directory) local contents="" local myTable={} print(file) local file=io.open(path,"r") print(file) if(file) then print("loading file") local contents=file:read("\*a") --print(contents) myTable=json.decode(contents) io.close(file) return myTable end return nil end

Thanks,

-Tristan

Hi Tristan

It took me many hours to figure out how to load and save data using json but I cracked it thanks to some good advice from Rob Miracle.

What I did first was to create a text file in notepad on my computer (players.txt) with all the basic data to be written to later, basically just a list of generic scores and names, and saved that text file into the same folder where all my lua modules and images are.

The text file looks like this…

10000

9000

8000

…               --and so on

Mr Jones

Mrs Jones

Mr Smith

…               --and so on

10

9

8

…               --and so on

Then read that text file and put the contents into a table ( which I called playerData{} ) like so…

 local \_path = system.pathForFile( "players.txt", system.ResourceDirectory ) local \_file = io.open( \_path, "r" ) local counter = 1 for line in \_file:lines() do if counter \< 11 then playerData.HighScores[counter] = tonumber( line ) end if counter == 11 then playerData.storedPlayers[1] = tostring( line ) end if counter \> 11 and counter \< 16 then playerData.currentPlayers[counter-11] = tostring( line ) end if counter \> 15 and counter \< 26 then playerData.HighScoreLevels[counter-15] = tonumber( line ) end if counter \> 25 then playerData.HighScoreNames[counter-25] = tostring( line ) end counter = counter + 1 end playerData.storedPlayersCount = 1 playerData.NumberOfHighScores = 10 io.close( \_file ) \_file = nil \_path = nil counter = nil line = nil

As you can see at the bottom of my code I also added more data to my playerData table.

Then I saved my table to a json file in the Documents Directory (Sandbox)

loadsave.saveTable(playerData, "players.json", system.DocumentsDirectory )

I have not ever needed to decode the contents by the way ( myTable=json.decode(contents) )

I just load them into a table using Rob Miracle’s “loadsave” module.

local loadsave = require("loadsave") gameData = {} gameData = loadsave.loadTable("gamedata.json", system.DocumentsDirectory )

The thing to note here is that your sandbox (Documents directory) should be empty when you build for device.

I delete the created files in the sandbox folder before I build because they will be created when your app is on the actual device.

Even though you can’t see them they will be there to read from and write to just like in the simulator.

I hope this helps.

Thanks! The solution saved my day, as I was stuck for many hours.

Thanks! The solution saved my day, as I was stuck for many hours.