How can I add a JSON object to the scope of a cloud variable in Java?

I used many JSON objects in applicationScope, sessionScope and viewScope to track related data. Writing and reading this data 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 that I wrote, I learned to read these variables that were written using SSJS, using the com.ibm.jscript.std.ObjectObject package with this code:

 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, which can then be read by SSJS and other Java Beans in the same way. I managed to write a region using Map and Hashtable, but they break the logic when trying to read using ObjectObject.

So, how can I create a new record in an area using ObjectObject and / or FBSValue packages? I cannot find how to create a new FBSValue which can then be added to ObjectObject. I am sure this is a simple thing that Newbs like me missed.

/ Newbs

+7
source share
1 answer

You can create an empty ObjectObject, fill it with FBSValues, and simply place it directly in the Map area:

 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 received later (as in the above examples), SSJS will see it as JSON, Java will see it exactly as it was saved.

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

Just remember to include only true JSON (strings, numbers, booleans, arrays, objects) if you store it somewhere higher than requestScope; in particular, FunctionObject does not implement Serializable, therefore JSON is safe for storage, JavaScript is not. Strictly speaking, it becomes toxic only when saved in viewScope in 8.5.2 and 8.5.3 (and even then only if the application’s save option is not set to store all pages in memory). But if IBM ever implements cluster support, then all objects stored in sessionScope and applicationScope must be serializable in order to provide cross-server public transport ... so in the interests of future design validation it is advisable to adhere to this principle for anything stored longer than the duration of a single request.

+6
source

All Articles