标签:style color io java strong for 文件 数据 sp
概念普及数据的持久化是序列化的又一个典型的应用,对象只有在序列化之后才能进行持久化存储,从持久化存储介质加载的数据通过反序列化转变成运行时对象。
local str_serialize = "" local function serialize (o) str_serialize = str_serialize or "" if o == nil then io.write("nil") str_serialize = str_serialize.."nil" return end if type(o) == "number" then io.write(o) str_serialize = str_serialize..o elseif type(o) == "string" then io.write(string.format("%q", o)) str_serialize = str_serialize..string.format("%q", o) elseif type(o) == "table" then io.write("{\n") str_serialize = str_serialize.."{\n" for k,v in pairs(o) do io.write(" ["); str_serialize = str_serialize.." [" serialize(k); io.write("] = ") str_serialize = str_serialize.."] = " serialize(v) io.write(",\n") str_serialize = str_serialize..",\n" end io.write("}") str_serialize = str_serialize.."}" elseif type(o) == "boolean" then io.write( o and "true" or "false" ) str_serialize = str_serialize..(o and "true" or "false") elseif type(o) == "function" then io.write( "function" ) str_serialize = str_serialize.."function" else error("cannot serialize a " .. type(o)) end return end local ddd = {a = 12,b = "Lua",key = "another \"one\"",d = false} serialize(ddd) print("") print(str_serialize)结果
{ ["a"] = 12, ["d"] = false, ["key"] = "another \"one\"", ["b"] = "Lua", } { ["a"] = 12, ["d"] = false, ["key"] = "another \"one\"", ["b"] = "Lua", }
标签:style color io java strong for 文件 数据 sp
原文地址:http://blog.csdn.net/shimazhuge/article/details/39583945