[swift-evolution] [Proposal] Foundation Swift Encoders
anthony.parker at apple.com
Mon Jun 12 11:09:22 CDT 2017
> On Jun 12, 2017, at 8:32 AM, Ben Rimmington via swift-evolution <swift-evolution at swift.org> wrote:
> The new `JSONSerialization.WritingOptions.sortedKeys` option was mentioned at WWDC.
> "What's New in Cocoa" [33:03 ... 33:33]
> When the `sortedKeys` option is available, can it be used **by default** in the `JSONEncoder`?
> Otherwise, can the `JSONEncoder.OutputFormatting` enum have extra cases?
We intend to make this option available to JSONEncoder as well, but since the underlying feature was tied to the OS release we couldn’t pre-announce it.
One nit pick: I don’t think default is the right answer, because sorting has a performance cost and we think it’s better to opt-in to the more expensive operations.
> case compact
> case compactWithSortedKeys
> case prettyPrinted
> case prettyPrintedWithSortedKeys
> -- Ben
>> On 16 Mar 2017, at 22:47, Itai Ferber <iferber at apple.com> wrote:
>> The sorted order of keys is an implementation detail of PropertyListSerialization which JSONSerialization does not share.
>> This would require a change in JSONSerialization, which is possible, but out of scope for this work specifically. It would have to go through review.
>> [FWIW, the key ordering isn’t changing at random; JSONSerialization writes keys in the order that it receives them (sorted in the order that NSDictionary stores them, based on hash). If you want this to not interfere with your Git repo (on a more immediate timescale), you can probably do something like adding a pre-commit Git hook to lint the file as needed. But I digress, this is totally off-topic.]
>>> On 16 Mar 2017, at 14:57, Ben Rimmington wrote:
>>> Sorry, I actually meant that keys would be ordered **alphabetically** (as they are with PropertyListSerialization).
>>> I'm storing output from JSONSerialization in a git repository, and when I add/remove a single key-value the entire file changes, due to keys being reordered (seemingly at random).
>>> -- Ben
>>>> Itai Ferber wrote:
>>>> By stable ordering, you mean dictionaries with keys being output in the order that they were encoded?
>>>> Potentially, but this would require additional work as neither Swift dictionaries nor NSMutableDictionary support this (reasonable seeing as dictionaries are by definition not sorted), and the current implementation uses JSONSerialization as the concrete serializer. Certainly not impossible, though.
>>>>> On 16 Mar 2017, at 8:49, Ben Rimmington wrote:
>>>>> Could the `JSONEncoder.OutputFormatting.prettyPrinted` case include the **stable ordering** of keys?
>>>>> -- Ben
> swift-evolution mailing list
> swift-evolution at swift.org
More information about the swift-evolution