If you are solely using JavaScript to access external json data, it may negate the purpose of utilizing a compiler. However, if your JavaScript involves more tasks beyond just parsing json into domain models, then the compiler can prove to be beneficial.
In our parsing process, we retrieve data using bracket notation and then organize it into our own models, which provide us with type checking and other advantages.
When it comes to handling data, I make use of the XHRManager class, which has been incredibly useful for managing data events efficiently.
/**
* @private
* @param {goog.events.Event} evt Event received from XhrIo.
*/
mypath.MyClass.prototype.onDataRecieved_ = function(evt) {
if (evt.type != 'complete') return;
var xhrIo = evt.target;
var data = xhrIo.getResponseJson();
//do something!
};
I must admit that my handling of XHRManager is still a work in progress, as I only recently started implementing it in my codebase.
For parsing tasks, I follow this approach: (Please excuse any rough portions from my code snippet.)
our.class.path.ContestJsonParser.prototype.setContestProperties =
function(contest, element) {
contest.setName(element['description']);
/**
* @type {!number}
*/
var timeAsInt = element['startTime'];
contest.setScheduledStartTime(timeAsInt);
var clockModel = contest.getClockModel();
if (goog.isDefAndNotNull(element['period'])) {
clockModel.setMatchState(element['period']['periodName']);
clockModel.setStateStartTime(element['period']['periodStartTime']);
}
//TODO (Johan) this needs to change today to consider the rest of the stats
//information
var stats = element['statistics'];
if (goog.isObject(stats) && goog.isDefAndNotNull(stats['score'])) {
var score = stats['score'];
contest.setMatchScore(score['home'], score['away']);
} else {
contest.setMatchScore(undefined, undefined); // clears score.
}
};