Kitura / Kitura-CouchDB

CouchDB adapter for Kitura
Apache License 2.0
51 stars 29 forks source link

Define dateEncodingStrategy #112

Open Spriter opened 5 years ago

Spriter commented 5 years ago

Currently there is no chance to define a custom dateEncodingStrategy. The JSONEncoder object is created without any config options.

    class func documentRequest<D: Document>(document: D, options: [ClientRequest.Options], callback: @escaping (DocumentResponse?, CouchDBError?) -> ()) {
        do {
            let requestBody = try JSONEncoder().encode(document)
            couchRequest(body: requestBody, options: options, passStatusCodes: [.created, .accepted], callback: callback)
        } catch {
            return callback(nil, CouchDBError(HTTPStatusCode.internalServerError, reason: error.localizedDescription))
        }
    }

Would be great if we can define a custom date encoding strategy like this.

let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .custom({ (date, encoder) in
    var container = encoder.singleValueContainer()
    try container.encode(DateFormatter.myDateFormatter.string(from: date))
})

What do you think about that?

ianpartridge commented 5 years ago

Thanks for opening the issue. Yes, that would be a nice addition to this project.