lionheart / openradar-mirror

A mirror of radars pulled from http://openradar.me/.
245 stars 17 forks source link

27937005: When compiling with Swift 3, serializing with JSONSerialization translates boolean (Bool) values to 1 or 0 instead of true or false #15671

Open openradar-mirror opened 8 years ago

openradar-mirror commented 8 years ago

Description

When using Swift 2.3, NSJSONSerialization serializes the dictionary ["first": true, "second": false] 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.

Steps to reproduce:

  1. In a Swift 2.3 project, run this unit test. It will pass.

    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")

    }

  2. In a Swift 3.0 project, run this unit test

    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")

    }

Expected Results:

I would expect the Swift 3.0 unit test to pass.

Actual Results:

The Swift 3.0 unit test fails. The value of jsonString is:

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

Configuration:

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.

Additional Notes:

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.

Product Version: Xcode Version 8.0 beta 6 (8S201h) on Sierra (macOs 10.12 Beta (16A294a)) Created: 2016-08-20T17:46:12.867340 Originated: 2016-08-20T00:00:00 Open Radar Link: http://www.openradar.me/27937005

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660

openradar-mirror commented 8 years ago

Modified: 2016-08-20T20:14:30.469660