util.json: Make encode(decode("[]"))=="[]".
authorWaqas Hussain <waqas20@gmail.com>
Tue, 23 Apr 2013 18:41:52 +0000 (14:41 -0400)
committerWaqas Hussain <waqas20@gmail.com>
Tue, 23 Apr 2013 18:41:52 +0000 (14:41 -0400)
util/json.lua

index ff7351a72f991dcb56ade78eb80c7d5e1153b8b3..e8de4d2d49952afa2a846efec356a09bfae90b8a 100644 (file)
@@ -18,6 +18,9 @@ local error = error;
 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 @@ function simplesave(o, buffer)
        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 @@ function json.decode(json)
        
        local readvalue;
        local function readarray()
-               local t = {};
+               local t = setmetatable({}, array_mt);
                next(); -- skip '['
                skipstuff();
                if ch == "]" then next(); return t; end