From a71150ae815b221dac18ee248eb848f498abc112 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Wed, 5 Apr 2023 12:26:40 +0100 Subject: [PATCH] jsonset: add support for assigning JSON strings --- core/modules/filters/json-ops.js | 5 ++++- editions/test/tiddlers/tests/test-json-filters.js | 3 +++ editions/tw5.com/tiddlers/filters/jsonset.tid | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/core/modules/filters/json-ops.js b/core/modules/filters/json-ops.js index f4b74e12d..51e509432 100644 --- a/core/modules/filters/json-ops.js +++ b/core/modules/filters/json-ops.js @@ -75,7 +75,7 @@ exports["jsonset"] = function(source,operator,options) { value = operator.operands[operator.operands.length - 1], results = []; if(operator.operands.length === 1 && operator.operands[0] === "") { - value = undefined; + value = undefined; // Prevents the value from being assigned } switch(type) { case "string": @@ -99,6 +99,9 @@ exports["jsonset"] = function(source,operator,options) { indexes = operator.operands; value = null; break; + case "json": + value = $tw.utils.parseJSONSafe(value,function() {return undefined;}); + break; default: // Use value unchanged break; diff --git a/editions/test/tiddlers/tests/test-json-filters.js b/editions/test/tiddlers/tests/test-json-filters.js index 3d0f16680..b2f2c8e82 100644 --- a/editions/test/tiddlers/tests/test-json-filters.js +++ b/editions/test/tiddlers/tests/test-json-filters.js @@ -116,6 +116,9 @@ describe("json filter tests", function() { expect(wiki.filterTiddlers("[{First}jsonset:array[d],[f],[5]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null,[]]}}']); expect(wiki.filterTiddlers("[{First}jsonset:object[d],[f],[5]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null,{}]}}']); expect(wiki.filterTiddlers("[{First}jsonset[missing],[id],[Antelope]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]}}']); + expect(wiki.filterTiddlers("[{First}jsonset:json[\"Antelope\"]]")).toEqual(['"Antelope"']); + expect(wiki.filterTiddlers("[{First}jsonset:json[id],[{\"a\":313}]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]},"id":{"a":313}}']); + expect(wiki.filterTiddlers("[{First}jsonset:json[notjson]]")).toEqual(['{"a":"one","b":"","c":1.618,"d":{"e":"four","f":["five","six",true,false,null]}}']); }); it("should support the format:json operator", function() { diff --git a/editions/tw5.com/tiddlers/filters/jsonset.tid b/editions/tw5.com/tiddlers/filters/jsonset.tid index 5e6b28e16..1c83ac8c6 100644 --- a/editions/tw5.com/tiddlers/filters/jsonset.tid +++ b/editions/tw5.com/tiddlers/filters/jsonset.tid @@ -61,8 +61,9 @@ The data type of the value to be assigned to the property can be specified with |!Suffix |!Description | |''string'' |The string is specified as the final operand | -|''boolean'' |The boolean value is true if the final operand is the string "true" and false if the final operand is the string "false". Any other value for the final string results in the property not being assigned | +|''boolean'' |The boolean value is true if the final operand is the string "true" and false if the final operand is the string "false". Any other value for the final string results prevents the property from being assigned | |''number'' |The numeric value is taken from the final operand. Invalid numbers are interpreted as zero | +|''json'' |The JSON string value is taken from the final operand. Invalid JSON prevents the property from being assigned | |''object'' |An empty object is assigned to the property. The final operand is not used as a value | |''array'' |An empty array is assigned to the property. The final operand is not used as a value | |''null'' |The special value null is assigned to the property. The final operand is not used as a value |