From 678a4cf779c444cf402ac9538a5373755b267ea0 Mon Sep 17 00:00:00 2001 From: Ravali Yatham Date: Mon, 20 Dec 2021 18:01:58 +0530 Subject: [PATCH] src: handle vertical tab escaping Both APIs fail when the key or value contain vertical tab. Fix that by adding escape sequencing for vertical tab Add two unit tests to validate this scenario. Fixes: #31 --- test/test-parse-verticaltab.js | 22 ++++++++++++++++++++++ test/test-stringify-verticaltab.js | 27 +++++++++++++++++++++++++++ yieldable-stringify.js | 1 + 3 files changed, 50 insertions(+) create mode 100644 test/test-parse-verticaltab.js create mode 100644 test/test-stringify-verticaltab.js diff --git a/test/test-parse-verticaltab.js b/test/test-parse-verticaltab.js new file mode 100644 index 0000000..76322b3 --- /dev/null +++ b/test/test-parse-verticaltab.js @@ -0,0 +1,22 @@ +'use strict'; + +const yj = require('../index'); +const tap = require('tap'); + +//string with nested quotes +const str = '{"name":"Ila Gould","age":22,"gender":"female","nested":"\\u000bcheck"}'; + +yj.parseAsync(str, (error, obj) => { + if (!error){ + tap.equal(str,JSON.stringify(obj)); + yj.stringifyAsync(obj,(err,data) => { + if(!err){ + tap.equal(data,str); + } + else + tap.fail(err); + }) + } + else + tap.fail(error); +}); diff --git a/test/test-stringify-verticaltab.js b/test/test-stringify-verticaltab.js new file mode 100644 index 0000000..c43e2bb --- /dev/null +++ b/test/test-stringify-verticaltab.js @@ -0,0 +1,27 @@ +'use strict'; + +const yj = require('../index'); +const tap = require('tap'); + +//Object with nested quotes +const obj = { + name: 'Jacqueline Poole', + gender: 'female', + age: 40, + a:"\vb" +}; + +yj.stringifyAsync(obj, (err, str) => { + if (!err){ + tap.equal(JSON.stringify(obj), str); + yj.parseAsync(str,(error,data) => { + if(!error){ + tap.same(data,obj); + } + else + tap.fail(error); +}) + } + else + tap.fail(err); +}); diff --git a/yieldable-stringify.js b/yieldable-stringify.js index b3814f6..56a4faa 100755 --- a/yieldable-stringify.js +++ b/yieldable-stringify.js @@ -47,6 +47,7 @@ let normalize = (string, flagN) => { '\n': '\\n', '\f': '\\f', '\r': '\\r', + '\v': '\\u000b', '"': '\\"', }; // Escape is implemented globally