Skip to content

[SR-7017] JSONDecoder will decode booleans as numbers #3736

@lilyball

Description

@lilyball
mannequin
Previous ID SR-7017
Radar None
Original Reporter @lilyball
Type Bug
Status Closed
Resolution Done
Environment

Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9

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

md5: da644e5cdf59c1c9ee3e87e02c010d6a

Issue Description:

JSONDecoder is perfectly happy to convert a boolean to a number. This is surprising.

Example:

import Foundation

struct Foo: Decodable {
    let value: Int
}

let jsonData = "{ \"value\": true }".data(using: .utf8)!
let foo = try! JSONDecoder().decode(Foo.self, from: jsonData)
print(foo.value) // prints: 1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions