4 ms·
when you are diffing the serialized output?
by jwkane 7y ago
when you are diffing the serialized output?
- philwelch 7y agoFrom what I recall of the JSON standard itself, there's no guarantee about key ordering being significant. If you're diffing serialized output to compare two JSON objects you need to be serializing it in a consistent format, otherwise even whitespace is going to throw you off.
- jwkane 7y agoIt's significant to a human that wants to know what has changed.
- philwelch 7y agoIf a human is inspecting serialized JSON using pen and paper, the human is presumably clever enough to match up key for key regardless of ordering. If the human is using a computer to compare two JSON payloads (as the use of a diffing algorithm suggests), the human and computer should be clever enough as a team to realize that they could just deserialize and reserialize each JSON payload such that the keys were lexicographically sorted and the data was pretty-printed in the exact same way before running it through the diffing algorithm. `jq -Sc` would do the trick.
- zo1 7y agoMost diff programs don't have what you describe. And in a lot of cases you don't have the easy ability to "do stuff" before running the input through a diffing algorithm.
- philwelch 7y ago> Most diff programs don't have what you describe. That’s why pipes were invented. > And in a lot of cases you don't have the easy ability to "do stuff" before running the input through a diffing algorithm. Well, in that case you’re not going to be able to meaningfully compare two JSON payloads because neither order nor white space have any semantic meaning in JSON. I’m really curious what you’re talking about though, since if you’re working on the shell you can easily use jq to do as I describe.
- deleted 7y ago[deleted]
- anonymoushn 7y agoSo you can sort the keys at serialization time rather than paying a performance penalty all the time?