I found some JSON code that works pretty well - http://www.chipmunkav.com/downloads/Json.lua
I tried the JSON4Lua library first, but for some reason it crashes on the device (not in the simulator..)
JSON is a nice way to serialize your objects for saving. For example:
function readScores()
local path = system.pathForFile( "scores.txt", system.DocumentsDirectory )
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
scoresList = Json.Decode(contents);
io.close( file )
end
end
function saveScores()
local path = system.pathForFile( "scores.txt", system.DocumentsDirectory )
-- create file b/c it doesn't exist yet
local file = io.open( path, "w" )
if file then
file:write( Json.Encode(scoresList) )
io.close( file )
end
end
It is also useful for communicating with web-based services.
And I was just about to use JSON4Lua today too -- I'll start off with this other JSON code instead. Thanks for the tip dwallin3!