tristanhimmelman/AlamofireObjectMapper

I always got 'objectmapper failed to serialize response' when parsing error response

Closed this issue · 9 comments

import Foundation
import ObjectMapper
import AlamofireObjectMapper

private let NameKey = "name"
private let DescriptionKey = "description"
private let CoverURLKey = "cover_url"
private let MembersAmountKey = "members_count"
private let SportNameKey = "sport_name"
private let PagePolicyKey = "policy"

class Tribe: Mappable {
    var name: String?
    var desc: String?
    var coverURL: NSURL?
    var membersAmount: Int?
    var sportName: String?
    var policy: ObjectPolicy?
    required init?(_ map: Map){

    }

    func mapping(map: Map) {
        name <- map[NameKey]
        desc <- map[DescriptionKey]
        coverURL <- (map[CoverURLKey], URLTransform())
        membersAmount <- map[MembersAmountKey]
        sportName <- map[SportNameKey]
        policy <- map[PagePolicyKey]
    }
}

this is my class and I just used simply -responseObject object method in APIClient
Thank in advance.

Hello dude!, The same thing happening to me as well. When I run your code for single object it is giving
Error Domain=com.alamofire.error Code=-6004 "ObjectMapper failed to serialize response." UserInfo={NSLocalizedFailureReason=ObjectMapper failed to serialize response.}

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
Alamofire.request(.GET, URL).responseObject("data") { (response: Response<WeatherResponse, NSError>) in

let weatherResponse = response.result.value
print(weatherResponse?.location)

if let threeDayForecast = weatherResponse?.threeDayForecast {
    for forecast in threeDayForecast {
        print(forecast.day)
        print(forecast.temperature)           
    }
}

}

So please help me out of from this.

@JayachandraA bad news, I dogged into the code and this library have just written an extension with own serializer and fixed error message for us.

I think the proper way is clone this repository and edit the serializer yourself.

ObjectMapper doesn't yet support specific error messaging when parsing errors occur. This is why this extension simply reports the same error.

@JayachandraA, looking at the sample you are trying to run, I can see the error by looking at the JSON response linked. You are trying to deserialize using data as a keypath but data isn't in the JSON response. Try again but omit the keypath.

@tristanhimmelman @macbaszii thanks guys for your response. finally I found the solution.

@JayachandraA What was your solution ? Could you share to us ?

@macbaszii I just assigned empty values when I'm declaring optional properties
like this
var name: String? = ""
var desc: String? = ""
var coverURL: NSURL? = nil
var membersAmount: Int? = 0
var sportName: String? = ""

@tristanhimmelman Hello I am having the same issue. I'll really appreciate if one can help me immediately, i really need a quick response.
Here is an example returning json
->
[
{
"eventdescription": "event2",
"participantid": "{7,2,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL}",
"mainphotoid": 3,
"photosid": "{7,3,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL}",
"ownerid": 2,
"invitationlist": "{7,4,6,8,NULL,NULL,NULL,NULL,NULL,NULL}",
"privilligeId": "{7,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}",
"eventid": 2,
"eventdate": "2016-10-05 00:00:00",
"eventitle": "Come on first2"
},
{
"eventdescription": "asd",
"participantid": "{9,1,2,3,NULL,NULL,NULL,NULL,NULL,NULL}",
"mainphotoid": 2,
"photosid": "{1,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}",
"ownerid": 2,
"invitationlist": "{2,4,7,8,NULL,NULL,NULL,NULL,NULL,NULL}",
"privilligeId": "{1,2,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL}",
"eventid": 3,
"eventdate": "2016-10-05 00:00:00",
"eventitle": "asdas"
},
{
"eventdescription": "UpdateasddddLet's starts",
"participantid": "{9,1,2,3,NULL,NULL,NULL,NULL,NULL,NULL}",
"mainphotoid": 2,
"photosid": "{1,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}",
"ownerid": 3,
"invitationlist": "{2,4,7,8,NULL,NULL,NULL,NULL,NULL,NULL}",
"privilligeId": "{1,2,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL}",
"eventid": 4,
"eventdate": "2016-10-05 00:00:00",
"eventitle": "asdasd"
},
null,
null,
null,
null,
null,
null,
null
]
Here is my class to create mappable
->
screen shot 2016-06-15 at 03 52 21

I have set the initial values to nil regarding @JayachandraA 's solution. before setting them to nil, i gave them values of "" (if they are string), 0(if they are int), [""](string array), [0](int array)

This is my request
->

screen shot 2016-06-15 at 03 56 02

This is what i get when i print the responseString

Success: true
Response String: Optional("[{"eventdescription":"event2","participantid":"{7,2,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL}","mainphotoid":3,"photosid":"{7,3,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL}","ownerid":2,"invitationlist":"{7,4,6,8,NULL,NULL,NULL,NULL,NULL,NULL}","privilligeId":"{7,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}","eventid":2,"eventdate":"2016-10-05 00:00:00","eventitle":"Come on first2"},{"eventdescription":"asd","participantid":"{9,1,2,3,NULL,NULL,NULL,NULL,NULL,NULL}","mainphotoid":2,"photosid":"{1,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}","ownerid":2,"invitationlist":"{2,4,7,8,NULL,NULL,NULL,NULL,NULL,NULL}","privilligeId":"{1,2,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL}","eventid":3,"eventdate":"2016-10-05 00:00:00","eventitle":"asdas"},{"eventdescription":"UpdateasddddLet's starts","participantid":"{9,1,2,3,NULL,NULL,NULL,NULL,NULL,NULL}","mainphotoid":2,"photosid":"{1,2,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}","ownerid":3,"invitationlist":"{2,4,7,8,NULL,NULL,NULL,NULL,NULL,NULL}","privilligeId":"{1,2,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL}","eventid":4,"eventdate":"2016-10-05 00:00:00","eventitle":"asdasd"},null,null,null,null,null,null,null]")

I am really desperate here i just hope that somebody can help me immediately

I have used swiftyJson and could handle the nulls.

Hello dude, Please help me.

URL (GET) - "https://api.github.com/repos/ReactiveCocoa/ReactiveCocoa/pulls?state=all"

image

image

image
...

Mapper below

image

I always got
image