Skip to content

Instantly share code, notes, and snippets.

@a0viedo
Last active August 29, 2015 14:01
Show Gist options
  • Save a0viedo/0fafc6475a0c8fb1e761 to your computer and use it in GitHub Desktop.
Save a0viedo/0fafc6475a0c8fb1e761 to your computer and use it in GitHub Desktop.
Complex example of Gson serialization to JSON and then back to an Object from JS
// you should have java npm package installed and arapi81_build001.jar and gson-2.2.4.jar in the same folder where you run this code
var java = require("java");
java.classpath.push('arapi81_build001.jar');
java.classpath.push('gson-2.2.4.jar')
var Arrays = java.import('java.util.Arrays');
var gson = java.newInstanceSync('com.google.gson.Gson');
java.newInstance('com.bmc.arsys.api.RegularForm', function(err, result){
var form = result;
form.setDefaultVUI('option 1', function (err){
console.log('originalForm -> defaultVUI:', 'option 1');
form.setKey('option 2', function(err) {
java.newInstance('java.util.ArrayList', function(err, arrayList) {
java.newInstance('com.bmc.arsys.api.PermissionInfo', function(err, permission) {
arrayList.add(permission);
arrayList.toArray(function(err, permissionArray){
Arrays.asList(permissionArray, function(err, permissionList) {
form.setPermissions(permissionList, function(err) {
console.log('originalForm -> permissions:', permissionList);
form.getKey(function(err, key){
console.log('originalForm -> key:', key);
gson.toJson(form, function(err, formJson) {
console.log('form in JSON', formJson);
form.getClass(function(err, formClass){
gson.fromJson(formJson, formClass, function(err, recoveredForm) {
console.log('recovered ->', err, recoveredForm);
recoveredForm.getKey(function(err, key){
console.log('recoveredForm -> key:',key);
recoveredForm.getPermissions(function(err, permissions) {
console.log('recoveredForm -> permissions:', permissions);
recoveredForm.getDefaultVUI(function(err, defaultVUI) {
console.log('recoveredForm -> defaultVUI:', defaultVUI);
gson.toJson(recoveredForm, function(err, formJson2){
var areEqual = formJson2 === formJson;
console.log('JSON from recovered object is equal than the JSON obtained before:', areEqual);
})
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment