lua中的Serialization

本文探讨了Lua中的Serialization技术,包括如何将数据转换为可存储或传输的格式,以及如何从这种格式恢复数据。内容涵盖了基础概念、内置的串行化库以及在实际项目中应用序列化的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--Serialization   写成流可以和网络进行联通
function serialize( o )
    if type( o ) == "number" then
        io.write(o)
    elseif type(o) == "string" then
        io.write("[[",o,"]]")
    end
end
a = 'a, "hello lua" \\'
--print(string.format("%q",a))
--lua 5.1中的 [=[...]=]   
--print([=['a, "hello lua" \\']=])
--保存无环的table
function n_serialize( o )

    if  type(o) == "number" then 
        io.write(o)

    elseif type(o) == "string" then
        io.write(string.format("%q",o))

    elseif type(o) =="table" then
        io.write("{\n")

        for k,v in pairs(o) do
            io.write("  ",k,"=")
            n_serialize( v )
            io.write(",\n") 
        end

        io.write("}\n")
    end
end
 n_serialize{a =12,b ='lua'}
--lua可以帮助你去扩展,简单的串行化;
--lua脚本和txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值