日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Serializing Lua objects into Lua Code

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Serializing Lua objects into Lua Code 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

The following little snippet allows you to ‘pickle’ Lua objects directly into Lua code (with the exception of functions, which are serialized as raw bytecode). Metatable support is on the way, but for now, it should be useful enough.

Example code:

view sourceprint? 01.dofile?"pickle.lua" 02.? 03.users?=?{ 04.user1?=?{pass?=?"abcdef",?start?=?10230}, 05.user2?=?{pass?=?"cdefba",?start?=?12032} 06.} 07.? 08.userdump?=?pickle.dumps(users) 09.print(userdump)

Outputs

view sourceprint? 01.return?{ 02.["user1"]?=?{ 03.["pass"]?=?"abcdef", 04.["start"]?=?10230, 05.}, 06.["user2"]?=?{ 07.["pass"]?=?"cdefba", 08.["start"]?=?12032, 09.}, 10.}

Pickle API:

  • pickle.dumps(object) -> String
    Pickles an object and returns it.
  • pickle.dump(object, filename) -> String
    Pickles an object and saves it as :filename, afterwards, returns the dump.
  • pickle.loads(object) -> Lua Object
    Loads a string dump of lua object and returns the object. The returned object will not injected into the global table.
  • pickle.load(filename) -> Lua Object
    Loads a pickled object from a file and returns it. The returned object will not be injected into the global table.

Full Source:

view sourceprint? 01.local?objects?=?{} 02.setmetatable(objects,?{__index={["subset"]=function(object,?proxies) 03.for?_,o in?ipairs(proxies)?do 04.if?object?==?o?then?return?true?end 05.end 06.end}}) 07.? 08.function?_pickle(object,?seen,?indent) 09.---if not seen then seen = {} end 10.if?not?indent?then?indent?=?""?end 11.? 12.local?serialize_key?=?function(key) 13.if?type(key)?==?"string"?then 14.return?"[\""..key.."\"]" 15.elseif?type(key)?==?"table"?then 16.return?"[".._pickle(key):gsub("\n"," ").."]" 17.else 18.return?"["..key.."]" 19.end 20.return?key 21.end 22.? 23.local?escape?=?function(o) 24.return?o:gsub("\\","\\\\"):gsub("'","\\'"):gsub('"','\\"') 25.end 26.? 27.---Switch Object type: 28.if?type(object)?==?"table"?then 29.local?serialize?=?"{\n" 30.for?key,?value in pairs(object)?do 31.serialize?=?serialize?..?indent.."\t"?..?serialize_key(key)?..?" = "?..tostring(_pickle(value,?seen,?indent.."\t"))?..?",\n" 32.end 33.serialize?=?serialize?..?indent?..?"}" 34.? 35.return?serialize 36.elseif?type(object)?==?"string"?then 37.return?'"'?..?escape(object)?..?'"' 38.elseif?type(object)?==?"function"?then 39.return?"loadstring([["..string.dump(object).."]])" 40.elseif?objects.subset(object,?{"userdata"})?then 41.return?nil 42.end 43.return?tostring(object) 44.end 45.? 46.pickle?=?{} 47.? 48.function?pickle.dumps(object) 49.return?"return "..?_pickle(object) 50.end 51.? 52.function?pickle.dump(object,?filename) 53.local?dump?=?pickle.dumps(object) 54.local?_file?=?io.open(filename,?"wb") 55._file:write(dump) 56._file:close() 57.return?dump 58.end 59.? 60.function?pickle.loads(object) 61.local?fn?=?loadstring(object) 62.if?fn?then 63.return?fn() 64.end 65.end 66.? 67.function?pickle.load(filename) 68.local?_file?=?io.open(filename,?"rb") 69.local?dump?=?_file:read("*all") 70.local?object?=?pickle.loads(dump) 71._file:close() 72.return?object 73.end

總結

以上是生活随笔為你收集整理的Serializing Lua objects into Lua Code的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。