Comparison

util/serialization.lua @ 3736:73399dd525e8

util.serialization: Implemented deserialize().
author Waqas Hussain <waqas20@gmail.com>
date Sun, 12 Dec 2010 06:29:19 +0500
parent 2923:b7049746bd29
child 3745:87f6eabd90c9
comparison
equal deleted inserted replaced
3735:40b54c46a14c 3736:73399dd525e8
12 local t_insert = table.insert; 12 local t_insert = table.insert;
13 local t_concat = table.concat; 13 local t_concat = table.concat;
14 local error = error; 14 local error = error;
15 local pairs = pairs; 15 local pairs = pairs;
16 local next = next; 16 local next = next;
17
18 local loadstring = loadstring;
19 local setfenv = setfenv;
20 local pcall = pcall;
17 21
18 local debug_traceback = debug.traceback; 22 local debug_traceback = debug.traceback;
19 local log = require "util.logger".init("serialization"); 23 local log = require "util.logger".init("serialization");
20 module "serialization" 24 module "serialization"
21 25
70 function serialize(o) 74 function serialize(o)
71 return t_concat(append({}, o)); 75 return t_concat(append({}, o));
72 end 76 end
73 77
74 function deserialize(str) 78 function deserialize(str)
75 error("Not implemented"); 79 if type(str) ~= "string" then return nil; end
80 str = "return "..str;
81 local f, err = loadstring(str, "@data");
82 if not f then return nil, err; end
83 setfenv(f, {});
84 local success, ret = pcall(f);
85 if not success then return nil, ret; end
86 return ret;
76 end 87 end
77 88
78 return _M; 89 return _M;