Skip to content

Instantly share code, notes, and snippets.

@mingyeow
Created August 14, 2016 06:41
Show Gist options
  • Save mingyeow/34c09e97a6364ba589868cde050f9cef to your computer and use it in GitHub Desktop.
Save mingyeow/34c09e97a6364ba589868cde050f9cef to your computer and use it in GitHub Desktop.
Using ObjectMapper When Json Contains Root
//: Please build the scheme 'ObjectMapperPlayground' first
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import ObjectMapper
let questionsArrayJson = "{\"questions\":[{\"id\":1,\"body\":\"ddd\",\"price_in_cent\":0,\"can_listen\":false,\"asker\":{\"id\":1,\"name\":\"feafeafeafe\",\"title\":null,\"about_me\":null,\"price_in_cent\":100,\"email\":null,\"authentication_token\":\"R3/KBYssIRLtRR30xJoFy7+c3XZ3NLcj1ClanCFyF/8UmlwdnVbWJrAdJyLrbnO+U50xaDK2+VX7/dTVXRmNmw==\",\"has_payment\":false},\"answerer\":{\"id\":2,\"name\":\"feafeafeafe\",\"title\":null,\"about_me\":null,\"price_in_cent\":100}},{\"id\":2,\"body\":\"ddd\",\"price_in_cent\":0,\"can_listen\":false,\"asker\":{\"id\":1,\"name\":\"feafeafeafe\",\"title\":null,\"about_me\":null,\"price_in_cent\":100,\"email\":null,\"authentication_token\":\"R3/KBYssIRLtRR30xJoFy7+c3XZ3NLcj1ClanCFyF/8UmlwdnVbWJrAdJyLrbnO+U50xaDK2+VX7/dTVXRmNmw==\",\"has_payment\":false},\"answerer\":{\"id\":2,\"name\":\"feafeafeafe\",\"title\":null,\"about_me\":null,\"price_in_cent\":100}}]}"
let questionObjectJson = "{\"question\":{\"id\":1,\"body\":\"ddd\",\"price_in_cent\":0,\"can_listen\":false,\"asker\":{\"id\":1,\"name\":null,\"title\":null,\"about_me\":null,\"price_in_cent\":100,\"email\":null,\"authentication_token\":\"R3/KBYssIRLtRR30xJoFy7+c3XZ3NLcj1ClanCFyF/8UmlwdnVbWJrAdJyLrbnO+U50xaDK2+VX7/dTVXRmNmw==\",\"has_payment\":false},\"answerer\":{\"id\":2,\"name\":null,\"title\":null,\"about_me\":null,\"price_in_cent\":100}}}"
let userObjectJson = "{\"user\":{\"id\":1,\"name\":\"afeafeafe\",\"title\":null,\"about_me\":null,\"price_in_cent\":100,\"email\":null,\"authentication_token\":\"R3/KBYssIRLtRR30xJoFy7+c3XZ3NLcj1ClanCFyF/8UmlwdnVbWJrAdJyLrbnO+U50xaDK2+VX7/dTVXRmNmw==\",\"has_payment\":false}}"
struct Question: Mappable {
var id: Int = 0
var body: String = ""
var asker: User = User()
var answerer: User = User()
var amount: Int = 0
init(){}
init?(_ map: Map){
// check if a required "name" property exists within the JSON.
}
mutating func mapping(map: Map){
id <- map["question.id"]
body <- map["question.body"]
asker <- map["question.asker"]
answerer <- map["question.target"]
amount <- map["question.amount"]
}
}
struct User: Mappable {
var id: Int = 0
var name: String = ""
var title: String = ""
var aboutMe: String = ""
var priceInCent: Int = 0
// Authenticated user only attributes
var authenticationToken: String?
var email: String?
var hasPayment: Bool = false
var screen_name: String = ""
var web_url: String = ""
var profile_image_url: String = ""
var answered_count: Int = 0
var cost_per_question: Int = 0
var income: Int = 0
var profile_image_nsurl: NSURL! { return NSURL(string:profile_image_url) }
init() {}
init?(_ map: Map) {}
var id_as_string:String{
return "\(id)"
}
mutating func mapping(map: Map) {
id <- map["user.id"]
name <- map["user.name"]
title <- map["user.title"]
aboutMe <- map["user.about_me"]
priceInCent <- map["user.price_in_cent"]
authenticationToken <- map["user.authentication_token"]
email <- map["user.email"]
hasPayment <- map["user.has_payment"]
screen_name <- map["user.screen_name"]
web_url <- map["user.web_url"]
profile_image_url <- map["user.profile_image_url"]
answered_count <- map["user.answered_count"]
cost_per_question <- map["user.cost_per_question"]
}
}
let user = Mapper<User>().map(userObjectJson)
print ("user maps correctly : \(user?.name)")
let question = Mapper<Question>().map(questionObjectJson)
print ("Question ID maps correctly : \(question?.id)")
print ("Asker does not map: \(question?.asker.id)")
// THIS DOES NOT MAP CORRECTlY
let questions = Mapper<Question>().mapArray(questionsArrayJson)
print ("Count of questions is wrong: \(questions?.count)")
print ("Body of first question is wrong: \(questions?.first?.body)")
struct QuestionResponse: Mappable {
var questions: [Question]?
init() {}
init?(_ map: Map) {}
mutating func mapping(map: Map) {
self.questions <- map["questions"]
}
}
let questionsResponses = Mapper<QuestionResponse>().map(questionsArrayJson)
print ("Using a special class to wrap responses work : \(questionsResponses?.questions?.count)")
print ("Mapping user is wrong: \(questionsResponses?.questions?.first?.answerer.name)")
//user maps correctly : Optional("afeafeafe")
//Question ID maps correctly : Optional(1)
//Asker does not map: Optional(0)
//Count of questions is wrong: Optional(1)
//Body of first question is wrong: Optional("")
//Using a special class to wrap responses work : Optional(2)
//Mapping user is wrong: Optional("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment