ibmruntimes / yieldable-json

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

When space is set identation does not work correctly for nested objects and arrays in stringifyAsync. #32

Open ErgerSusha opened 3 years ago

ErgerSusha commented 3 years ago

If you set space parameter and call stringifyAsync nested object and arrays are not indented properly. Here is a test suite, that confirms such behavior:

'use strict';

const yj = require('../index');
const tap = require('tap');

const obj = {
  name: 'Jacqueline Poole',
  gender: 'female',
  age: {
    value: 40
  },
  agesArray: [
    10,
    20,
    30
  ]
};

yj.stringifyAsync(obj, null, '  ', (err, str) => {
  if (!err)
    tap.equal(str, '{\n  "name": "Jacqueline Poole",\n  "gender": "female",\n  "age": ' +
      '{\n    "value": 40\n  },\n  "agesArray": [\n    10,\n    20,\n    30\n  ]\n}');
  else
    tap.fail(err);
});

Output:

not ok 1 - should be equal
  ---
  found: |-
    {
      "name": "Jacqueline Poole",
      "gender": "female",
      "age": {
      "value": 40
    },
      "agesArray": [
      10,
      20,
      30
    ]
    }
  wanted: |-
    {
      "name": "Jacqueline Poole",
      "gender": "female",
      "age": {
        "value": 40
      },
      "agesArray": [
        10,
        20,
        30
      ]
    }
gireeshpunathil commented 3 years ago

thanks @ErgerSusha for reporting this bug. I will look into it; and meanwhile feel free to come up with a PR, if you have ideas on how to fix it!