by danhau · 1y ago · 0 comments
{ “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.
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.
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.
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.