0

I'm trying to decode a date sent from my Node.JS backend in Swift 5.1 but I can't seem to fix it. I've created a JSONDecoder(), set its dateDecodingStrategy to .iso8601 but still it gives me the following error:

The data couldn’t be read because it isn’t in the correct format. (localizedDescription)

This is the request I'm trying to parse

{ 
   "parties":[ 
      { 
         "location":{ 
            "type":"Point",
            "coordinates":[ 
               50.92,
               4.0587
            ]
         },
         "participants":[ 
            "5db8838eaffe445c66076a88"
         ],
         "declines":[ 

         ],
         "_id":"5dd0264902611d001e968396",
         "name":"DnD: Into the Abyss",
         "date":"2020-03-13T00:00:00.000Z",
         "maxSize":3,
         "gameId":"5dd0229102611d001e968392",
         "createdAt":"2019-11-16T16:39:37.878Z",
         "updatedAt":"2019-11-16T16:39:37.878Z",
         "__v":0
      }
   ]
}

The code

    func getPartiesNearYou(maxDistance: Int, userId: String, latitude: Double, longitude: Double) {
        let urlString = "\(url)getPartiesNearYou?distance=\(maxDistance)&lat=\(latitude)&long=\(longitude)&userId=\(userId)"
        print("URL", urlString)
        performRequest(with: urlString) {data, response, error in
            if error != nil {
                self.delegate?.didFail(with: error!)
                return
            }

            if let safeData = data {
                let decoder = JSONDecoder()
                    decoder.dateDecodingStrategy = .iso8601
                do {
                    let decodedContainer = try decoder.decode(PartiesNetworkContainer.self, from: safeData)
                    self.delegate?.updateParties(self, decodedContainer.parties)
                } catch {
                    self.delegate?.didFail(with: error)
                }
            }
        }
    }

Party Struct

public struct Party: Codable {

    var _id: String?
    var name: String
    var date: Date
    var maxSize: Int = 4
    var participants: [String]
    var gameId: String
    var location: Location
    var declines: [String]

    init(id _id:String?, name: String, date: Date, maxSize: Int, participants: [String], gameId: String, location: Location, declines: [String]) {
        self._id = _id
        self.name = name
        self.date = date
        self.maxSize = maxSize
        self.participants = participants
        self.gameId = gameId
        self.location = location
        self.declines = declines
    }

}
Mout Pessemier
  • 1,665
  • 3
  • 19
  • 33
  • 1
    you neecd to create a custom date decoding strategy that includes the fractional seconds as well https://stackoverflow.com/a/28016692/2303865 `extension JSONDecoder.DateDecodingStrategy { static let iso8601withFractionalSeconds = custom { let container = try $0.singleValueContainer() let string = try container.decode(String.self) guard let date = Formatter.iso8601.date(from: string) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid date: " + string) } return date } }` – Leo Dabus Dec 03 '19 at 20:32
  • @LeoDabus I'll check it out, thanks in advance! – Mout Pessemier Dec 03 '19 at 20:38

0 Answers0