Does raise the question whether there should be an OutputFormatting option for stableKeyOrder
or similar. I feel an SE proposal coming on…
Easier said than done, I fear. JSONEncoder
and JSONDecoder
use Foundation NSDictionary
under the hood (at least for the open source version of Foundation). That would need to be swapped out for an ordered hash of some kind in order to facilitate the new feature and even then I am not sure off the top of my head whether JSONSerialization
would play nicely with the goal of stable ordering.
Is stable ordering even a feature of JSON in other implementations, or have I wandered off into the bushes here?
Yeah, it’s pretty common in my experience for ordering of hashes to remain stable while parsing JSON. FineJSON
is an example of a Swift Encoder/Decoder that retains ordering. The popular Yams
Encoder/Decoder for YAML also retains ordering. So, given an implementation like OpenAPIKit that is capable of holding onto that ordering, we do already have some options for encoding and decoding. Of course, if ordering is not important for a particular use-case, the Foundation’s JSONEncoder and JSONDecoder are great!
Then gosh, if it's common elsewhere, Foundation's implementation should probably be doing the correct thing here, too. I've hit it in a few places when working with things like the PropertyListSerialization
classes, where order can be important but is lost anytime you run through one of the serializers.
Anyway, sorry to derail the OpenAPIKit thread - back on topic - I'm super excited about this work, too!
Proposal thread created here.