bozimmerman / CoffeeMud

Full featured, mature MUD supporting MSP, MXP, OLC, with a web server, mail server, and MORE!
Apache License 2.0
191 stars 100 forks source link

GMCP - Malformed JSON strings #22

Closed JTM-rootstorm closed 6 years ago

JTM-rootstorm commented 6 years ago

Using Mudlet 3.9.0 as client and this package for GMCP stuff.

Error:

 <Lua error:InvalidJSONInput: parse error: invalid object key (must be a string)
          08553,"name":"a ration pack",}{"id":1628722269,"name":"a wat
                   (right here) ------^
 at ..\3rdparty\lua_yajl\lua_yajl.c line 345>

Digging around the scripts from the package show no sign of doing anything but parsing whatever data it's given from the server, so I'm left thinking that a bit of code somewhere is adding in a comma where it shouldn't be; The toString functions for the MiniJSON.java classes under both core and siplet.support are really the only places I can find where this would happen, specifically

for(final Iterator<String> k = keySet().iterator(); k.hasNext();)
            {
                final String keyVar = k.next();
                value.append("\"").append(toJSONString(keyVar)).append("\":");
                final Object obj = get(keyVar);
                appendJSONValue(value, obj);
                if(k.hasNext())
                {
                    value.append(",");
                }
            }
            value.append("}");

I feel like k.hasNext() is causing it to toss another comma in there before the closing brace, but I'm not sure as to why

I guess I'll be poking at it until it either turns out my hunch is wrong (or is not the lib's fault) or someone beats me to the solution

bozimmerman commented 6 years ago

Thanks for letting me know. Actually that block from MiniJSON is perfect. The bug is in CMProtocols.java, where the packet is constructed like so: doc.append("\"name\":\"").append(MiniJSON.toJSONString(I.Name())).append("\","); final String attribs = makeGMCPAttribs(I); if(attribs.length()>0) doc.append("\"attrib\":\"").append(attribs.toString()).append("\",");

--- see the problem? If there are no attributes, then it ends in a comma.

The fix is below. It is in several places, however, so you might just want to pull the latest rev.

doc.append("\"id\":").append(I.hashCode()).append(","); doc.append("\"name\":\"").append(MiniJSON.toJSONString(I.Name())).append("\""); final String attribs = makeGMCPAttribs(I); if(attribs.length()>0) doc.append(",\"attrib\":\"").append(attribs.toString()).append("\"");

JTM-rootstorm commented 6 years ago

Yeah, just a couple minutes more after enabling GMCP debug text and I would've had the right spot. So (not) close

I'll give the latest commit a spin!

JTM-rootstorm commented 6 years ago

Fun one: error still happens because a comma is missing between the } and {

 <Lua error:InvalidJSONInput: parse error: after array element, I expect ',' or ']'
          36131,"name":"a ration pack"}{"id":2110729899,"name":"a wate
                     (right here) ------^
 at ..\3rdparty\lua_yajl\lua_yajl.c line 345>

my sloppy fix to that is

if (i == mob.numItems() - 1) {
    doc.append("}");
} else {
    doc.append("},");
}

though that's char.items.list specific, dont quite know if it'll be needed elsewhere just yet.