开发者

Safe JSON parsing in GWT?

How do we do safe JSON parsing with GWT? It looks like the json parser GWT offers uses eval() - is there a different option which uses the native implementations when available?

Thanks

------------- Edit ----------------------------

Ok I got a script from here:

http://code.google.com/p/json-sans-eval/

which is supposed to not use eval (so should be safe I hope!), and use it like this:

private native JavaScriptObject nativeParseUntruste开发者_如何学CdJson(String jsonString) /*-{ 
    return $wnd.jsonParse(jsonString);    
}-*/; 

it works, does that look ok?

Thanks!!


I don't know of any built-in GWT way to parse JSON without reverting to eval, but you can try using your favorite parser via JSNI then returning a JavaScriptObject to pass into the constructor to a JSONObject, e.g.

native JavaScriptObject native_parse(String json_data) /*-{
  return $wnd.JSON.parse(json_data);    
}-*/;

where JSON.parse can refer to https://github.com/douglascrockford/JSON-js/blob/master/json2.js, e.g. via the following HTML:

<script src="json2.js"></script>

Then try creating a JSONObject via:

new JSONObject(native_parse(json_data));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜