mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-27 02:20:28 +00:00
52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
/*\
|
|
title: js/JSONParser.js
|
|
|
|
Compiles JSON objects into JavaScript functions that render them in HTML and plain text
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true */
|
|
"use strict";
|
|
|
|
var Renderer = require("./Renderer.js").Renderer,
|
|
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) {
|
|
return new Renderer([renderObject(JSON.parse(text))],new Dependencies(),this.store);
|
|
};
|
|
|
|
exports.JSONParser = JSONParser;
|
|
|
|
})();
|