谷歌闭包编译器和json [英] google closure compiler and json
问题描述
我有一个json字符串,我解析,然后用点表示法访问对象的属性。但是,在谷歌闭包编译器中,点符号( MyObject.PropertyName
)会发出警告,表明该属性未定义。
I have a json string that I parse and then access the properties of the object with the dot notation. However, in the google closure compiler, the dot notation (MyObject.PropertyName
) gives warning that the property isn't defined.
目前,我正在使用的解决方案是将我的代码转换为括号表示法( MyObject ['PropertyName']
)。这将删除警告,但也会阻止编译器执行其工作。另一方面,当我写 JSON.stringify(MyObject)
时,服务器会收到一个字符串,其属性名称是可以理解的。
For now, the solution I'm using is to convert my code into bracket notation (MyObject['PropertyName']
). This removes the warning but also prevents the compiler from doing its job. On the other hand, when I write JSON.stringify(MyObject)
, the server receives a string with property names that are understandable.
所以我的问题是在处理运行时反序列化和序列化的json对象时,如何在高级模式下最好地使用google编译器。
So my question is how do we best use the google compiler in advanced mode when working with json objects that are deserialized and serialized at runtime.
推荐答案
如果您要编写的唯一JavaScript是访问外部json,那么它就无法使用编译器。但是,如果除了将json解析为域模型之外,你甚至还有一些微不足道的JavaScript工作,那么编译器可能会很有用。
If the only JavaScript you are going to write is accessing external json, then it defeats the point of using the compiler. However, if you have even a trivial amount of JavaScript which does work besides parsing your json into domain models then the compiler may be useful.
在我们的解析器中,我们访问我们的数据通过括号表示法,我们可以正确获取数据。从那里我们将数据填充到我们自己的模型中,我们使用它们。符号。这些被大量重命名,给我们进行类型检查和所有这些优点。
In our parsers we access our data via bracket notation so we can get the data properly. From there we stuff the data into our own models, which we use the . notation on. These get wildly renamed, gives us type checking and all of that goodness.
编辑>>
对于数据,我使用 XHRManager 。这是一个非常好的课程。当我从该池中获取数据事件时,我按如下方式处理它。
Edit>> For data I use the XHRManager. This is one seriously nice class. When I get a data event from that pool I handle it as follows.
/**
* @private
* @param {goog.events.Event} evt The event recieved from the XhrIo.
*/
mypath.MyClass.prototype.onDataRecieved_ = function(evt) {
if (evt.type != 'complete') return;
var xhrIo = evt.target;
var data = xhrIo.getResponseJson();
//do somethign!
};
我必须警告你,我的XHRManager处理仍然有待改进。我上周才重构我的代码才开始使用它。
I have to warn you, my XHRManager handling still leaves a fair bit to be desired. I only refactored my code last week to start using it.
对于解析我这样做:(这是我的代码库中的一些原始内容,所以忽略一些丑陋的。)
For parsing I do this: (This is some raw stuff from my code base, so ignore some of the ugly.)
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.
}
};
这篇关于谷歌闭包编译器和json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!