cJobject Class in Google Apps Script

The Rest to Excel library is a set of classes and modules that simplify the populating of Excel sheets with data from RestFul APIs. Google Apps Script of course already has plenty of much better ways to deal with this but, but the objective here is to manage the transition from Excel to Google Docs, and to be able to use the same library definitions and code for both platforms.

cJobject approach

The complex part of the cJobject implementation in VBA was the jSon parser. I won't need to reimplement that, since GAS has inbuilt jSon management (it is javaScript after all), but I still want to be able to use cJobject, since all the code that will be converted over will be using that object. Since the GAS jSon parser is all about populating native javaScript objects with jSON and visa versa, then a useful trick would be to be able to covert cJobjects to and from native javaScript objects. That way, all the existing code that uses cJobject can co-exist and still allow for the use of the inbuilt GAS capabilities.

cJobject to Native

Since the cJobject, is after all, an attempt to make VBA look like javaScript whilst handling recursive objects, the conversion is really not too complex. Let's take a look at how to covert a cJobject to a native javaScript object heirachy. It is of course recursive, given the unstructured depth - but it is rather concise and works very nicely.

Serializing a cJobject

This is now a cinch. Just convert it to a javaScript native object as above, and use GAS jSon functions to stringify it. Nice, but we don't have the beautifier available as I had implemented in VBA.

cJobject.prototype.serialize = function(optBeautify){ // beautify not implemented as im using the built in JSONifier // the VBA version uses my own. return JSON.stringify(this.toNative());};

Native to cJobject

Coming the other way - creating a cJobject from a javaScript native object structure is a little more tricky, but still the same recursive approach, and is even more concise.

cJobject.prototype.fromNative = function(native) { // take a native jscript object and convert it to a cjobject var self = this;

// if its an array then create an array root, and recurse with each array element if (isArray(native)) { self.addArray(); for (var j = 0; j < native.length;j++) { self.add().fromNative(native[j]); } } // if its an object, then add each key as the next lower cjobject else if (IsObject(native)) { for (k in native) { self.add (k).fromNative(native[k]); } } // finally we get to the value; else { self.xValue = native; } return self;};

Deserializing a cJobject from a jSon string

It's simply converting jSon to a native javaScript object, then converting that native object to a cJobject as above.

cJobject.prototype.deSerialize = function(s) { // the VBA version uses its own jSON parser. This one will use the JSON.parse built in // simply we just parse it to native jscript object, then convert it to a cJobject return this.fromNative(JSON.parse(s));};