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-2441] When compiling with Swift 3, serializing with JSONSerialization translates boolean (Bool) values to 1 or 0 instead of true or false #4335

Closed
swift-ci opened this issue Aug 20, 2016 · 0 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-2441
Radar rdar://problem/27937005
Original Reporter jbrayton (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 8.0 beta 6

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, 3.0Regression
Assignee None
Priority Medium

md5: cec5813b8a6e14090a7b6dc0702da046

duplicates:

  • SR-2381 Swift 3 (Xcode 8 beta 6) archives booleans in XML plist as integers

Issue Description:

Start with this dictionary:

["first": true, "second": false]

When using Swift 2.3, it will be serialized as as:

{"first":true,"second":false}

When using Swift 3.0, JSONSerialization serializes the same dictionary as:

{"first":1,"second":0}

Some web service APIs, including the Gmail API, will reject 1 or 0 when expecting a boolean value.


This unit test will pass in a Swift 2.3 unit test:

func testJson() {
    
    let inputJsonDictionary = ["first": true, "second": false]
    
    guard let data = try? NSJSONSerialization.dataWithJSONObject(inputJsonDictionary, options: []) else {
        XCTFail()
        return
    }
    
    guard let jsonString = String(data: data, encoding: NSUTF8StringEncoding) else {
        XCTFail()
        return
    }
    print(jsonString)
    XCTAssertTrue(jsonString == "{\"first\":true,\"second\":false}" || jsonString == "{\"second\":false},\"first\":true")
}

This analogous Swift 3.0 unit test will fail:

func testJson() {
    
    let inputJsonDictionary = ["first": true, "second": false]
    
    guard let data = try? JSONSerialization.data(withJSONObject: inputJsonDictionary, options: []) else {
        XCTFail()
        return
    }
    
    guard let jsonString = String(data: data, encoding: String.Encoding.utf8) else {
        XCTFail()
        return
    }
    print(jsonString)
    XCTAssertTrue(jsonString == "{\"first\":true,\"second\":false}" || jsonString == "{\"second\":false},\"first\":true")
}

It fails because the boolean failures are serialized to 1 and 0 instead of true and false.


I encountered this using Xcode 8.0 beta 6 while developing an iOS app. I can reproduce the bug both on the simulator and on an iPod Touch running iOS 10 beta 7 (14A5345a). I am not certain, but I believe this is a change in behavior since Xcode 8.0 beta 5.


I think this is an important bug. Many app developers write apps that talk to web services outside of their control.

Thank you for taking a look.

@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

1 participant