ibmruntimes / yieldable-json

Asynchronous JSON parser and stringify APIs that make use of generator patterns
Other
145 stars 22 forks source link

Escape vertical tab #31

Open tschechniker opened 3 years ago

tschechniker commented 3 years ago

Hi,

just came over this issue. If you have a string including a vertical tab the json is broken. We need to escape it. Could you please extend the escaping list and add "\v" to be escaped too?

Thanks!

Tobi

tschechniker commented 3 years ago

something like this helps:

yieldable-stringify.js

 let escape = {
    '\b': '\\b',
    '\t': '\\t',
    '\n': '\\n',
    '\f': '\\f',
    '\r': '\\r',
    '\v': '\\u000B',
    '"': '\\"',
  };
gireeshpunathil commented 3 years ago

@tschechniker - thanks for reporting this, I will look at this.

daniele-pini commented 2 weeks ago

Hello, I have the same problem with characters \u0000 to \u0019.

Unfortunately, I acquire strings from a 3rd party source that I need to parse, modify and re-stringify, so I also wonder if there are other possible cases, too?

To clarify, in this example:

const yj = require('yieldable-json');

const obj = { value: '\u0000' };

const str1 = JSON.stringify(obj);
console.log('str1: ' + str1);

yj.stringifyAsync(obj, (err, str2) => {
  console.log('str2: ' + (str2));
  const parsed = JSON.parse(str2);
  console.log(parsed);
});

I get:

str1: {"value":"\u0019"}
str2: {"value":""}
undefined:1
{"value":""}
          ^

SyntaxError: Bad control character in string literal in JSON at position 10
    at JSON.parse (<anonymous>)
    at /home/puppy/work/test/index.js:11:23
    at Immediate.<anonymous> (/home/puppy/work/test/node_modules/yieldable-json/yieldable-stringify.js:276:18)
    at process.processImmediate (node:internal/timers:483:21)