简体   繁体   中英

Can the XPage JSON library automatically convert a Java Bean to a JSON representation?

I want to use the com.ibm.commons.util.io.json.* library which comes with the XPages runtime to serialise a Java Bean into JSON.

The question is can it do it automatically by just passing it the object - like you can with the Google library - http://code.google.com/p/google-gson/ or do you need to construct the JSON manually by which I mean passing the individual properties to construct the JSON.

Having trouble locating the documentation for this library, though I have seen some examples:

http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=JSON%20and%20REST%20Samples

http://www-10.lotus.com/ldd/ddwiki.nsf/dx/Sending_requests_in_Java_dds10

Ideally we dont want to use a 3rd party library, even though it works great, because we need to modify the java security properties file which in turn gets wiped if the server gets upgraded.

The com.ibm.commons.util.io.json library is a genric library for converting JSON representations to Java objects, back and forth. By generic, I mean that it uses a factory to both browse and update the Java objects (see: JsonFactory). By implementing such a factory, and implementing the getter/setters for all the properties, one can serialize/deserialize any kind of objects. The JSON library is equiped with a set of predefined factories:

  • JsonJavaFactory, that maps JSON Objects to Java Maps (with a extended version that uses a JsonJavaObject wrapper which is more convenient)
  • JsonJavaScriptFactory, that maps JSON objects to actual JavaScript objects (see: ObjectObject) and Java values (String, Integer...) to JavaScript values (FBSString, FBSNumber...). These objects can be directly used by the server side JS engine.

We don't have a factory for JavaBeans per say, but implementing such a factory should not be a big deal.

The ibm commons library for json works by constructing an object, then adding json properties to the object. It can not auto-serialize an object, and works really only with primitive data types.

I've attached some SSJS code to illustrate how to work with the class. It assumes recordMap is a java map instance with some beans in it, and each bean has 5 fields named fieldName1 through fieldName5. The code iterates through each bean in the map, retrieves the 5 fields, convert the values to JSON, then pushes them into array. Finally the array is put inside another json object that includes the count, and the array itself.

var jsonObjArr = [];

var itr:java.util.Iterator = recordMap.keySet().iterator();
while (itr.hasNext()) {

   var record = recordMap.get(itr.next());
   var jsonObj:com.ibm.commons.util.io.json.JsonJavaObject = 
          new com.ibm.commons.util.io.json.JsonJavaObject();

    jsonObj.putJsonProperty("fieldName1", record.getFieldName1());
    jsonObj.putJsonProperty("fieldName2", record.getFieldName2());
    jsonObj.putJsonProperty("fieldName3", record.getFieldName3());
    jsonObj.putJsonProperty("fieldName4", record.getFieldName4());
    jsonObj.putJsonProperty("fieldName5", record.getFieldName5());
    jsonObj.putJsonProperty("fieldName6", record.getFieldName6());

    jsonObjArr.push(com.ibm.commons.util.io.json.JsonGenerator
        .toJson(com.ibm.commons.util.io.json.JsonJavaFactory.instanceEx, empr));

};

var jsonString = "{" +
    "count:" + @Text(jsonObjArr.length) + "," +
    "employees:" + "[" + jsonObjArr.join(",") + "]" + 
"}";

return jsonString;

Hope this helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM