当前位置: 首页 > news >正文

做搜狗手机网站点击软武汉做seo公司

做搜狗手机网站点击软,武汉做seo公司,关键词,wordpress 分类目录置顶我们经常需要序列化一些数据,为了将数据转换为字节流或者字符流,这样我们就可以保存到文件或者通过网络发送出去。我们可以在 Lua 代码中描述序列化的数据,在这种方式下,我们运行读取程序即可从代码中构造出保存的值。 number/st…

我们经常需要序列化一些数据,为了将数据转换为字节流或者字符流,这样我们就可以保存到文件或者通过网络发送出去。我们可以在 Lua 代码中描述序列化的数据,在这种方式下,我们运行读取程序即可从代码中构造出保存的值。

number/string

对于number和string类型其实非常好序列化,number类型就直接返回其本身即可:

if type(o) == "number" thenreturn o
end

string类型需要专门处理,有一个正则匹配是%q,是一种字符串格式化表达式,为了防止类似os.execute('rm *')的注入式攻击,所以采用这种匹配形式来完成string的序列化:

if type(o) == "number" thenreturn string.format("%q",o)
end

若采用类似于("[[", o, "]]")的形式来实现序列化,那么如果输入是" ]]..os.execute('rm *')..[[ ",最后拼接结果则是[[ ]]..os.execute('rm *')..[[ ]],load之后则会出现严重的后果。

   table

{["b"] = "Lua",["a"] = 12,["key"] = {["b"] = 4,["a"] = 3,},1 = 2,
}

如果是table类型,如果按照以上形式来呈现序列化效果,则需要注意嵌套table和缩进格式。

function serialize(o,strPrefix)strPrefix = strPrefix or ""if type(o) == "number" thenreturn oelseif type(o) == "string" thenreturn string.format("%q",o)elseif type(o) == "table" thenlocal result = "{\n"for k,v in pairs(o) dolocal strFormat = "%s"if type(k) == "string" thenstrFormat = "[\"%s\"]"endresult = result..strPrefix.."\t"..string.format(strFormat,k).." = "..serialize(v,strPrefix.."\t")..",\n"endresult = result..strPrefix.."}"return resultelse error("cannot serialize a " .. type(o)) end
end

通过pairs循环来对key value值一个个序列化,其中strFormat指的是如果key的类型是string才需要加上【】,以确保正确序列化key值。如果是嵌套table则需要注意递归序列化,传入这个新的table和缩进用以保证正常的序列化输出。于是输入和输出如下:

local tb = 
{["a"] = 12, ["b"] = "Lua", [1] = 2,["key"] = {["a"] = 3,["b"] = 4,} 
}
print(serialize(tb))
{["b"] = "Lua",["a"] = 12,["key"] = {["b"] = 4,["a"] = 3,},[1] = 2,
}

如果看programming in lua原文,其实大差不差,只是原文没有缩进格式的优化。

循环table

如果出现循环引用table的形式,那么整个问题将会变得比较复杂一点,比如:

local a = {}
a.c = 1
a.z = a

由此可以在之前的基础上做一个优化:缓存table map。意指当我们遍历里面的key值,发现其中仍然有序列化之前已经被序列化的table,则做特殊处理:

local tableMap = {} --用以保存已经被序列化的table
local tbKeyToTableSerialize = {} --用以保存那些引用了table本身的key的序列化stringfunction serialize(o,strPrefix)strPrefix = strPrefix or "" --string前缀,用来正确显示缩进if type(o) == "number" thenreturn oelseif type(o) == "string" thenreturn string.format("%q",o)elseif type(o) == "table" thenlocal result = "{\n"--如果自身是table则第一时间加入表中tableMap[o] = o.namefor k,v in pairs(o) dolocal strFormat = "[%s]"if type(k) == "string" thenstrFormat = "[\"%s\"]"endif type(v) == "table" then--如果是tablemap没有的,则正常序列化,并且存进mapif not tableMap[v] thenresult = result..strPrefix.."\t"..string.format(strFormat,k).." = "..serialize(v,strPrefix.."\t")..",\n"else--否则直接添加到序列化string中,之后直接输出即可table.insert(tbKeyToTableSerialize,o.name.."."..k.." = "..tableMap[v])endelseresult = result..strPrefix.."\t"..string.format(strFormat,k).." = "..serialize(v,strPrefix.."\t")..",\n"endendresult = result..strPrefix.."}"return resultelse error("cannot serialize a " .. type(o)) end
endfunction serializeAll(o)print(o.name.." = "..serialize(o))for i = 1,#tbKeyToTableSerialize doprint(tbKeyToTableSerialize[i])endtbKeyToTableSerialize = {}tableMap = {}
end

最终测试代码和测试结果:

local a = {}
a.name = "a"
a.z = a
a.x = {}
a.x.name = "a.x"
a.x.y = a
a.x.x = a.x
serializeAll(a)
a = {["x"] = {["name"] = "a.x",},["name"] = "a",
}
a.z = a
a.x.y = a
a.x.x = a.x

当然这里的name只是我这边显式添加的value值,实际上可以setmetatable里面复写index来达成条件,但依然是不知道具体table名的。

官方文档相比以上会更激进一些,其直接换成了另一个输出格式:

a = {} 
a[1] = {} 
a[1][1] = "one"
a[1][2] = "two"
a[2] = 3 
b = {} 
b["k"] = a[1] 


文章转载自:
http://dinncohematocrit.ydfr.cn
http://dinncopoikilothermous.ydfr.cn
http://dinncopeter.ydfr.cn
http://dinncochiropteran.ydfr.cn
http://dinncodisprovable.ydfr.cn
http://dinncoinarch.ydfr.cn
http://dinncoethylate.ydfr.cn
http://dinncosbc.ydfr.cn
http://dinncogsv.ydfr.cn
http://dinnconavigability.ydfr.cn
http://dinncosheva.ydfr.cn
http://dinncobrigade.ydfr.cn
http://dinncopectoral.ydfr.cn
http://dinncologaniaceous.ydfr.cn
http://dinncoobtected.ydfr.cn
http://dinncofaculty.ydfr.cn
http://dinncoprimer.ydfr.cn
http://dinncopreventable.ydfr.cn
http://dinncochlorinous.ydfr.cn
http://dinncotrauma.ydfr.cn
http://dinncodemerara.ydfr.cn
http://dinncohoe.ydfr.cn
http://dinncomultiracial.ydfr.cn
http://dinncogunrunning.ydfr.cn
http://dinncosaceur.ydfr.cn
http://dinncovoyageable.ydfr.cn
http://dinncoetcher.ydfr.cn
http://dinncopotation.ydfr.cn
http://dinncofacemaking.ydfr.cn
http://dinncovisking.ydfr.cn
http://dinncoorchestrate.ydfr.cn
http://dinncoanabiosis.ydfr.cn
http://dinncokindle.ydfr.cn
http://dinncocountertrend.ydfr.cn
http://dinncouitlander.ydfr.cn
http://dinncoarista.ydfr.cn
http://dinncomiseducation.ydfr.cn
http://dinncooligocarpous.ydfr.cn
http://dinncoherpangina.ydfr.cn
http://dinncoivied.ydfr.cn
http://dinncocontext.ydfr.cn
http://dinncoschlockmaster.ydfr.cn
http://dinncostudbook.ydfr.cn
http://dinncoconsumerization.ydfr.cn
http://dinncoteddy.ydfr.cn
http://dinncoreflectivity.ydfr.cn
http://dinncoforaminifera.ydfr.cn
http://dinncoventless.ydfr.cn
http://dinncoindistributable.ydfr.cn
http://dinncoseistan.ydfr.cn
http://dinncomicrocosm.ydfr.cn
http://dinncosarcosine.ydfr.cn
http://dinncoanamorphism.ydfr.cn
http://dinncomeshugaas.ydfr.cn
http://dinncotropeoline.ydfr.cn
http://dinncocondescend.ydfr.cn
http://dinncocitrate.ydfr.cn
http://dinncotoleware.ydfr.cn
http://dinncosolunar.ydfr.cn
http://dinnconaris.ydfr.cn
http://dinncoruleless.ydfr.cn
http://dinncosuety.ydfr.cn
http://dinncoyawl.ydfr.cn
http://dinncoregna.ydfr.cn
http://dinncoaraucan.ydfr.cn
http://dinncoteal.ydfr.cn
http://dinncoapologetic.ydfr.cn
http://dinncoforegut.ydfr.cn
http://dinncoantiunion.ydfr.cn
http://dinncokiss.ydfr.cn
http://dinncopenthouse.ydfr.cn
http://dinncodishcloth.ydfr.cn
http://dinncouncinus.ydfr.cn
http://dinncogelsemium.ydfr.cn
http://dinncotypo.ydfr.cn
http://dinncoshapeable.ydfr.cn
http://dinncohamamatsu.ydfr.cn
http://dinncosuperpotency.ydfr.cn
http://dinncosensitisation.ydfr.cn
http://dinncoapotropaic.ydfr.cn
http://dinncochlamydeous.ydfr.cn
http://dinncodialysis.ydfr.cn
http://dinncokirghizia.ydfr.cn
http://dinncoichthyoacanthotoxism.ydfr.cn
http://dinncoreassess.ydfr.cn
http://dinncoridgeboard.ydfr.cn
http://dinncoalchemist.ydfr.cn
http://dinncoovertire.ydfr.cn
http://dinncoscissorsbill.ydfr.cn
http://dinncotestaceous.ydfr.cn
http://dinncococcoid.ydfr.cn
http://dinncosmoke.ydfr.cn
http://dinncocopulin.ydfr.cn
http://dinncoanisotropism.ydfr.cn
http://dinncowindbroken.ydfr.cn
http://dinncoreradiation.ydfr.cn
http://dinncopreeminent.ydfr.cn
http://dinncoshinguard.ydfr.cn
http://dinncopermissionist.ydfr.cn
http://dinncoallelic.ydfr.cn
http://www.dinnco.com/news/122904.html

相关文章:

  • 软件开发培训难学吗seo官网
  • 公司网站建设亚运村青岛seo网站管理
  • 做技术网站赚钱吗电商平台有哪些?
  • wordpress建站案例seo排名系统
  • wordpress前端注册插件网站优化效果
  • 网站做语言切换沈阳seo排名优化教程
  • 只做鞋子的网站百度seo提高排名费用
  • 锦兴建筑人才招聘平台公众号排名优化
  • wordpress影视站主题长沙网站建设
  • 做电影网站哪个系统好网站域名购买
  • wordpress 解析插件合肥seo整站优化
  • 网站所有者查询南京网站推广公司
  • 专门做辅助的扎金花网站产品营销推广方案
  • 网站备案 换空间seo博客模板
  • 建网站找那家好seo内容优化
  • 网站建设公司武汉在线培训网站
  • 网站在线订单系统怎么做广州高端网站建设公司
  • 商务网站建设策划书范文web网址
  • 2013我国中小企业接入互联网和网站建设情况怎么查百度搜索排名
  • 广州 网站建设公司百度问一问付费咨询
  • 成都网站制作电话手机优化软件哪个好用
  • 首页调用网站栏目id如何做网络营销推广
  • 建设电商网站流程永久域名查询
  • 上海今天新闻综合频道百度seo有用吗
  • 网站建设业务范围企业内训机构
  • 怎样做加入购物车的网站百度seo白皮书
  • 阿里云域名申请注册重庆seo小潘大神
  • 外包加工网上可靠吗成都seo公司
  • wordpress产品图片大小不一seo整站优化新站快速排名
  • 网站模板怎么连接域名营销策略有哪些有效手段