PEDN> Main Web>NPL>SerializationLib (2008-02-29, LiXizhi) Change language en zh-cn? EditAttach

SerializationLib

serialization functions in commonlib

Title serialization functions in commonlib
Author(s) LiXizhi
Date 2006/11/25
File script/ide/serialization.lua

Description

serialization functions in commonlib

TIP Sample Code

NPL.load("(gl)script/ide/commonlib.lua");
-- include commonlib to use this lib
NPL.load("(gl)script/ide/serialization.lua");

Member Functions

commonlib.echo

output input to log safely in a single echo line. Only used in debugging or testing Internally it uses commonlib.dump which handles recursive tables.

  • param p1 : anything to echo, table, nil, value, function, etc.
  • param fastmode : if true, table recursion is igored. it may cause stack overflow if set true with recursive table

syntax

function commonlib.echo(p1, fastmode)

parameters

p1 anything to echo, table, nil, value, function, etc.
fastmode  

commonlib.serializeToFile

[[ serialize a table to the current file: function and user data are exported as nil value.

  • param o : table to serialize
]]

syntax

function commonlib.serializeToFile(file, o)

parameters

file  
| o | table to serialize ]] |

commonlib.serialize

serialize to string e.g. print(commonlib.serialize(o))

syntax

function commonlib.serialize(o)

parameters

o  

commonlib.serialize_compact

same as commonlib.serialize, except that it is more compact by removing all \r\n and comments, etc.

syntax

function commonlib.serialize_compact(o)

parameters

o  

commonlib.LoadTableFromFile

this function will return a table created from file. function may return nil e.g. local t = commonlib.LoadTableFromFile("temp/t.txt") if(t~=nil) then end

syntax

function commonlib.LoadTableFromFile(filename)

parameters

filename  

commonlib.LoadTableFromString

body should be a string of "{ any thing here }" return the table.

syntax

function commonlib.LoadTableFromString(body)

parameters

body  

commonlib.SaveTableToFile

this function will return a table created from file. function may return nil.e.g. local t = {test=1}; commonlib.SaveTableToFile(t, "temp/t.txt");

syntax

function commonlib.SaveTableToFile(o, filename)

parameters

o  
filename  

commonlib.serialize2

serialize to string work with SaveTableToFile2? serialization will be well organized and easy to read the table structure e.g. print(commonlib.serialize(o, 1))

syntax

function commonlib.serialize2(o, lvl)

parameters

o  
lvl  

commonlib.SaveTableToFile2

this function will record a table to a specific file. different to SaveTableToFile? is that this file will be well organized and easy to read the table structure local t = {test=1}; commonlib.SaveTableToFile(t, "temp/t.txt");

syntax

function commonlib.SaveTableToFile2(o, filename)

parameters

o  
filename  

commonlib.dump


DataDumper? .lua code is from: http://lua-users.org/wiki/DataDumper added by LXZ on 2008.5.1

[[ Copyright (c) 2007 Olivetti-Engineering SA

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]

local dumplua_closure = [[ local closures = {} local function closure(t) closures[#closures+1] = t t[1] = assert(loadstring(t[1])) return t[1] end

for _,t in pairs(closures) do for i = 2,#t do debug.setupvalue(t[1], i-1, t[i]) end end ]]

local lua_reserved_keywords = { 'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while' }

local function keys(t) local res = {} local oktypes = { stringstring = true, numbernumber = true } local function cmpfct(a,b) if oktypes[type(a)..type(b)] then return a < b else return type(a) < type(b) end end for k in pairs(t) do res[#res+1] = k end table.sort(res, cmpfct) return res end

local c_functions = {} for _,lib in pairs{'_G', 'string', 'table', 'math', 'io', 'os', 'coroutine', 'package', 'debug'} do local t = _G[lib] or {} lib = lib .. "." if lib == "_G." then lib = "" end for k,v in pairs(t) do if type(v) == 'function' and not pcall(string.dump, v) then c_functions[v] = lib..k end end end

[[ LiXizhi 2008.5.15: I modified to disable function and userdata dumping. DataDumper? consists of a single Lua function, which could easily be put in a separate module or integrated into a bigger one. The function has four parameters, but only the first one is mandatory. It always returns a string value, which is valid Lua code. Simply executing this chunk will import back to a variable the complete structure of the original variable. For simple structures, there is only one Lua instruction like a table constructor, but some more complex features will output a script with more instructions.

All the following language features are supported:

Simple Lua types: nil, boolean, number, string Tables are dumped recursively Table metatables are also dumped recursively Simple Lua functions (no upvalue) are dumped with loadstring Lua closures with upvalues are also supported, using the debug library! Known C functions are output using their original name Complex tables structures with internal references are supported

  • param value : can be of any supported type
  • param varname : optional variable name. Depending on its form, the output will look like:
nil
"return value"
identifier
"varname = value"
other
"varname".."value"
  • param fastmode : is a boolean value:
true
optimizes for speed. Metatables, closures, C functions and references are not supported. Returns a code chunk without any space or new line!
false
supports all advanced features and favors readable code with good indentation.
  • param indent : the number of additional indentation level. Default is 0.
]]

syntax

function commonlib.dump(value, varname, fastmode, ident)

parameters

value can be of any supported type
varname  
| fastmode | is a boolean value:
true
optimizes for speed. Metatables, closures, C functions and references are not supported. Returns a code chunk without any space or new line!
false
supports all advanced features and favors readable code with good indentation. |
ident  
Topic revision: r1 - 2008-02-29 - 15:26:12 - LiXizhi
 

ParaEngine Developer Network

This site is powered by the TWiki collaboration platformCopyright © 2004-2009 ParaEngine Corporation
Ideas, requests, problems regarding ParaEngine platform Send feedback