← Back to context

Comment by helsinki

3 months ago

”The response should be a JSON object mapping Emoji (as Strings) to their count (as Numbers)”

This means ordering semantics are lost.

But JSON mappings are ordered. The thing producing/consuming them might choose to map them to an unordered mapping but inherent to them being serialized is you get an order for free.

  • The JSON specification describes objects as unordered. Which means any standards compliant JSON encoders or decoders can and will produce maps in different orders even when the same object is passed through twice.

    It’s also worth noting that quite a few languages don’t guarantee ordered maps either.

    If you want an ordered map then you really need a key/value map inside an array:

       [
         {
           Key: “:thumb:”,
           Value: 4
         },
         {
           Key: “:smile:”,
           Value: 1
         }
       ]
    
    

    Though in this specific instance, you’d be better off with more specific key names like “emoji” and “count” (respectively).

    Edit: HN stripped the emojis from my comment so I added ASCII placeholder strings into the example to illustrate the same point.

  • Not by the spec. Some implementations may preserve the order, but the JSON spec doesn’t guarantee that, so you cannot rely on it.