User42590
User42590

Reputation: 2531

JSON.parse() is not working in Worklight Hybrid Adapter

I am trying to use Adapter for Push Notification. I followed all steps define in this IBM Worklight Developer site for Push Notification and make a project and its working. Now further i want to send notifications to a specific device. For this worklight give a method getDeviceSubscriptions() which return JSON array containing number of object for each subscribed device.

deviceSubscriptions = userSubscription.getDeviceSubscriptions() ;

the JSON include:

[{

"platform":"Google",

"eventSourceId":"PushAdapter.PushEventSource",

"alias":"myPush",

"token":" ",

"userAgent":" ",

"device":" ",

"applicationId":" ",

"options":{}

}]

Now i want to use the data in JSON for getting device and token. Now for this i use JSON.stringify(deviceSubscriptions) this method convert deviceSubscriptions into string. But i want that i can direct access to token and device in JSON array for this i used JSON.parse(deviceSubscriptions ) but this giving error:

"Ecma Error: TypeError: Cannot find default value for object. (C%3A%5Cworkspace%5CFINAL%5Cadapters%5CPush/Push-impl.js#43)"

Line 43 is:

JSON.parse(deviceSubscriptions)

Any help would be appreciated

Upvotes: 0

Views: 1246

Answers (1)

cnandreu
cnandreu

Reputation: 5111

JavaScript code for the Worklight Adapters runs on the Worklight Server using Mozilla Rhino. Rhino is an open-source implementation of JavaScript written entirely in Java. You may not have access to everything the JavaScript engines running on the clients have access to.

Seems like either JSON or JSON.parse is undefined. Try adding the following library to your Worklight Adapter [name]-impl.js file:

json2.js: This file creates a JSON property in the global object, if there isn't already one, setting its value to an object containing a stringify method and a parse method. The parse method uses the eval method to do the parsing, guarding it with several regular expressions to defend against accidental code execution hazards. On current browsers, this file does nothing, prefering the built-in JSON object.

Source.

Upvotes: 3

Related Questions