Rob Miracle's "loadsave"-JSON-related method

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

hi Rob, so im enjoying your “loadsave” code that you’ve given us…many thanks!

im just curious if this can be used to save tables too??
for example

myTable.color = “blue”

–this works well, very easy… but…how bout this

myTable.manycolors = {“blue”,“red”,“black”,“cyan”}

–if it does, kindly show us an example how to load the contents of the table…im pretty noob so plss bear with me.  i’ve tried this but dont seem to work

–here’s what i did to test if it can save tables–

myTable.manycolors = {“blue”,“red”,“black”,“cyan”}

loadsave.saveTable(myTable,“myTable.json”)

–so i load the saved datas—

myTable = loadsave.loadTable(“myTable.json”)

for i=1, #myTable.manycolors do

        print(myTable.manycolors[i])

end

— unfortunately, it doesnt load the values that i expect

thanks ROB!

To answer your first question - yes you can definitely save tables to json files, that’s the only thing I use them for.

When you say it doesn’t load the values you expect, do you mean it doesn’t load any values, or it loads them but in an incorrect order?

When I save tables to json files I find that it doesn’t preserve the order of my tables. If I create a table like so:

myTable = {"apple", "banana", "cherry", "date"}

and save it to json, I’ll quite often see the json file like this:

["cherry","banana","date", "apple"]

The only way around this is to either index things manually as you create them (i.e myTable{[1] = “apple”, etc}), or have some other way of handling the data (such as sorting alphabetically, by score etc).

To answer your first question - yes you can definitely save tables to json files, that’s the only thing I use them for.

When you say it doesn’t load the values you expect, do you mean it doesn’t load any values, or it loads them but in an incorrect order?

When I save tables to json files I find that it doesn’t preserve the order of my tables. If I create a table like so:

myTable = {"apple", "banana", "cherry", "date"}

and save it to json, I’ll quite often see the json file like this:

["cherry","banana","date", "apple"]

The only way around this is to either index things manually as you create them (i.e myTable{[1] = “apple”, etc}), or have some other way of handling the data (such as sorting alphabetically, by score etc).

@AlanPlantPot

 thanks brah! my  loadsave test is working!! 

big cheers to Rob Miracle who made this ! :))

Another question regarding the loadsave code

If I store game settings and level scores using loadsave class, will the score and settings be available after I release an update of the app or not? I do not want the user to see his score drop down to zero after updating the app :slight_smile:

Do you have a class that stores data such that if I upgrade the app later, the data is not overwritten and is still accessible.
 

If you store them in a file in the DocumentsDirectory, then they will be not be overwritten when updating the app :slight_smile:

ok great. It saves by default in that directory I guess.

Thanks for the help and speedy answer :slight_smile:

@AlanPlantPot

 thanks brah! my  loadsave test is working!! 

big cheers to Rob Miracle who made this ! :))

Another question regarding the loadsave code

If I store game settings and level scores using loadsave class, will the score and settings be available after I release an update of the app or not? I do not want the user to see his score drop down to zero after updating the app :slight_smile:

Do you have a class that stores data such that if I upgrade the app later, the data is not overwritten and is still accessible.
 

If you store them in a file in the DocumentsDirectory, then they will be not be overwritten when updating the app :slight_smile:

ok great. It saves by default in that directory I guess.

Thanks for the help and speedy answer :slight_smile: