Software /
code /
prosody
Comparison
util/serialization.lua @ 6791:e813e8cf6046
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 20 Aug 2015 13:05:22 +0200 |
parent | 6777:5de6b93d0190 |
child | 8555:4f0f5b49bb03 |
comparison
equal
deleted
inserted
replaced
6776:4412a2307c89 | 6791:e813e8cf6046 |
---|---|
18 | 18 |
19 local debug_traceback = debug.traceback; | 19 local debug_traceback = debug.traceback; |
20 local log = require "util.logger".init("serialization"); | 20 local log = require "util.logger".init("serialization"); |
21 local envload = require"util.envload".envload; | 21 local envload = require"util.envload".envload; |
22 | 22 |
23 module "serialization" | 23 local _ENV = nil; |
24 | 24 |
25 local indent = function(i) | 25 local indent = function(i) |
26 return string_rep("\t", i); | 26 return string_rep("\t", i); |
27 end | 27 end |
28 local function basicSerialize (o) | 28 local function basicSerialize (o) |
69 log("error", "cannot serialize a %s: %s", type(o), debug_traceback()) | 69 log("error", "cannot serialize a %s: %s", type(o), debug_traceback()) |
70 func(t, "nil"); | 70 func(t, "nil"); |
71 end | 71 end |
72 end | 72 end |
73 | 73 |
74 function append(t, o) | 74 local function append(t, o) |
75 _simplesave(o, 1, t, t.write or t_insert); | 75 _simplesave(o, 1, t, t.write or t_insert); |
76 return t; | 76 return t; |
77 end | 77 end |
78 | 78 |
79 function serialize(o) | 79 local function serialize(o) |
80 return t_concat(append({}, o)); | 80 return t_concat(append({}, o)); |
81 end | 81 end |
82 | 82 |
83 function deserialize(str) | 83 local function deserialize(str) |
84 if type(str) ~= "string" then return nil; end | 84 if type(str) ~= "string" then return nil; end |
85 str = "return "..str; | 85 str = "return "..str; |
86 local f, err = envload(str, "@data", {}); | 86 local f, err = envload(str, "@data", {}); |
87 if not f then return nil, err; end | 87 if not f then return nil, err; end |
88 local success, ret = pcall(f); | 88 local success, ret = pcall(f); |
89 if not success then return nil, ret; end | 89 if not success then return nil, ret; end |
90 return ret; | 90 return ret; |
91 end | 91 end |
92 | 92 |
93 return _M; | 93 return { |
94 append = append; | |
95 serialize = serialize; | |
96 deserialize = deserialize; | |
97 }; |