Skip to content
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

[SR-6163] JSONDecoder cannot decode RFC 7159 JSON #4402

Closed
swift-ci opened this issue Oct 16, 2017 · 11 comments
Closed

[SR-6163] JSONDecoder cannot decode RFC 7159 JSON #4402

swift-ci opened this issue Oct 16, 2017 · 11 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-6163
Radar rdar://problem/38549586
Original Reporter mikkelam (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 23
Component/s Foundation
Labels Bug, Codable
Assignee @itaiferber
Priority Medium

md5: 7ea104c1b9f4df36ef6d428fc2c7cfd0

is duplicated by:

  • SR-7213 Allow JSONEncoder to Encode Top-Level Fragments

relates to:

  • SR-12275 JSONEncoder on Linux can't encode number JSON fragments

Issue Description:

For example

22

Is valid RFC 7159 JSON. This can be parsed using the JSONSerializer like so

let json = "22".data(using: .utf8)!

if let value = (try? JSONSerialization.jsonObject(with: json, options: .allowFragments)) as? Int {
    print(value) // 22
}

But there is no way to use the allowFragments option for JSONDecoder.

@itaiferber
Copy link
Contributor

@swift-ci Create

@itaiferber
Copy link
Contributor

As is discussed in https://forums.swift.org/t/top-level-t-self-encoded-as-number-json-fragment/11001, if we choose to, we can resolve this either on the JSONEncoder/JSONDecoder side, or on the JSONSerialization side (for encoding).

@swift-ci
Copy link
Contributor Author

swift-ci commented Apr 5, 2019

Comment by Vasily Anisimov (JIRA)

Hello! Any progress on this issue?

@itaiferber
Copy link
Contributor

Yes, work has been ongoing internally, but I can't speak to the specifics here.

@benrimmington
Copy link
Contributor

This appears to be resolved with the latest macOS and iOS.

JSONEncoder and JSONDecoder now support top-level "primitive" types (strings, numbers, booleans) by default.

But I can't see any relevant changes in the implementation or unit tests.

@swift-ci
Copy link
Contributor Author

Comment by Andrey Yakushev (JIRA)

Looks like @benrimmington is right. Rechecked right now: tests are successful with Xcode 11 on iOS 13.1 and fail on iOS 10.3.1.

@swift-ci
Copy link
Contributor Author

Comment by Chris Ballinger (JIRA)

As @benrimmington said, it looks like the open source release of Swift's Darwin JSONEncoder/JSONDecoder has not been updated to reflect this change, and doesn't match the stdlib that is being shipped with iOS 13:

I guess Apple is shipping a private fork and forgot to upstream the fix?

@swift-ci
Copy link
Contributor Author

Comment by Chris Ballinger (JIRA)

I made a small PR that updates the open source stdlib to match the behavior of the one shipping with iOS 13: apple/swift#28818

@benrimmington
Copy link
Contributor

Darwin:

Other platforms:

@swift-ci
Copy link
Contributor Author

Comment by Diogo Tridapalli (JIRA)

It's fixed on Xcode 11.4.

@benrimmington
Copy link
Contributor

SR-12275 has been resolved.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants