Page 1 of 1

SABnzbd API Issue Re: slots:""

Posted: September 22nd, 2010, 1:07 pm
by r1pclaw
I've been playing with google's gson (version 1.5) library, and it works wonders for parsing the JSON output (SABnzbd version 0.5.4), however I have come across a bit of an issue with the way "slots" are reported.

When there are no slots in use (noofslots: 0)  slots are reported with:
"slots":""

Which implies to the library that it is a string not an array.
When there are slots in use it is reported as:
"slots":[{"status":"Queued","index":0,"eta":"unknown"......

If there are no values in the array, it should report

"slots":[]

As a result, the library throws
java.lang.ClassCastException: com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonArray

I have been using a string replace before putting it through the GSON parser to get around the issue.
IE

string.replace("slots\":\"\"", "slots\":[]");

Maybe something to be fixed in the future,  I couldn't find this issue in the forums, sorry if it's a double post.

Re: SABnzbd API Issue Re: slots:""

Posted: September 22nd, 2010, 1:18 pm
by shypike
It looks incorrect indeed.
I'll see if I can fix it.

Isn't the problem more in your own code than in gson?
slots : "" is valid json.
It's only that you don't know in advance whether you get a string or a list.

Re: SABnzbd API Issue Re: slots:""

Posted: September 22nd, 2010, 1:23 pm
by r1pclaw
Awesome, that was fast :P

Re: SABnzbd API Issue Re: slots:""

Posted: September 22nd, 2010, 1:26 pm
by r1pclaw
Oh, and that was for the 'queue' JSON response, I haven't investigated the history one yet.

Re: SABnzbd API Issue Re: slots:""

Posted: September 22nd, 2010, 1:30 pm
by r1pclaw
I was looking at this:
https://docs.sonatype.com/display/NX/Em ... cification

So i'm guessing that google is using that kind of standard for parsing.

The code for parsing:

Gson gson = new Gson();
SabQueue que = null;
//This grabs the queue information, cfg is an object holding host,port,SSL,apikey
String data = HttpInterface.getData(buildRequest(cfg,SabQueries.QUEUE,null));
if(data != null)
{
//here is the actual parsing (i strip off the {"queue": and trailing '}'
que = gson.fromJson(data.substring(9,data.length()-1) , SabQueue.class);
}

And it delivers me an object that is fully populated.

Of course, SabQueue is an object that has all of the fields reported by the sabnzbd queue request, and all getter/setters so that gson can figure it out by field names

Re: SABnzbd API Issue Re: slots:""

Posted: September 22nd, 2010, 1:40 pm
by r1pclaw
The "scripts" field is an array as well (on a Queue json request), but it reports an empty array as:
"scripts":[]

Re: SABnzbd API Issue Re: slots:""

Posted: September 22nd, 2010, 4:29 pm
by shypike
Yeah, I found the culprit.

Code: Select all

    if slotinfo:
        info['slots'] = slotinfo
    else:
        info['slots'] = ''
Will fix it in the next release.

Re: SABnzbd API Issue Re: slots:""

Posted: September 23rd, 2010, 7:03 pm
by r1pclaw
Right on, not a difficult issue to get around, just had me stumped for a bit.

Thanks!