2012-03-02 11:31:39 +00:00
|
|
|
/*\
|
|
|
|
title: js/JSONParser.js
|
|
|
|
|
|
|
|
Compiles JSON objects into JavaScript functions that render them in HTML and plain text
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
2012-03-03 18:33:16 +00:00
|
|
|
var Renderer = require("./Renderer.js").Renderer,
|
2012-03-02 11:31:39 +00:00
|
|
|
Dependencies = require("./Dependencies.js").Dependencies,
|
|
|
|
utils = require("./Utils.js");
|
|
|
|
|
|
|
|
var renderObject = function(obj) {
|
|
|
|
var children = [],t;
|
|
|
|
if(obj instanceof Array) {
|
|
|
|
for(t=0; t<obj.length; t++) {
|
|
|
|
children.push(Renderer.ElementNode("li",{
|
|
|
|
"class": ["jsonArrayMember"]
|
|
|
|
},[renderObject(obj[t])]));
|
|
|
|
}
|
|
|
|
return Renderer.ElementNode("ul",{
|
|
|
|
"class": ["jsonArray"]
|
|
|
|
},children);
|
|
|
|
} else if(typeof obj === "object") {
|
|
|
|
for(t in obj) {
|
|
|
|
children.push(Renderer.ElementNode("li",{
|
|
|
|
"class": ["jsonObjectMember"]
|
|
|
|
},[Renderer.SplitLabelNode("JSON",[Renderer.TextNode(t)],[renderObject(obj[t])])]));
|
|
|
|
}
|
|
|
|
return Renderer.ElementNode("ul",{
|
|
|
|
"class": ["jsonObject"]
|
|
|
|
},children);
|
|
|
|
} else {
|
|
|
|
return Renderer.LabelNode("JSON" + (typeof obj),[Renderer.TextNode(JSON.stringify(obj))],["jsonValue"]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var JSONParser = function(options) {
|
|
|
|
this.store = options.store;
|
|
|
|
};
|
|
|
|
|
|
|
|
JSONParser.prototype.parse = function(type,text) {
|
2012-03-03 18:33:16 +00:00
|
|
|
return new Renderer([renderObject(JSON.parse(text))],new Dependencies(),this.store);
|
2012-03-02 11:31:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.JSONParser = JSONParser;
|
|
|
|
|
|
|
|
})();
|