Home page logo
/

nmap-dev logo Nmap Development mailing list archives

Re: json library
From: Toni Ruottu <toni.ruottu () iki fi>
Date: Wed, 29 Jun 2011 18:34:55 +0300

Ok. This seems like the way to go. Thank you!

On Wed, Jun 29, 2011 at 6:29 PM, Daniel Miller <bonsaiviking () gmail com> wrote:
Lua has a built-in way to signal different behaviors: metatables. Here's a
patch for the json library that checks the metatable for a "json" key. It
can be set to "array" or "object", to override the default behavior.


Index: nselib/json.lua
===================================================================
--- nselib/json.lua     (revision 24436)
+++ nselib/json.lua     (working copy)
@@ -95,21 +95,22 @@
        elseif type(obj) == "string" then
                return escape(obj)
        elseif type(obj) == "table" then
-               local k, v, elems
+               local k, v, elems, mtval
                elems = {}
-               if #obj > 0 then
-                       -- Array
-                       for _, v in ipairs(obj) do
-                               elems[#elems + 1] = generate(v)
-                       end
-                       return "[" .. table.concat(elems, ", ") .. "]"
-               else
-                       -- Object
-                       for k, v in pairs(obj) do
-                               elems[#elems + 1] = escape(k) .. ": " ..
generate(v)
-                       end
-                       return "{" .. table.concat(elems, ", ") .. "}"
-               end
+    mtval = rawget(getmetatable(obj) or {}, "json")
+    if mtval == "array" or (mtval ~= "object" and #obj > 0) then
+      -- Array
+      for _, v in ipairs(obj) do
+        elems[#elems + 1] = generate(v)
+      end
+      return "[" .. table.concat(elems, ", ") .. "]"
+    else
+      -- Object
+      for k, v in pairs(obj) do
+        elems[#elems + 1] = escape(k) .. ": " .. generate(v)
+      end
+      return "{" .. table.concat(elems, ", ") .. "}"
+    end
        else
                error("Unknown data type in generate")
        end

Here's example code using this feature:

local t = {'one', 'two', name="foo"}
print(json.generate(t)) -- prints ["one", "two"]
setmetatable(t, {json="object"})
print(json.generate(t)) -- prints {"1": "one", "2": "two", "name": "foo"}
t={}
setmetatable(t, {json="array"})
print(json.generate(t)) -- prints []

Hope this helps!
Dan
On Wed, Jun 29, 2011 at 10:00 AM, Toni Ruottu <toni.ruottu () iki fi> wrote:

"{}" is probably needed as well. I've been looking at ways we could
escape the normal behaviour. Lua has byte strings while json has
unicode strings. One option might be using a Lua-object with an
invalid utf-8 string to signal, that we want that object to be treated
in some specific manner. I am not sure, if this is the best way,
though. :-/

On Wed, Jun 29, 2011 at 5:01 PM, Gorjan Petrovski <mogi57 () gmail com>
wrote:
I've looked into the json library and found that if it encounters a
NULL object, then it just returns string with a value "null", so that
would make the "{}" representation redundant.
Well JSON-wise, is there such a thing as a string representing a
nameless empty object when there is a definition for nil?
Has anyone ever encountered the use of "{}"?
If the answer to either of these questions is no, then we can make the
json library default to "[]" when it sees an empty table.

On the other hand if the "{}" is used more than "[]",  a hack has to
be used, and we should probably note this case in the NSEDoc.

Cheers,
Gorjan

On Wed, Jun 29, 2011 at 12:41 AM, Toni Ruottu <toni.ruottu () iki fi>
wrote:
I am trying to use the json library to interact with a server that
uses json-rpc. The server expects me ot pass an empty argument list,
i.e. "[]". My problem is that json.generate({}) results in "{}", and
the server gives me an error for sending an object instead of an
array. Is this something we should fix in the json library, or should
I just come up with some kind of a hack?

 --Toni
_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://seclists.org/nmap-dev/




--
Gorjan

_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://seclists.org/nmap-dev/


_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://seclists.org/nmap-dev/


  By Date           By Thread  

Current thread:
[ Nmap | Sec Tools | Mailing Lists | Site News | About/Contact | Advertising | Privacy ]