优雅的LUA数据记录方法-serpent序列化+LUA Table

目录

简述

项目里需要使用LUA脚本将数据记录到文件,要方便的增加、查找、删除,要方便的加载与存回。

使用序列化/反序列化 + lua table可以很容易实现这些功能。

序列化将table转换为字符串

反序列化将table从字符串中恢复出来

如何集成?

进入https://github.com/pkulchenko/serpent,下载serpent.lua, 将这个文件复制到lua可见的地方,比如工程目录一级路径。我这里放到了LUA的同级路径,通过测试发现可以require到这个模块。

如何使用

序列化或者反序列化之前,要先加载serpent模块

lua 复制代码
serpent_handle = require "serpent"
assert(serpent_handle)

序列化

首先,我们构建一个即将序列化的table,table中包含按照下标顺序递增的数据,包含字典数据,包含子表,内容如下

lua 复制代码
local Send_Table = {
    1,
    2,
    3,
    ["name"] = "winston",
    age = 16,
    child_table = {
        hair_length = 1000,
        hair_color = "Black"
    }
}

serpent提供三种序列化API,dump,line, block,三者没有太多的区别,dump是全功能的,line和block没有办法做表的自我引用。

向这些函数中输入一个表作为形参,函数将会返回序列化之后的字符串

我们把每一种都打印出来观察一下

复制代码
--store_string = serpent_handle.dump(Send_Table)
--print(store_string)

do local _={[1]=1,[2]=2,[3]=3,name="winston",age=16,child_table={hair_color="Black",hair_length=1000}};return _;end

--store_string = serpent_handle.line(Send_Table)
--print(store_string)

{1, 2, 3, age = 16, child_table = {hair_color = "Black", hair_length = 1000} --[[table: 000002a2d1245120]], name = "winston"} --[[table: 000002a2d12458a0]]

--store_string = serpent_handle.block(Send_Table)
--print(store_string)

{
  1,
  2,
  3,
  age = 16,
  child_table = {
    hair_color = "Black",
    hair_length = 1000
  } --[[table: 0000028afdd050e0]],
  name = "winston"
} --[[table: 0000028afdd05a20]]

可以看出,dump似乎是一个函数,返回定义的local表_

line像是把表的内存都定义到了,不知道有什么用,所有信息记录到一行。

block就是line插入回车的版本。

拿到这些字符串之后,我们就可以将其存入文件保存下来了,文件I/O不在本文讨论范围,参考https://www.runoob.com/lua/lua-file-io.html

好吧,为了下一节的流畅叙述,还是贴个代码

lua 复制代码
file_handle = io.open("store.txt", "w+")
io.output(file_handle)
io.write(store_string)
io.close()

反序列化

我们把store.txt里的内容修改一下,

我们希望把修改过的数据反序列化到另一个表Recv_Table里,并且打印各个成员

反序列化使用如下:反序列化只有一个形参string,有两个返回值ok和res。ok指示反序列化是否成功,res是反序列化后的表

lua 复制代码
ok, res = serpent.load(string)

测试代码如下:

lua 复制代码
serpent_handle = require "serpent"
assert(serpent_handle)

file_handle = io.open("store.txt", "r")
io.input(file_handle)
local ok, Recv_Table = serpent_handle.load(io.read("a*"))
io.close()

print("Recv_Table[1]:"..Recv_Table[1])
print("Recv_Table[2]:"..Recv_Table[2])
print("Recv_Table[3]:"..Recv_Table[3])
print("age:"..Recv_Table.age)
print("hair_color:"..Recv_Table.child_table.hair_color)
print("name:"..Recv_Table.name)

现象如下

参考

更多信息,参考https://github.com/pkulchenko/serpent

相关推荐
神经毒素13 分钟前
WEB安全--Java安全--LazyMap_CC1利用链
java·开发语言·网络·安全·web安全
酷炫码神38 分钟前
C#语法基础
开发语言·c#
ddd...e_bug39 分钟前
GMT之Bash语言使用
开发语言·bash
码农秋41 分钟前
填坑记: 古董项目Apache POI 依赖异常排除
开发语言·tomcat·jsp·poi·依赖冲突
qq_6536444642 分钟前
如何查看打开的 git bash 窗口是否是管理员权限打开
开发语言·windows·git·bash
sadoshi43 分钟前
phpstudy的Apache添加AddType application/x-httpd-php .php .php5配置无效的处理方式
开发语言·php·apache
阑梦清川1 小时前
关于Go语言的开发环境的搭建
开发语言·后端·golang
言之。1 小时前
Makefile 在 Go 项目中的实践
开发语言·elasticsearch·golang
Nobkins1 小时前
2023CCPC河南省赛暨河南邀请赛个人补题ABEFGHK
开发语言·数据结构·c++·算法·图论
南部余额1 小时前
Python 类变量与实例变量完全指南:区别、使用场景及常见陷阱
开发语言·python