D.Bugger
D.Bugger

Reputation: 2359

XPages: generate JSON server side, use client side

It should not be such a difficult hurdle, but I just can't see how to get past this. I want to generate a JSON structure on the server, using ArrayObject and ObjectObject objects, and use it both as server-side datasource (works) and also client-side. How can I transport the data easily from the server to the client?

I tried with toJson in a ScriptBlock, but it fails (due to a Java error, something with FBSUndefined if I remember correctly). I also tried with toJson in a jsonRpcService control, with the same error. I'll try to find some other way to stringify the object I have, but that's about it. I could do an Ajax-call, but that would be a last resort solution.

Do you have other/better ways to transfer a server object to the browser?

Thanks!!

Upvotes: 0

Views: 366

Answers (3)

xpages-noob
xpages-noob

Reputation: 1579

Getting a FBSUndefined error indicates that the object you want to create the JSON of explicitely contains an "undefined" value. This can for example happen if you set the property of an object equal to the property of another object which does not contain this property.

IMO your data is the problem and not the way you want to create the JSON. You should first check the code that generates the object. If you can't find the part that causes the problem you could write a helper function to iteratively go through the object and search for the undefined value.

Upvotes: 1

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15739

From Java, I would recommend using com.ibm.commons.util.io.json.JsonJavaArray and com.ibm.commons.util.io.json.JsonJavaObject. They work just like Maps / Lists and can contain those interfaces (so I add an ArrayList as into a JsonJavaObject via .put(myKey, myArrayList)). To output as JSON for an Output Script block, for example, just call JsonJavaObject.toString() or JsonJavaArray.toString(). It's the solution I'll be showing in an upcoming blog post on charts.

Upvotes: 3

stwissel
stwissel

Reputation: 20384

The easiest way is to wrap your code into a service bean and use a custom Rest service (that's a control in the ExtLib). I wrote an article on the steps:

https://www.wissel.net/blog/2014/10/custom-rest-service-in-xpages-using-a-service-bean.html

If you only use SSJS... the approach still works. You can code the response inside the control using SSJS.

Upvotes: 3

Related Questions