ljharb / json-stable-stringify

MIT License
52 stars 11 forks source link

not sorting values in an array if the array is a value of a key. #12

Open jerrywoo96 opened 8 months ago

jerrywoo96 commented 8 months ago

given:

let data = {
  "a": {
    "a": ["c", "b", "a"]
  }
}

running stringify(data, { space: 2 }); does not sort the array. instead, it was unsorted.

Result:

{
  "a": {
    "a": ["c", "b", "a"]
  }
}

Expected:

{
  "a": {
    "a": ["a", "b", "c"]
  }
}
net commented 8 months ago

Arrays imply a specific ordering, so most use cases would require that arrays do not get sorted as that would change the semantics of the object.

ljharb commented 8 months ago

Indeed; I'm not sure why you'd expect it to become sorted.

jerrywoo96 commented 8 months ago

if the order is not a concern, then sorting it makes it easier to find when looking at a json file? how about adding as an option? like {spaces: 2, sortArray: true}?

ljharb commented 8 months ago

If you want it sorted, it should be sorted before trying to stringify it - it doesn't really make sense to me to have a serialization option that fundamentally alters the data.