5 ms·
What slightly annoys me about JSON is that the order of object properties is defined to be irrelevant. { “hello“: 123, “world“: “foo“ } is the same as { “wor
by danhau 2y ago
What slightly annoys me about JSON is that the order of object properties is defined to be irrelevant.
{ “hello“: 123, “world“: “foo“ }
is the same as
{ “world“: “foo“, “hello“: 123 }
If these two were semantically different, writing deserializer would be easier and more efficient, since you can simply expect the next tokens to represent the currently visited class member, or error.
Otherwise you need to construct an object tree and look up its properties by name / hash.
- nly 2y agoApache Avro's C++ deserializer for the JSON serialization used to expect exactly that: keys to be in schema order. Trust me you don't want this. Usually the reason you want to use JSON in the first place is you want to support third party data access. Even if you constrained the field order you'd still have to deal with absent fields.
- jasonthorsness 2y agoNotably this is one of the main differences between JSON and the MongoDB BSON format. Though some client libraries just treat it as JSON and don’t preserve the order.
- inbx0 2y agoDoes the JSON spec actually say that those objects should be "equal", or does it just leave that detail to implementations? In JavaScript at least, those two are not exactly "the same", in the sense that you can observe the difference if you want to. If you parse those JSON strings and then iterate the keys (e.g. with Object.keys), the ordering will be different.
- reichstein 2y agoThe JSON spec only defines the JSON text format. It doesn't say what the text means. There are obvious interpretations, but every program that reads or writes JSON can decide what it does with it. On the other hand, the thing that makes JSON actually useful is the interoperability, that JSON written by one program, on one platform, can be read by another preterm on another platform. Those programs have to agree on a protocol, what the JSON text must satisfy and what it means. It's usually not considered valuable to require object properties to be in a specific order, so they don't. But they could.