Property Iteration in JSON serialization

David-Sarah Hopwood david-sarah at jacaranda.org
Tue Oct 13 22:07:05 PDT 2009


Brian Kardell wrote:
> So - keeping in mind that the instance and the serialization are
> separate things - what if I would like to order my serialization keys
> (property names) in natural order?  It has no impact on the parsing or
> internal representation, but imposing a known order on the
> serialization, even a trivial one, makes things easier to find and
> documentation easier to understand.  We've implemented this in a few
> local serializers in multiple languages and, since during
> serialization we are writing them out by iterating the keys, it's
> actually quite trivial to merely call sort() on the keys before
> iteration.  Likewise, since this only has to be done once per type -
> not once per instance, there is very, very minimal overhead (you are
> generally sorting less than 20 keys regardless of the number of
> instances).

Well, JS doesn't have types in that sense. So, unless an implementation
were to exploit any internal optimizations it has for recognizing objects
of the same "shape" [*], it would indeed have to sort the keys of every
instance.


[*] Do any common implementations actually do that, other than for
    packed arrays?

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the es-discuss mailing list