lucagez / slow-json-stringify

The slowest stringifier in the known universe. Just kidding, it's the fastest (:
https://github.com/lucagez/slow-json-stringify
MIT License
468 stars 17 forks source link

Missing commas and quotation marks in JSON output #33

Closed daddy-cool closed 2 years ago

daddy-cool commented 2 years ago

Hi I discovered a pretty weird issue and I didn't find any open issue for this.
Basically what happens is that if you stringify an object that doesn't have every key as defined in the schema you will have missing commas and quotation marks on every subsequent stringify-action for objects that do have all keys.

Using my code snippet below you can reproduce this issue:

const stringify = sjs({
  a: attr('string'),
  b: attr('number'),
  c: attr('boolean')
})

// full input -> no issues
stringify({ a: 'world', b: 42, c: true }) // {"a":"world","b":42,"c":true}

// lets remove "b" -> no issues
stringify({ a: 'world', c: true }) // {"a":"world","c":true}

// full input -> missing comma after "b"
stringify({ a: 'world', b: 42, c: true }) // {"a":"world","b":42"c":true}

// lets remove "a" -> missing comma after "b"
stringify({ b: 42, c: true }) // {"b":42"c":true}

// full input -> missing two commas and one quotation mark
stringify({ a: 'world', b: 42, c: true }) // {"a":"world"b":42"c":true}

I tested on the latest node.js v12, v14 and v16 version

lucagez commented 2 years ago

Thank you very much for reporting the issue! The issue has been identified and solved. There's a WIP pr here https://github.com/lucagez/slow-json-stringify/pull/34/files that still needs a bit of polishing.

Mainly the relevant changes are included in this file https://github.com/lucagez/slow-json-stringify/pull/34/files#diff-45da6211f4fbb464f0d9881207fd1363b9822736cdc71d853eda58e4ad490d2aR1 . I removed the flag that was causing the undesired mutation.

RichardWright commented 2 years ago

Why is this closed? The pr is still open and this is causing issues for us.