Created
January 10, 2023 18:47
-
-
Save mdb1/2f82f85add839d90dd86d56c5f6d50c7 to your computer and use it in GitHub Desktop.
Default JsonEncoder and JsonDecoder to reuse across the app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| extension JSONEncoder { | |
| /// Returns a JSONEncoder object using: | |
| /// * `.convertToSnakeCase` as `keyEncodingStrategy`. | |
| /// * `.iso8601` as `.dateEncodingStrategy`. | |
| static var `default`: JSONEncoder { | |
| let encoder = JSONEncoder() | |
| encoder.keyEncodingStrategy = .convertToSnakeCase | |
| encoder.dateEncodingStrategy = .iso8601 | |
| return encoder | |
| } | |
| } | |
| extension JSONDecoder { | |
| /// Returns a JSONDecoder object using: | |
| /// * `.convertFromSnakeCase` as` keyDecodingStrategy`. | |
| /// * `.iso8601` as `.dateDecodingStrategy`. | |
| static var `default`: JSONDecoder { | |
| let decoder = JSONDecoder() | |
| decoder.keyDecodingStrategy = .convertFromSnakeCase | |
| decoder.dateDecodingStrategy = .iso8601 | |
| return decoder | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment