# HG changeset patch # User Waqas Hussain # Date 1366742512 14400 # Node ID 9733836629f9d8eafa7b0ff86984fc589521ec49 # Parent 1091d7c3b4d2c13cffc426f145240485fc7bbd70 util.json: Make encode(decode("[]"))=="[]". diff -r 1091d7c3b4d2 -r 9733836629f9 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