-
Notifications
You must be signed in to change notification settings - Fork 1.2k
JSONSerialization: add WritingOptions.sortedKeys #1102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
By the way, @parkera: is there a reason that |
@swift-ci please test |
Performance measurements of my first implementation showed ~8% cost in the default non-sorted case compared to the previous implementation, which is obviously not acceptable. I've pushed another commit which eliminates this cost. It does make the code slightly more complex (there are now separate @pushkarnk - please can you re-run the CI? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, save for the specifics of the string comparison
Foundation/NSJSONSerialization.swift
Outdated
let b = b.key as? String else { | ||
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "NSDictionary key must be NSString"]) | ||
} | ||
return a < b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will apply the exact same sorting strategy as we do on Darwin platforms. We are currently doing the equivalent of:
NSString * const key1 = ...
NSString * const key2 = ...
const NSRange range = NSMakeRange(0, key1.length);
NSLocale * const locale = [NSLocale systemLocale];
return [key1 compare:key2 options:NSNumericSearch | NSCaseInsensitiveSearch | NSForcedOrderingSearch range:range locale:locale];
Especially for archiving purposes, we should try to be consistent here.
@ianpartridge The "Type Properties" vs. "Constants" thing looks like an artifact of the documentation generation system. Likely worth filing a Radar about that |
Foundation/NSJSONSerialization.swift
Outdated
let range = a.startIndex ..< a.endIndex | ||
let locale = NSLocale.systemLocale() | ||
|
||
return a.compare(b, options: options, range: range, locale: locale) == .orderedAscending |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @itaiferber. Does this look right now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@swift-ci Please test |
@swift-ci please test and merge |
Build failed with
I'll take a look tomorrow. |
2a3f331
to
d067ae2
Compare
It looks like SE-0180 means that the compiler now infers As @phausler - can you try again please? |
@swift-ci please test and merge |
@ianpartridge The docs are updated independently of our headers, so we'll need a bug to fix those (Separate from all of this stuff of course). |
Thanks @parkera - I've opened a Radar for the doc issue. |
@ianpartridge What's the Radar number? Want to make sure it gets to the right place. :) |
33233023 |
@ianpartridge Thanks! |
This implements the new
sortedKeys
constant inJSONSerialization.WritingOptions
and adds some tests. This constant is documented at https://developer.apple.com/documentation/foundation/jsonserialization.writingoptions - it is new in the macOS 10.13 SDK.This option is used by the new Swift 4
JSONEncoder
which has been recently implemented.Note: I am still doing performance testing on this to check it doesn't regress the non-sorted case, but I'm opening the PR for review and comment now.
CC: @bubski @itaiferber