Changeset

5516:9733836629f9

util.json: Make encode(decode("[]"))=="[]".
author Waqas Hussain <waqas20@gmail.com>
date Tue, 23 Apr 2013 14:41:52 -0400 (2013-04-23)
parents 5514:1091d7c3b4d2
children 5517:9d7349bbe4d2
files util/json.lua
diffstat 1 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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