Skip to content

Support JSONSerialization of more number types #650

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

Merged
merged 1 commit into from
Sep 28, 2016

Conversation

seabaylea
Copy link
Contributor

Currently the JSON Serializer only handles the "common" numbers types: Int, Double and NSNumber.

Trying to serialise the other number types: UInt, Int8, UInt8, etc as well as Float results in an Error.

This PR adds support for the wider set of standard types.

@phausler
Copy link
Contributor

This is incompatible with the Darwin version:

let json = ["foo" : UInt8(4)]
let data = try! JSONSerialization.data(withJSONObject: json, options: [])

print("\(String(data: data, encoding: .utf8)!)")

fails with the exception:

2016-09-21 12:34:16.233614 JSONTest[50614:1325824] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff81f4442b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff9689ca2a objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff81fc2c05 +[NSException raise:format:] + 197
    3   Foundation                          0x00007fff838e973a _writeJSONValue + 698
    4   Foundation                          0x00007fff83946636 ___writeJSONObject_block_invoke + 226
    5   CoreFoundation                      0x00007fff81eb863e __NSDictionaryEnumerate + 862
    6   Foundation                          0x00007fff8394642a _writeJSONObject + 430
    7   Foundation                          0x00007fff838e965f _writeJSONValue + 479
    8   Foundation                          0x00007fff838e942c -[_NSJSONWriter dataWithRootObject:options:error:] + 124
    9   Foundation                          0x00
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

@parkera
Copy link
Contributor

parkera commented Sep 21, 2016

Why does Int8 bridge to _SwiftValue instead of NSNumber in the first place? I thought this was something they were looking into fixing.

@phausler
Copy link
Contributor

The changes for numeric bridging have not yet landed, but to replicate that behavior then we should funnel the top level input to _SwiftValue.store() and traverse references instead of structures.

@seabaylea
Copy link
Contributor Author

I've updated to use _SwiftValue.store(obj) for the various number types, which matches the behaviour on Darwin.

@phausler
Copy link
Contributor

@swift-ci please test

@parkera
Copy link
Contributor

parkera commented Sep 28, 2016

@phausler you ok with this? should we merge now?

@phausler
Copy link
Contributor

Yea, this seems like a good first step to gaining more pairity

@phausler phausler merged commit e05884b into swiftlang:master Sep 28, 2016
pushkarnk pushed a commit to pushkarnk/swift-corelibs-foundation that referenced this pull request Oct 7, 2016
parkera pushed a commit that referenced this pull request Oct 7, 2016
* [SR-2694] Cannot serialize JSON object with array (#648)

* Support JSONSerialization of more number types (#650)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants