Notes on the serialization of LUA programming objects

Source: Internet
Author: User
Tags object serialization

AboutLUAProgram DesignObject serializationLearning notes are the content to be introduced in this article.LUAMediumObjectOfSerializationFor details, refer to this article.

We usually need to serialize some data, that is, to convert the data into a byte stream or a bytes stream, and then we can easily persist it. There are many formats available for text serialization: SOAP, Json, and even custom xml. However, lua script is also a good choice.

For more information, see 《LuaProgramming version 2nd) "Simplified Chinese version detailed LUA script language data files and persistence based on the Implementation below. Note that this implementation can only target non-circular tables and Cannot serialize or deserialize functions, threads, and userdata. In order to prevent the serialized data from being too large, there is no detailed typographical control.

 
 
  1. function serialize(obj)  
  2.     local lua = "" 
  3.     local t = type(obj)  
  4.     if t == "number" then  
  5.         lualua = lua .. obj  
  6.     elseif t == "boolean" then  
  7.         lualua = lua .. tostring(obj)  
  8.     elseif t == "string" then  
  9.         lualua = lua .. string.format("%q", obj)  
  10.     elseif t == "table" then  
  11.         lualua = lua .. "{\n"  
  12.         for k, v in pairs(obj) do  
  13.             lualua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"  
  14.         end  
  15.         local metatable = getmetatable(obj)  
  16.         if metatable ~= nil and type(metatable.__index) == "table" then  
  17.             for k, v in pairs(metatable.__index) do  
  18.                 lualua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"  
  19.             end  
  20.         end  
  21.         lualua = lua .. "}"  
  22.     elseif t == "nil" then  
  23.         return nil  
  24.     else  
  25.         error("can not serialize a " .. t .. " type.")  
  26.     end  
  27.     return lua  
  28. end  
  29.  
  30. function unserialize(lua)  
  31.     local t = type(lua)  
  32.     if t == "nil" or lua == "" then  
  33.         return nil  
  34.     elseif t == "number" or t == "string" or t == "boolean" then  
  35.         lua = tostring(lua)  
  36.     else  
  37.         error("can not unserialize a " .. t .. " type.")  
  38.     end  
  39.     lua = "return " .. lua  
  40.     local func = loadstring(lua)  
  41.     if func == nil then return nil end  
  42.     return func()  
  43. end  
  44.  
  45. data = {["a"] = "a", ["b"] = "b", [1] = 1, [2] = 2, ["t"] = {1, 2, 3}}  
  46. print(serialize(unserialize(1)))  
  47. print(serialize(unserialize(serialize(data)))) 

Summary: AboutLUAProgram DesignObject serializationThe content of the study notes has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.