Skip to content

[SR-13927] Unusual error with JSONDecoder #56325

@swift-ci

Description

@swift-ci
Previous ID SR-13927
Radar rdar://problem/71992646
Original Reporter Hackice (JIRA User)
Type Bug
Status Reopened
Resolution

Attachment: Download

Environment

Xcode: Version 12.2 (12B45b)

macOS: 11.0.1 (20B29)

Mac: Mac mini (2018)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee kylemacomber (JIRA)
Priority Medium

md5: 8f09c902dd67e14883756601a56985b9

Issue Description:

When I used JSONDecoder to parse the Codable model, the compiler gave some meaningless error messages and no exception was thrown:

Sometimes the compiler will not display error in this case, but throws the exception "The given data did not contain a top-level value.", https://github.com/apple/swift/blob/main/stdlib/public/Darwin/Foundation/JSONEncoder.swift line: 1210, but obviously this is also meaningless, because my Model is just a very simple one-level struct.

Interestingly, the decode was successful after I commented out one of the attributes:

This is my json string:

{\"notification_id\":\"1107227364\",\"client_id\":\"14064\",\"user_id\":\"11857799\",\"create_user_id\":\"0\",\"refer_id\":\"0\",\"read_flag\":\"0\",\"pin_flag\":\"1\",\"enable_flag\":\"1\",\"module\":\"system\",\"title\":\"\"}

And Codable model:

struct Model: Codable {
    let notificationID, clientID, userID, createUserID: String?
    let referID, readFlag, pinFlag, enableFlag: String?
    let module: String?
    
    enum CodingKeys: String, CodingKey {
        case notificationID = "notification_id"
        case clientID = "client_id"
        case userID = "user_id"
        case createUserID = "create_user_id"
        case referID = "refer_id"
        case readFlag = "read_flag"
        case pinFlag = "pin_flag"
        case enableFlag = "enable_flag"
        case module
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions