Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a JSON object to a scoped variable in Java?

Tags:

xpages

I have used many JSON object in applicationScope, sessionScope, and viewScope to track related data. Writing and reading these in SSJS is very simple:`

//Create a app scope variable
applicationScope.put("myvarname", {p1:"part 1", p2:"part2"});

// read and use the app scope variable ...
var myvar = applicationScope.get("myvarname");
//Work with parts as myvar.p1, myvar.p2, etc...

In the Java code I have been writing I have learned to read these variables which were written using SSJS using the com.ibm.jscript.std.ObjectObject package with code like this:

ObjectObject myvar = (ObjectObject) ExtLibUtil
        .getApplicationScope().get(dbkey);
FBSValue localFBS = myvar.get("p1");
String myp1 = localFBS.stringValue();
localFBS = myvar.get("p2");
String myp2 = localFBS.stringValue();

Now, of course, I want to write a new entry using the Java Bean that can then be read by SSJS and other Java Beans in the same manner. I managed to write to the scope using a Map and a Hashtable, but these crash the logic when trying to read using the ObjectObject.

So, how would I go about building a new entry in the scope using the ObjectObject and/or FBSValue packages? I cannot find how to create a new FBSValue that can then be added to an ObjectObject. I am sure it is a simple thing a Newbs like me has missed.

/Newbs

like image 617
Newbs Avatar asked Mar 28 '12 21:03

Newbs


1 Answers

You can construct an empty ObjectObject, populate it with FBSValues, and just put it directly into the scope Map:

ObjectObject myvar = new ObjectObject();
try {
    myvar.put("p1", FBSUtility.wrap("part 1"));
    myvar.put("p2", FBSUtility.wrap("part 2"));
} catch (InterpretException e) {
    e.printStackTrace();
}
Map<String, Object> applicationScope = ExtLibUtil.getApplicationScope();
applicationScope.put("myvarname", myvar);

When retrieving it later (as in the examples you provided), SSJS will see it as JSON, Java will see it exactly as it was stored.

If you need to store deeper hierarchies, you can put instances of ArrayObject and ObjectObject inside an ObjectObject in addition to primitives, so, just like JSON itself, you can nest these as deep as you need.

Just be sure to only include true JSON (strings, numbers, booleans, arrays, objects) if you'll be storing it anywhere higher than the requestScope; specifically, FunctionObject does not implement Serializable, so JSON is safe to store, JavaScript is not. Strictly speaking, this only becomes toxic when stored in the viewScope in 8.5.2 and 8.5.3 (and even then, only when the application's persistence option is not set to keep all pages in memory). But if IBM ever implements cluster support, then all objects stored in sessionScope and applicationScope will need to be serializable to allow for inter-server state transport... so, in the interest of future-proofing the design, it's wise to hold to this principle for anything stored longer than the duration of a single request.

like image 114
Tim Tripcony Avatar answered Nov 16 '22 01:11

Tim Tripcony