Handling duplicate keys in JSON

I’m trying to see any reasonable way to handle duplicate keys in JSON. It was surprising to me, but apparently the JSON specification doesn’t disallow this. And certain tools (e.g. HTTPie) may encounter such JSON that they can’t reasonably ignore. (For an example scenario, see this HTTPie feature request I raised.)

It is easy to parse. We can use the multidict package and pass object_pairs_hook=MultiDict.

However, I can’t see any reasonable way to serialize it. We can’t implement JSONEncoder.default because it has to return a supported basic type, and converting to a dict would obviously throw away the duplicate keys. And there appears to be strong resistance* to making the json module support Mapping in general in place of dict.

It looks to me like there’s no reasonable way to successfully format a JSON object with duplicate keys without writing a complete JSON formatter from scratch. Am I wrong?

* - Apparently I am not allowed to include more than two links. This was intended to be a link to the conversation “Json does not support Mapping and MutableMapping” on this site, with path t/json-does-not-support-mapping-and-mutablemapping/2829