util.json: Make encode(decode("[]"))=="[]".
authorWaqas Hussain <waqas20@gmail.com>
Tue, 23 Apr 2013 14:41:52 -0400
changeset 5516 9733836629f9
parent 5514 1091d7c3b4d2
child 5517 9d7349bbe4d2
util.json: Make encode(decode("[]"))=="[]".
util/json.lua
--- a/util/json.lua	Tue Apr 23 14:49:48 2013 +0200
+++ b/util/json.lua	Tue Apr 23 14:41:52 2013 -0400
@@ -18,6 +18,9 @@
 local newproxy, getmetatable = newproxy, getmetatable;
 local print = print;
 
+local has_array, array = pcall(require, "util.array");
+local array_mt = hasarray and getmetatable(array()) or {};
+
 --module("json")
 local json = {};
 
@@ -165,7 +168,12 @@
 	elseif t == "string" then
 		stringsave(o, buffer);
 	elseif t == "table" then
-		tablesave(o, buffer);
+		local mt = getmetatable(o);
+		if mt == array_mt then
+			arraysave(o, buffer);
+		else
+			tablesave(o, buffer);
+		end
 	elseif t == "boolean" then
 		t_insert(buffer, (o and "true" or "false"));
 	else
@@ -237,7 +245,7 @@
 	
 	local readvalue;
 	local function readarray()
-		local t = {};
+		local t = setmetatable({}, array_mt);
 		next(); -- skip '['
 		skipstuff();
 		if ch == "]" then next(); return t; end