Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

URL-Mangled JSON in NDK extensions

If I pass the following through JNext

 

{  slGameId : "cdc1ee7e-403b-4d68-ac1a-cb1ebf36f782",
    slGameSecret : "dNVkPkrrp68BOeOu4NqUx1ZeF+iWY21BEFF4hT4A7spAuIYUhiE49w==",
    slGameVersion : "1.0",
    slCurrency : "ACF",
    slLanguageCode : "en"
}

 

I get this out of the JSON parser in the extension...

 

slGameId = "%22cdc1ee7e-403b-4d68-ac1a-cb1ebf36f782%22";
slGameSecret = "%22dNVkPkrrp68BOeOu4NqUx1ZeF+iWY21BEFF4hT4A7spAuIYUhiE49w==%22";
slGameVersion = "%221.0%22";
slCurrency = "%22ACF%22";
slLanguageCode = "%22en%22";
 

 

Iy you pass URLs it gets a LOT worse cos the of URL encoding and C++ doesn't have a handy URLDecode like JavaScript does.

 

Is there some simple way around this or do I have to keep on converting the strings programatically?

 

 

 

 




Click the like button if you find my posts useful!
Retired
Posts: 856
Registered: ‎07-15-2008
My Device: Passport
My Carrier: Bell

Re: URL-Mangled JSON in NDK extensions

You did a JSON.stringify on the JSON before passing it to JNEXT?

I assume you're doing exactly what the template does?

Tim Windsor
Open Source Technical Lead
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: URL-Mangled JSON in NDK extensions

Tried with and without and stringifying in various places before JNext

 

webworks.js does a stringify itself anyway/ When executing the send it applies JSON.stringify(params)

 

Most of the demos and existing extensions don't send through stuff that needs to come out the other end as passed anyway so it's not gonna be that common an issue.

 

The 'example' code above includes slGameSecret which is Base64 encoded and as such ends up replacing the trailing equal symbols with two %3Ds

 

Took me a while to work out while Scoreloop was starting fine in Cascades with the same code but falling over in NDK Extension even after I stripped the enclosing %22s. I was getting SC_Error_h = 111 (not in the header) which translates to "Invalid Game Secret"

 

I'm meant to be banging my headon the wall with Scoreloop, not URLDecoding Smiley Happy




Click the like button if you find my posts useful!
Retired
Posts: 856
Registered: ‎07-15-2008
My Device: Passport
My Carrier: Bell

Re: URL-Mangled JSON in NDK extensions

I'll mess around with it and see what I can figure out.

Tim Windsor
Open Source Technical Lead
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: URL-Mangled JSON in NDK extensions

Don't worry about it Tim I'm removing the issue from the equation (hard code - write unescape at end) The issue is that I don't know std::string - or much of C++ I'll combine issues here... My other from yesterday - logging - AMAZINGLY useful I did a hacky open file, write to file, read file, return info to user and a few strategically place fprintf WOW this wants introducing to Template with a debug switch - I'd debug do this else do that



Click the like button if you find my posts useful!
Retired
Posts: 856
Registered: ‎07-15-2008
My Device: Passport
My Carrier: Bell

Re: URL-Mangled JSON in NDK extensions

Yes, that sounds very useful. Do you want to send me what you have done, when you get a chance? Or I can hack away on it.

Tim Windsor
Open Source Technical Lead
Developer
Posts: 1,280
Registered: ‎03-03-2011
My Device: Playbook, Z10, Q10, Z30 with Files & Folders and Orbit of course
My Carrier: Vodafone

Re: URL-Mangled JSON in NDK extensions

[ Edited ]

The problem seems to be in clientFiles\webworks<version>.js:

 

 function createWebworksReady() {
        function RemoteFunctionCall(functionUri) {
...
            this.addParam = function (name, value) {
                params[name] = encodeURIComponent(JSON.stringify(value));
            };

Solution is to wrap your arguments in another object in client.js:

 

_self.myMethod = function (param1, param2, param3) {
		var args = {data&colon; {param1:param1, param2:param2, param3:param3}};
		return window.webworks.execSync(_ID, "myMethod", args);
	};

Parse and decode them in index.js:

 

module.exports = {
	myMethod: function (success, fail, args) {	
		args = JSON.parse(decodeURIComponent(args["data"]));
		success(template.getInstance().execute(args));
	}
};

Then decode them again in C++:

 

string MyExtension::InvokeMethod(const string& command) {
	try {
		int space;
		std::string strMethod;
		std::string strArgs;
		Json::Reader jsr;
		Json::Value args;

		// split command and arguments
		space = command.find_first_of(" ");
		if(space<0)
			throw runtime_error("no arguments");

		strMethod = command.substr(0, space);
		strArgs = command.substr(space+1, command.length());

		if(!jsr.parse(strArgs, args))
			throw runtime_error("could not parse JSON");

... do something with strMethod, args["param1"], args["param2"] etc.

 

This WebWorks extension architecture seems  very convoluted and inefficient to me. Way too much plumbing. There must be a better way.

Files & Folders, the unified file & cloud manager for PlayBook and BB10 with SkyDrive, SugarSync, Box, Dropbox, Google Drive, Google Docs. Free 3-day trial! - Jon Webb - Innovatology - Utrecht, Netherlands
Highlighted
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: URL-Mangled JSON in NDK extensions

I ended up writing some C to do URLDecoding and use it in a few extensions now




Click the like button if you find my posts useful!
Retired
Posts: 856
Registered: ‎07-15-2008
My Device: Passport
My Carrier: Bell

Re: URL-Mangled JSON in NDK extensions

Some of this plumbing comes from early design decisions that are difficult or impossible to change without serious platform breaking issues. That's why I created the extension templates, so it would remove some of the guesswork out of the process of writing them.

Tim Windsor
Open Source Technical Lead
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: URL-Mangled JSON in NDK extensions

Talking of the extensions templates which is the recommended one?

 

QDE always gives you memory while most (mine included) are based on Template

 

It appears that webworks has exolved since Feb when I was introduced to templates via LED - that was memory-based and as I remember didn't insist on Stringifying everything as is currently the case.

 

The most non-instrusive way I've found of getting around the mangling is the bit of C on the other end method.




Click the like button if you find my posts useful!