Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'

I am getting the following error when I try to encode an object.

'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'

The definition of this object is as under:-

public struct Item: Codable {
    public var _id: Int
    public var name: String
    public var price: Float

    public init(_id: Int, name: String, price: Float) {
        self._id = _id
        self.name = name
        self.price = price
    }    

    public enum CodingKeys: String, CodingKey {
        case _id = "id"
        case name
        case price
    }
}

And I am trying to encode it by:

public func createDictionaryRequestForAddingItems(item : Item)->Data{
    let dictRequest = ["item":item];
    let dataRequest = try! JSONSerialization.data(withJSONObject: dictRequest, options: []);
    return dataRequest;
}

If instead of an item object I use a simple object like a String or an Int directly, then it all works, but when the request needs an Item object (which IS-A Codable instance) then it gives the above error.

JSONSerialization.isValidJSONObject(item) always gives false, even for the requests that getting properly getting encoded.

like image 262
Ankur Israni Avatar asked Feb 27 '26 21:02

Ankur Israni


1 Answers

The problem is that you are trying to combine two types of JSON encoding. JSONSerialization and Codable. JSONSerialization has nothing to do with Codable.

Actually, you want something like this:

public func createDictionaryRequestForAddingItems(item: Item) -> Data {
   let dictRequest = ["item": item]
   let dataRequest = try! JSONEncoder().encode(dictRequest)
   return dataRequest
}

JSONSerialization can encode only the following types: Array, Dictionary, String, Bool and numeric types (e.g. Double, Int).

like image 161
Sulthan Avatar answered Mar 01 '26 11:03

Sulthan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!