mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-08-20 14:45:59 +00:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
/*\
|
|
title: $:/core/parsers/jsonparser.js
|
|
type: application/javascript
|
|
module-type: parser
|
|
|
|
Parses a JSON object into a parse tree
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true */
|
|
"use strict";
|
|
|
|
var renderObject = function(obj) {
|
|
var children = [],t;
|
|
if($tw.utils.isArray(obj)) {
|
|
for(t=0; t<obj.length; t++) {
|
|
children.push($tw.Tree.Element("li",{
|
|
"class": ["jsonArrayMember"]
|
|
},[renderObject(obj[t])]));
|
|
}
|
|
return $tw.Tree.Element("ul",{
|
|
"class": ["jsonArray"]
|
|
},children);
|
|
} else if(typeof obj === "object") {
|
|
for(t in obj) {
|
|
children.push($tw.Tree.Element("li",{
|
|
"class": ["jsonObjectMember"]
|
|
},[$tw.Tree.splitLabelNode("JSON",[$tw.Tree.Text(t)],[renderObject(obj[t])])]));
|
|
}
|
|
return $tw.Tree.Element("ul",{
|
|
"class": ["jsonObject"]
|
|
},children);
|
|
} else {
|
|
return $tw.Tree.labelNode("JSON" + (typeof obj),[$tw.Tree.Text(JSON.stringify(obj))],["jsonValue"]);
|
|
}
|
|
};
|
|
|
|
var JSONParser = function(options) {
|
|
this.wiki = options.wiki;
|
|
};
|
|
|
|
JSONParser.prototype.parse = function(type,text) {
|
|
return new $tw.Renderer([renderObject(JSON.parse(text))],new Dependencies());
|
|
};
|
|
|
|
exports["application/json"] = JSONParser;
|
|
|
|
})();
|