[vlc-commits] generate properly escaped json

Rob Jonson git at videolan.org
Wed Oct 5 18:51:31 CEST 2011


vlc | branch: master | Rob Jonson <rob at hobbyistsoftware.com> | Wed Sep 28 13:05:15 2011 +0100| [35cb1ed0089d0f85464ee8f9c2d17352971bad3d] | committer: Jean-Baptiste Kempf

generate properly escaped json

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=35cb1ed0089d0f85464ee8f9c2d17352971bad3d
---

 share/lua/http/requests/browse.json     |    5 +--
 share/lua/http/requests/playlist.json   |    4 +--
 share/lua/http/requests/status.json     |    4 +--
 share/lua/intf/modules/httprequests.lua |   43 +++----------------------------
 4 files changed, 8 insertions(+), 48 deletions(-)

diff --git a/share/lua/http/requests/browse.json b/share/lua/http/requests/browse.json
index da9fc9a..39fb9b5 100644
--- a/share/lua/http/requests/browse.json
+++ b/share/lua/http/requests/browse.json
@@ -33,8 +33,7 @@ httprequests.processcommands()
 
 local browseTable=httprequests.getbrowsetable()
 
-print('{')
-httprequests.printTableAsJson(browseTable,0)
-print('}')
+httprequests.printTableAsJson(browseTable)
+
 
 ?>
diff --git a/share/lua/http/requests/playlist.json b/share/lua/http/requests/playlist.json
index f99866e..7f531f3 100644
--- a/share/lua/http/requests/playlist.json
+++ b/share/lua/http/requests/playlist.json
@@ -31,8 +31,6 @@ require "httprequests"
 
 httprequests.processcommands()
 
-print('{')
-httprequests.printTableAsJson(httprequests.playlisttable(),0)
-print('}')
+httprequests.printTableAsJson(httprequests.playlisttable())
 
 ?>
diff --git a/share/lua/http/requests/status.json b/share/lua/http/requests/status.json
index f98ed22..d446158 100644
--- a/share/lua/http/requests/status.json
+++ b/share/lua/http/requests/status.json
@@ -33,8 +33,6 @@ httprequests.processcommands()
 
 local statusTable=httprequests.getstatus(true)
 
-print('{')
-httprequests.printTableAsJson(statusTable,0)
-print('}')
+httprequests.printTableAsJson(statusTable)
 
 ?>
diff --git a/share/lua/intf/modules/httprequests.lua b/share/lua/intf/modules/httprequests.lua
index cd86113..9e549c4 100644
--- a/share/lua/intf/modules/httprequests.lua
+++ b/share/lua/intf/modules/httprequests.lua
@@ -24,6 +24,7 @@
 
 module("httprequests",package.seeall)
 
+local dkjson = require ("dkjson")
 
 
 
@@ -195,47 +196,11 @@ function xmlString(s)
   end
 end
 
-local printJsonKeyValue = function (k,v,indent)
-	print("\n")
-	for i=1,indent do print(" ") end
-	if (k) then
-		print("\""..k.."\":")
-	end
 
-	if (type(v)=="number") then
-		print(xmlString(v))
-	elseif (type(v)=="table") then
-		 if (v._array==NULL) then
-          	print("{\n")
-    		printTableAsJson(v,indent+2)
-    		print("\n}")
-          else
-          	print("[")
-          	printArrayAsJson(v._array,indent+2)
-          	print("\n]")
-          end
-	else
-    	print("\""..xmlString(v).."\"")
-    end
-end
 
-
-printArrayAsJson = function(array,indent)
-	first=true
-	for i,v in ipairs(array) do
-		if not first then print(",") end
-		printJsonKeyValue(NULL,v,indent)
-		first=false
-	end
-end
-
-printTableAsJson = function (dict,indent)
-	first=true
-	for k,v in pairs(dict) do
-		if not first then print(",") end
-		printJsonKeyValue(k,v,indent)
-		first=false
-    end
+printTableAsJson = function (dict)
+	local output=dkjson.encode (dict, { indent = true })
+	print(output)
 end
 
 local printXmlKeyValue = function (k,v,indent)



More information about the vlc-commits mailing list