0

Getting exception *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)' while trying to encode this Swift object to JSON. All non-optional members, objects Codable. What is the right way to encode or should use some 3rd party library?

struct MediaItem: Codable {
    var key: String = ""
    var filename: String = ""
}

struct NoteTask: Codable {
    var id: String = ""
    var notes: String = ""
    var mediaList: [MediaItem] = []
}

static func addTask(task: NoteTask, callback: @escaping TaskAPICallback) {
    let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration)

    let url = URL(string: postUrl)
    var request : URLRequest = URLRequest(url: url!)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    let params: [String: Codable?] = [
        "email": task.id,
        "notes": task.notes,
        "fileList": task.fileList
    ]
    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])
    } catch {
        DispatchQueue.main.async {
            callback(false)
        }
        return
    }

    ...
}
4
  • Please add JSON string Commented Dec 2, 2019 at 4:08
  • An obvious improvement would be to print the error in the catch clause. Commented Dec 2, 2019 at 9:19
  • @JoakimDanielson Except that the catch clause doesn't get triggered here. An Objective-C exception is being thrown. Commented Dec 2, 2019 at 16:52
  • 1
    @AndrewMadsen That was more a general comment/recommendation from my side Commented Dec 2, 2019 at 17:02

1 Answer 1

1

The issue is that you're using JSONSerialization instead of JSONEncoder. JSONSerialization is the older, Foundation/Objective-C way of writing objects to JSON. It will only work with Foundation objects (see the documentation for a complete list).

Instead, you should use JSONEncoder. The tricky part is that JSONEncoder can't directly encode a Dictionary without some work on your part. There are a few ways to solve this, but if this is the only JSON format you're going to use, I'd probably just create custom keys for your structs using CodingKeys.

struct MediaItem: Codable {
    var key: String = ""
    var filename: String = ""
}

struct NoteTask: Codable {
    var id: String = ""
    var notes: String = ""
    var mediaList: [MediaItem] = []

    enum CodingKeys: String, CodingKey {
        case id = "email"
        case notes = "notes"
        case mediaList = "fileList"
    }
}

static func addTask(task: NoteTask, callback: @escaping TaskAPICallback) {
    let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration)

    let url = URL(string: postUrl)
    var request : URLRequest = URLRequest(url: url!)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    do {
        request.httpBody = try JSONEncoder().encode(task)
    } catch {
        DispatchQueue.main.async {
            callback(false)
        }
        return
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.