tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.14k stars 1.03k forks source link

toJSONString() not converting dates #1099

Open mxnmike opened 4 years ago

mxnmike commented 4 years ago

Your JSON dictionary:

 "mainEvent": {
        "id": 1,
        "contact": "",
        "email": "",
        "day": "",
        "phone": "",
        "openReservation": "2020-06-05",
        "closeReservation": "2020-08-01",
        "crawlEpisodeNumber": "",
        "crawlEpisodeTitle": "",
        "crawlBody": "",
        "events": [
        ]
    }

Your model:

class MainEventDTO: Mappable {

    var id: Int
    var title, contact, phone, createdAt, email: String
    var day: String
    var updatedAt: String
    var events: [EventDTO]
    var openReservation: Date
    var closeReservation: Date
    var reservationDateStatus: ReservationStatus
    var crawlEpisodeNumber: String
    var crawlEpisodeTitle: String
    var crawlBody: String

    init() {

        id = 0
        title = ""
        contact = ""
        phone = ""
        email = ""
        createdAt = ""
        updatedAt = ""
        events = []
        day = ""
        openReservation = Date()
        closeReservation = Date()
        reservationDateStatus = .closed
        crawlEpisodeNumber = ""
        crawlEpisodeTitle = ""
        crawlBody = ""

    }

    required convenience init?(map: Map) {
        guard map.JSON["id"] != nil, map.JSON["contact"] != nil, map.JSON["phone"] != nil, map.JSON["events"] != nil else { return nil }
        self.init()
    }

    func mapping(map: Map) {
        id <- map["id"]
        title <- map["title"]
        contact <- map["contact"]
        phone <- map["phone"]
        email <- map["email"]
        createdAt <- map["createdAt"]
        updatedAt <- map["updatedAt"]
        events <- map["events"]
        day <- map["day"]
        crawlEpisodeNumber <- map["crawlEpisodeNumber"]
        crawlEpisodeTitle <- map["crawlEpisodeTitle"]
        crawlBody <- map["crawlBody"]
        openReservation <- (map["openReservation"], YearDateTransform())
        closeReservation <- (map["closeReservation"], YearDateTransform())
        reservationDateStatus =  Date().reservationStatus(openReservation, closeReservation)
    }
}

open class YearDateTransform: DateFormatterTransform {  
    static let reusableISODateFormatter = DateFormatter(withFormat: "yyyy-MM-dd", locale: Locale.current.identifier)
    public init() {
        super.init(dateFormatter: YearDateTransform.reusableISODateFormatter)
    }
}

What you did:

let mainEvent = MainEvent() // Initialize and get all the info from my class
let repoString = mainEvent.toJSONString()

What you expected:

I expected something like:

getting my complete object parsed to json formatted string

What you got:

I get all the information correct, except for the openReservation and closeReservation its completely dissappeared.

Im using class, because im passing by reference, so thats needed.

I know its mapping the object properly, because if i print directly mainEvent.openReservation or closeReservation i get the date.... is just missing when converted to JSONString.... how can i fix this?

I need this because im storing it temporary in userdefaults as Cache information.