Merge with trunk
authorMatthew Wild <mwild1@gmail.com>
Wed, 18 Jan 2012 15:08:05 +0000
changeset 4479 d1ddfc4709ce
parent 4478 3e33b2d2e48e (current diff)
parent 4474 b08a46cf06e6 (diff)
child 4480 187ce118aea6
Merge with trunk
--- a/util/json.lua	Wed Jan 18 15:07:40 2012 +0000
+++ b/util/json.lua	Wed Jan 18 15:08:05 2012 +0000
@@ -1,6 +1,6 @@
 
 local type = type;
-local t_insert, t_concat, t_remove = table.insert, table.concat, table.remove;
+local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort;
 local s_char = string.char;
 local tostring, tonumber = tostring, tonumber;
 local pairs, ipairs = pairs, ipairs;
@@ -79,11 +79,25 @@
 	if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
 		t_insert(buffer, "{");
 		local mark = #buffer;
-		for k,v in pairs(hash) do
-			stringsave(k, buffer);
-			t_insert(buffer, ":");
-			simplesave(v, buffer);
-			t_insert(buffer, ",");
+		if buffer.ordered then
+			local keys = {};
+			for k in pairs(hash) do
+				t_insert(keys, k);
+			end
+			t_sort(keys);
+			for _,k in ipairs(keys) do
+				stringsave(k, buffer);
+				t_insert(buffer, ":");
+				simplesave(hash[k], buffer);
+				t_insert(buffer, ",");
+			end
+		else
+			for k,v in pairs(hash) do
+				stringsave(k, buffer);
+				t_insert(buffer, ":");
+				simplesave(v, buffer);
+				t_insert(buffer, ",");
+			end
 		end
 		if next(__hash) ~= nil then
 			t_insert(buffer, "\"__hash\":[");
@@ -129,6 +143,11 @@
 	simplesave(obj, t);
 	return t_concat(t);
 end
+function json.encode_ordered(obj)
+	local t = { ordered = true };
+	simplesave(obj, t);
+	return t_concat(t);
+end
 
 -----------------------------------