-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[SR-2151]NSJSONSerialization.data produces illegal JSON code #540
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
I think the intent here is correct, but I would rather factor the serialization logic into NSJSON instead of into NSNumber. There are several kinds of serialization (e.g., NSCoding), so putting the JSON-specific logic in NSNumber itself seems like unnecessary coupling. That also means we could create the number formatter once and reuse it through the entire serialization instead of creating one over and over again with the same settings, which could be very expensive. (Probably we don't even need to use an actual formatter here at all, but it's fine for now. NSJSONSerialization in Foundation uses |
2839a10
to
1b8d45e
Compare
@parkera thanks for the review! I totally agree in hindsight it is very obvious of the coupling. I updated the pr, please kindly take a look when you get a chance. |
@swift-ci please test linux |
NSJSONSerialization.data(withJSONObject:options) produces illegal JSON code https://bugs.swift.org/browse/SR-2151
1. moved format logic out of NSNumber and in to NSJSonSerialization 2. lazy load the formatter to be instantiated if needed 3. create a single format string to work with all formats since we are lazy loading a single formatter
update to latest master
Looks like this may need some fixes for Linux (where bridging is unfortunately still a bit different):
|
@parkera I updated and removed the casting and also updated to the latest master |
@swift-ci please test linux |
@parkera hi, any update on this pr is there any thing i can help with? |
Should be good to go |
Oops - ran afoul of our other changes. I need to revert. |
|
@DaveLiu888 can you rebase on top of master and re-submit the PR? |
private lazy var _numberformatter: CFNumberFormatter = { | ||
let formatter: CFNumberFormatter | ||
formatter = CFNumberFormatterCreate(nil, CFLocaleCopyCurrent(), kCFNumberFormatterNoStyle) | ||
CFNumberFormatterSetProperty(formatter, kCFNumberFormatterMaxFractionDigits, 15._bridgeToObject()) |
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.
The syntax for bridging has changed; I would suggest to actually just create a NSNumber via NSNumber(value: 15)
[pull] swiftwasm from main
What's in this pull request?
Resolved bug number:SR-2151 (https://bugs.swift.org/browse/SR-2151)
Implementation
Tests (note all tests are done on NSJSONSerialization due to access lvl protection )
Problem :
NSJSONSerialization.data produces illegal JSON code
Cause :
NSJSONSerialization
func serializeNumber(_ num: NSNumber) throws
uses NSNumber
open override var description: String
which callsopen func description(withLocale locale: Locale?) -> String
with a nil locale.The nil locale case is handled by setting the format style to kCFNumberFormatterNoStyle & kCFNumberFormatterMaxFractionDigits
the leading zero does not show up which results in the incorrect format.
["foo": 0.123]
should be
but is
Proposed Solution:
NSJSONSerialization func serializeNumber(_ num: NSNumber)
stop callingdescription(withLocale locale: Locale?) -> String
as locale imho should not have any determination for JSON serializationinternal var serializationString: String
to return the proper formatted stringcheck whether or not the underlying store is a _cfObject is a fractionDigit type which need leading zero padding and conditionally use a custom formatter string
"0.###############"
.Reason for using custom format string instead of existing style was largely due to the documentation and programing guide suggestions on the fact exact formats of this sort should be decoupled from Locale
https://developer.apple.com/library/prerelease/content/documentation/CoreFoundation/Conceptual/CFDataFormatting/Articles/dfCreatingCFNumberFormatters.html
https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFNumberFormatterRef/#//apple_ref/doc/constant_group/Number_Formatter_Styles