I'm trying to achieve something generic to parse my JSON response so far i'm able to get them this close
class ParentJson<T: Mappable> : Mappable {
var StatusCode: Int?
var Message: String?
var dataArray: [T]?
var dataObject: T?
required init?(map: Map) {
}
func mapping(map: Map) {
StatusCode <- map["StatusCode"]
Message <- map["Message"]
dataArray <- map["Data"]
dataObject <- map["Data"]
}
}
class DataJson: Mappable {
var OffsiteReportID: Int?
var InspectionActivityID: Int?
var OffsiteReportAddedByID: Int?
var OffsiteReportDisplayID: String?
var OffsiteReportDate: String?
var OffsiteReportDescription: String?
var OffsiteReportDetails: String?
required init?(map: Map) {
}
func mapping(map: Map) {
OffsiteReportID <- map["OffsiteReportID"]
InspectionActivityID <- map["InspectionActivityID"]
OffsiteReportAddedByID <- map["OffsiteReportAddedByID"]
OffsiteReportDisplayID <- map["OffsiteReportDisplayID"]
OffsiteReportDate <- map["OffsiteReportDate"]
OffsiteReportDescription <- map["OffsiteReportDescription"]
OffsiteReportDetails <- map["OffsiteReportDetails"]
}
}
JSON under "Data" could be Object or an Array. Basic issue is I don't want to use dataArray & dataObject only to leave on variable nil and check which one is nil and use other one I want to have only one variable which return Object or array of objects based on data, I have idea about "Mapping Context" but i'm not sure if that could help me
I am facing similar issue. where T can be object or array. While I am trying to pass array of any class then it is giving error
Type '[MyItem]' does not conform to protocol 'Mappable'
class MyResponse<T: Mappable>: Mappable {
var status:Bool = false
var message:String = ""
var data:T?
required init?(map: Map) {
}
func mapping(map:Map) {
status <- map["status"]
message <- map["message"]
data <- map["data"]
}
}
Working code for single object MyResponse<MyDetail>
Not working for array MyResponse<[MyItem]>
Months and months later I still am not entirely sure how ObjectMapper intends for Mappable items to be used in generics like this. For nearly a year now I've had code similar to the above, just with alternate versions like MyResponseList<T: Mappable>: Mappable which has var data: [T]? when I need it, but I really hate doing this.
@viral-etymon i think [MyItem] , becomes Array of My item and array don't confirm to Mappable, MyItem does, but there must be some way to get either object or array of objects
@bdrelling I'm using same thing but i feel it's redundant, let's hope something better comes out of our discussion here
This is an area where there is room for improvement in ObjectMapper. My current approach is to use two different models based on the response type (which I agree is not ideal). If we could have [T: Mappable] conform to Mappable it would solve the issue. If anybody wants to try to tackle adding this functionality I'm sure many people would be happy.
@tristanhimmelman would this be something that a custom transform class could solve? I'm currently looking for the same desired functionality and trying to figure out how best to solve it besides just having two different model properties.
@tristanhimmelman more specifically. opposed to trying to differentiate between an array or object; could I just map the type [NSObject] and use a transform to wrap a response of type object inside of an array? That way an array is always returned with the model?
Most helpful comment
This is an area where there is room for improvement in ObjectMapper. My current approach is to use two different models based on the response type (which I agree is not ideal). If we could have
[T: Mappable]conform toMappableit would solve the issue. If anybody wants to try to tackle adding this functionality I'm sure many people would be happy.