From edd3156430dd94b0ced630a1b70f2e2ef0ff6c36 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sat, 13 Apr 2019 14:47:27 +0100 Subject: [PATCH] Improve loading/importing of JSON files First part of fix for #3875 The idea is to do a better of job of distinguishing JSON files that contain tiddlers versus those that contain plain blobs of JSON that should be stored as a single application/json tiddler. Under Node.js, .json files with an accompanying metafile are always treated as a JSON blob. Without a meta file, those that appear to not contain valid tiddlers are returned as a JSON blob, otherwise the tiddlers within the file are imported. In the browser, we don't have .meta files so we rely on the valid tiddler check. --- boot/boot.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/boot/boot.js b/boot/boot.js index f4add931b..fada4014d 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -1469,8 +1469,40 @@ $tw.modules.define("$:/boot/tiddlerdeserializer/html","tiddlerdeserializer",{ }); $tw.modules.define("$:/boot/tiddlerdeserializer/json","tiddlerdeserializer",{ "application/json": function(text,fields) { - var data = JSON.parse(text); - return $tw.utils.isArray(data) ? data : [data]; + var isTiddlerValid = function(data) { + // Not valid if it's not an object with a title property + if(typeof(data) !== "object" || !$tw.utils.hop(data,"title")) { + return false; + } + for(var f in data) { + if($tw.utils.hop(data,f)) { + // Check field name doesn't contain whitespace or control characters + if(typeof(data[f]) !== "string" || /[\x00-\x1F\s]/.test(f)) { + return false; + } + } + } + return true; + }, + isTiddlerArrayValid = function(data) { + for(var t=0; t