Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define dateEncodingStrategy #112

Open
Spriter opened this issue Nov 2, 2019 · 1 comment
Open

Define dateEncodingStrategy #112

Spriter opened this issue Nov 2, 2019 · 1 comment

Comments

@Spriter
Copy link

Spriter commented Nov 2, 2019

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants