util/serialization.lua
author Kim Alvefur <zash@zash.se>
Sat, 23 Mar 2024 20:48:19 +0100
changeset 13465 c673ff1075bd
parent 13135 03f1509a6105
permissions -rw-r--r--
mod_posix: Move everything to util.startup This allows greater control over the order of events. Notably, the internal ordering between daemonization, initialization of libunbound and setup of signal handling is sensitive. libunbound starts a separate thread for processing DNS requests. If this thread is started before signal handling has been set up, it will not inherit the signal handlers and instead behave as it would have before signal handlers were set up, i.e. cause the whole process to immediately exit. libunbound is usually initialized on the first DNS request, usually triggered by an outgoing s2s connection attempt. If daemonization happens before signals have been set up, signals may not be processed at all.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1135
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2222
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2222
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
     4
-- Copyright (C) 2018 Kim Alvefur
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5021
diff changeset
     5
--
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     7
-- COPYING file in the source package for more information.
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     8
--
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     9
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    10
local getmetatable = getmetatable;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    11
local next, type = next, type;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    12
local s_format = string.format;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    13
local s_gsub = string.gsub;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    14
local s_rep = string.rep;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    15
local s_char = string.char;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    16
local s_match = string.match;
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
local t_concat = table.concat;
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12786
diff changeset
    19
local to_hex = require "prosody.util.hex".to;
9869
37278b420c74 util.serialization: Use util.hex
Kim Alvefur <zash@zash.se>
parents: 9868
diff changeset
    20
3736
73399dd525e8 util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
    21
local pcall = pcall;
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12786
diff changeset
    22
local envload = require"prosody.util.envload".envload;
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 3745
diff changeset
    23
12786
8815d3090928 util.mathcompat: Module to ease reuse of math.type()
Kim Alvefur <zash@zash.se>
parents: 12785
diff changeset
    24
if not math.type then
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12786
diff changeset
    25
	require "prosody.util.mathcompat"
12786
8815d3090928 util.mathcompat: Module to ease reuse of math.type()
Kim Alvefur <zash@zash.se>
parents: 12785
diff changeset
    26
end
8815d3090928 util.mathcompat: Module to ease reuse of math.type()
Kim Alvefur <zash@zash.se>
parents: 12785
diff changeset
    27
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    28
local pos_inf, neg_inf = math.huge, -math.huge;
12785
22066b02887f util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 11097
diff changeset
    29
local m_type = math.type;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    30
9868
9d80e679c371 util.serialization: Allow overriding table iterator
Kim Alvefur <zash@zash.se>
parents: 9867
diff changeset
    31
local function rawpairs(t)
9d80e679c371 util.serialization: Allow overriding table iterator
Kim Alvefur <zash@zash.se>
parents: 9867
diff changeset
    32
	return next, t, nil;
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    33
end
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    34
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    35
local function fatal_error(obj, why)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    36
	error("Can't serialize "..type(obj) .. (why and ": ".. why or ""));
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    37
end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    38
9572
0bc399953c27 util.serialization: Rename non-fatal fallback handler for clarity
Kim Alvefur <zash@zash.se>
parents: 9571
diff changeset
    39
local function nonfatal_fallback(x, why)
9573
ce403b6470f8 util.serialization: Encode non-fatal error in way that can be restored
Kim Alvefur <zash@zash.se>
parents: 9572
diff changeset
    40
	return s_format("{__type=%q,__error=%q}", type(x), why or "fail");
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    41
end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    42
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    43
local string_escapes = {
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    44
	['\a'] = [[\a]]; ['\b'] = [[\b]];
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    45
	['\f'] = [[\f]]; ['\n'] = [[\n]];
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    46
	['\r'] = [[\r]]; ['\t'] = [[\t]];
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    47
	['\v'] = [[\v]]; ['\\'] = [[\\]];
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    48
	['\"'] = [[\"]]; ['\''] = [[\']];
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    49
}
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    50
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    51
for i = 0, 255 do
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    52
	local c = s_char(i);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    53
	if not string_escapes[c] then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    54
		string_escapes[c] = s_format("\\%03d", i);
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    55
	end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    56
end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    57
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    58
local default_keywords = {
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    59
	["do"] = true; ["and"] = true; ["else"] = true; ["break"] = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    60
	["if"] = true; ["end"] = true; ["goto"] = true; ["false"] = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    61
	["in"] = true; ["for"] = true; ["then"] = true; ["local"] = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    62
	["or"] = true; ["nil"] = true; ["true"] = true; ["until"] = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    63
	["elseif"] = true; ["function"] = true; ["not"] = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    64
	["repeat"] = true; ["return"] = true; ["while"] = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    65
};
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    66
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    67
local function new(opt)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    68
	if type(opt) ~= "table" then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    69
		opt = { preset = opt };
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    70
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    71
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    72
	local types = {
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    73
		table = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    74
		string = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    75
		number = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    76
		boolean = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    77
		["nil"] = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    78
	};
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    79
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    80
	-- presets
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    81
	if opt.preset == "debug" then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    82
		opt.preset = "oneline";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    83
		opt.freeze = true;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    84
		opt.fatal = false;
9572
0bc399953c27 util.serialization: Rename non-fatal fallback handler for clarity
Kim Alvefur <zash@zash.se>
parents: 9571
diff changeset
    85
		opt.fallback = nonfatal_fallback;
9571
69f589c888e7 util.serialization: Disable use of unquoted table keys by default
Kim Alvefur <zash@zash.se>
parents: 9570
diff changeset
    86
		opt.unquoted = true;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    87
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    88
	if opt.preset == "oneline" then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    89
		opt.indentwith = opt.indentwith or "";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    90
		opt.itemstart = opt.itemstart or " ";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    91
		opt.itemlast = opt.itemlast or "";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    92
		opt.tend = opt.tend or " }";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    93
	elseif opt.preset == "compact" then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    94
		opt.indentwith = opt.indentwith or "";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    95
		opt.itemstart = opt.itemstart or "";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    96
		opt.itemlast = opt.itemlast or "";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
    97
		opt.equals = opt.equals or "=";
9571
69f589c888e7 util.serialization: Disable use of unquoted table keys by default
Kim Alvefur <zash@zash.se>
parents: 9570
diff changeset
    98
		opt.unquoted = true;
13135
03f1509a6105 util.serialization: Add a "pretty" preset
Kim Alvefur <zash@zash.se>
parents: 12979
diff changeset
    99
	elseif opt.preset == "pretty" then
03f1509a6105 util.serialization: Add a "pretty" preset
Kim Alvefur <zash@zash.se>
parents: 12979
diff changeset
   100
		opt.fatal = false;
03f1509a6105 util.serialization: Add a "pretty" preset
Kim Alvefur <zash@zash.se>
parents: 12979
diff changeset
   101
		opt.freeze = true;
03f1509a6105 util.serialization: Add a "pretty" preset
Kim Alvefur <zash@zash.se>
parents: 12979
diff changeset
   102
		opt.unquoted = true;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   103
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   104
9572
0bc399953c27 util.serialization: Rename non-fatal fallback handler for clarity
Kim Alvefur <zash@zash.se>
parents: 9571
diff changeset
   105
	local fallback = opt.fallback or opt.fatal == false and nonfatal_fallback or fatal_error;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   106
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   107
	local function ser(v)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   108
		return (types[type(v)] or fallback)(v);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   109
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   110
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   111
	local keywords = opt.keywords or default_keywords;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   112
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   113
	-- indented
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   114
	local indentwith = opt.indentwith or "\t";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   115
	local itemstart = opt.itemstart or "\n";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   116
	local itemsep = opt.itemsep or ";";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   117
	local itemlast = opt.itemlast or ";\n";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   118
	local tstart = opt.tstart or "{";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   119
	local tend = opt.tend or "}";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   120
	local kstart = opt.kstart or "[";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   121
	local kend = opt.kend or "]";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   122
	local equals = opt.equals or " = ";
9571
69f589c888e7 util.serialization: Disable use of unquoted table keys by default
Kim Alvefur <zash@zash.se>
parents: 9570
diff changeset
   123
	local unquoted = opt.unquoted == true and "^[%a_][%w_]*$" or opt.unquoted;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   124
	local hex = opt.hex;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   125
	local freeze = opt.freeze;
9567
ed0090f8b709 util.serialization: Make maximum table depth configurable
Kim Alvefur <zash@zash.se>
parents: 9489
diff changeset
   126
	local maxdepth = opt.maxdepth or 127;
9570
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   127
	local multirefs = opt.multiref;
9868
9d80e679c371 util.serialization: Allow overriding table iterator
Kim Alvefur <zash@zash.se>
parents: 9867
diff changeset
   128
	local table_pairs = opt.table_iterator or rawpairs;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   129
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   130
	-- serialize one table, recursively
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   131
	-- t - table being serialized
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   132
	-- o - array where tokens are added, concatenate to get final result
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   133
	--   - also used to detect cycles
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   134
	-- l - position in o of where to insert next token
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   135
	-- d - depth, used for indentation
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   136
	local function serialize_table(t, o, l, d)
9568
9a1e2f5f674f util.serialization: Separate errors for multiple table references and max depth
Kim Alvefur <zash@zash.se>
parents: 9567
diff changeset
   137
		if o[t] then
9a1e2f5f674f util.serialization: Separate errors for multiple table references and max depth
Kim Alvefur <zash@zash.se>
parents: 9567
diff changeset
   138
			o[l], l = fallback(t, "table has multiple references"), l + 1;
9a1e2f5f674f util.serialization: Separate errors for multiple table references and max depth
Kim Alvefur <zash@zash.se>
parents: 9567
diff changeset
   139
			return l;
9a1e2f5f674f util.serialization: Separate errors for multiple table references and max depth
Kim Alvefur <zash@zash.se>
parents: 9567
diff changeset
   140
		elseif d > maxdepth then
9a1e2f5f674f util.serialization: Separate errors for multiple table references and max depth
Kim Alvefur <zash@zash.se>
parents: 9567
diff changeset
   141
			o[l], l = fallback(t, "max table depth reached"), l + 1;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   142
			return l;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   143
		end
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   144
9570
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   145
		-- Keep track of table loops
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   146
		local ot = t; -- reference pre-freeze
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   147
		o[t] = true;
9570
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   148
		o[ot] = true;
9488
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   149
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   150
		if freeze == true then
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   151
			-- opportunity to do pre-serialization
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   152
			local mt = getmetatable(t);
9488
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   153
			if type(mt) == "table" then
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   154
				local tag = mt.__name;
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   155
				local fr = mt.__freeze;
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   156
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   157
				if type(fr) == "function" then
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   158
					t = fr(t);
11097
ec14d4fce855 util.serialization: Let freeze metamethod return a literal string
Kim Alvefur <zash@zash.se>
parents: 10065
diff changeset
   159
					if type(t) == "string" then
ec14d4fce855 util.serialization: Let freeze metamethod return a literal string
Kim Alvefur <zash@zash.se>
parents: 10065
diff changeset
   160
						o[l], l = t, l + 1;
ec14d4fce855 util.serialization: Let freeze metamethod return a literal string
Kim Alvefur <zash@zash.se>
parents: 10065
diff changeset
   161
						return l;
ec14d4fce855 util.serialization: Let freeze metamethod return a literal string
Kim Alvefur <zash@zash.se>
parents: 10065
diff changeset
   162
					end
9488
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   163
					if type(tag) == "string" then
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   164
						o[l], l = tag, l + 1;
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   165
					end
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   166
				end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   167
			end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   168
		end
9488
c667887d78ad util.serialization: Simpler metatable pre-processing
Kim Alvefur <zash@zash.se>
parents: 9487
diff changeset
   169
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   170
		o[l], l = tstart, l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   171
		local indent = s_rep(indentwith, d);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   172
		local numkey = 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   173
		local ktyp, vtyp;
9867
aebfd1601ae2 util.serialization: Optimize handling of last table separator
Kim Alvefur <zash@zash.se>
parents: 9696
diff changeset
   174
		local had_items = false;
9868
9d80e679c371 util.serialization: Allow overriding table iterator
Kim Alvefur <zash@zash.se>
parents: 9867
diff changeset
   175
		for k,v in table_pairs(t) do
9867
aebfd1601ae2 util.serialization: Optimize handling of last table separator
Kim Alvefur <zash@zash.se>
parents: 9696
diff changeset
   176
			had_items = true;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   177
			o[l], l = itemstart, l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   178
			o[l], l = indent, l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   179
			ktyp, vtyp = type(k), type(v);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   180
			if k == numkey then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   181
				-- next index in array part
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   182
				-- assuming that these are found in order
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   183
				numkey = numkey + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   184
			elseif unquoted and ktyp == "string" and
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   185
				not keywords[k] and s_match(k, unquoted) then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   186
				-- unquoted keys
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   187
				o[l], l = k, l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   188
				o[l], l = equals, l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   189
			else
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   190
				-- quoted keys
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   191
				o[l], l = kstart, l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   192
				if ktyp == "table" then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   193
					l = serialize_table(k, o, l, d+1);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   194
				else
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   195
					o[l], l = ser(k), l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   196
				end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   197
				-- =
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   198
				o[l], o[l+1], l = kend, equals, l + 2;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   199
			end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   200
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   201
			-- the value
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   202
			if vtyp == "table" then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   203
				l = serialize_table(v, o, l, d+1);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   204
			else
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   205
				o[l], l = ser(v), l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   206
			end
9867
aebfd1601ae2 util.serialization: Optimize handling of last table separator
Kim Alvefur <zash@zash.se>
parents: 9696
diff changeset
   207
			o[l], l = itemsep, l + 1;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   208
		end
9867
aebfd1601ae2 util.serialization: Optimize handling of last table separator
Kim Alvefur <zash@zash.se>
parents: 9696
diff changeset
   209
		if had_items then
aebfd1601ae2 util.serialization: Optimize handling of last table separator
Kim Alvefur <zash@zash.se>
parents: 9696
diff changeset
   210
			o[l - 1] = itemlast;
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   211
			o[l], l = s_rep(indentwith, d-1), l + 1;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   212
		end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   213
		o[l], l = tend, l +1;
9570
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   214
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   215
		if multirefs then
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   216
			o[t] = nil;
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   217
			o[ot] = nil;
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   218
		end
dbfa286cfa88 util.serialization: Add option for allowing multiple references to the same table (but not cycles)
Kim Alvefur <zash@zash.se>
parents: 9568
diff changeset
   219
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   220
		return l;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   221
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   222
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   223
	function types.table(t)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   224
		local o = {};
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   225
		serialize_table(t, o, 1, 1);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   226
		return t_concat(o);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   227
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   228
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   229
	local function serialize_string(s)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   230
		return '"' .. s_gsub(s, "[%z\1-\31\"\'\\\127-\255]", string_escapes) .. '"';
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   231
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   232
9486
903e0cfd4cc9 util.serialization: Make check of prefix for optional hex encoding stricter
Kim Alvefur <zash@zash.se>
parents: 9483
diff changeset
   233
	if type(hex) == "string" then
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   234
		function types.string(s)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   235
			local esc = serialize_string(s);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   236
			if #esc > (#s*2+2+#hex) then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   237
				return hex .. '"' .. to_hex(s) .. '"';
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   238
			end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   239
			return esc;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   240
		end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   241
	else
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   242
		types.string = serialize_string;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   243
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   244
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   245
	function types.number(t)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   246
		if m_type(t) == "integer" then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   247
			return s_format("%d", t);
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   248
		elseif t == pos_inf then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   249
			return "(1/0)";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   250
		elseif t == neg_inf then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   251
			return "(-1/0)";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   252
		elseif t ~= t then
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   253
			return "(0/0)";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   254
		end
9489
20aad0108999 util.serialization: Remove encoding of very large or very small numbers in scientific notation
Kim Alvefur <zash@zash.se>
parents: 9488
diff changeset
   255
		return s_format("%.18g", t);
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   256
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   257
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   258
	-- Are these faster than tostring?
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   259
	types["nil"] = function()
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   260
		return "nil";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   261
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   262
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   263
	function types.boolean(t)
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   264
		return t and "true" or "false";
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   265
	end
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   266
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   267
	return ser;
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   268
end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   269
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6673
diff changeset
   270
local function deserialize(str)
3736
73399dd525e8 util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   271
	if type(str) ~= "string" then return nil; end
73399dd525e8 util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   272
	str = "return "..str;
9487
1d1541630c20 util.serialization: Use '=' prefix for chunk source description
Kim Alvefur <zash@zash.se>
parents: 9486
diff changeset
   273
	local f, err = envload(str, "=serialized data", {});
3736
73399dd525e8 util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   274
	if not f then return nil, err; end
73399dd525e8 util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   275
	local success, ret = pcall(f);
73399dd525e8 util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   276
	if not success then return nil, ret; end
73399dd525e8 util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents: 2923
diff changeset
   277
	return ret;
546
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   278
end
1e65f64dfabf Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   279
10064
7a36b7ac309b util.serialization: Cache default serialization instance (fixes #1389)
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
   280
local default = new();
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6673
diff changeset
   281
return {
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   282
	new = new;
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   283
	serialize = function (x, opt)
10064
7a36b7ac309b util.serialization: Cache default serialization instance (fixes #1389)
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
   284
		if opt == nil then
7a36b7ac309b util.serialization: Cache default serialization instance (fixes #1389)
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
   285
			return default(x);
7a36b7ac309b util.serialization: Cache default serialization instance (fixes #1389)
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
   286
		else
7a36b7ac309b util.serialization: Cache default serialization instance (fixes #1389)
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
   287
			return new(opt)(x);
7a36b7ac309b util.serialization: Cache default serialization instance (fixes #1389)
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
   288
		end
9011
ae3c52419ec1 util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
   289
	end;
6780
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6673
diff changeset
   290
	deserialize = deserialize;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 6673
diff changeset
   291
};