From 0d40e85620340a5121332c25edac1adb10a0448f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Sun, 17 Nov 2024 13:08:06 +0100 Subject: [PATCH] Document JSON coder extensions in related article --- .../HandySwift.docc/Essentials/Extensions.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Sources/HandySwift/HandySwift.docc/Essentials/Extensions.md b/Sources/HandySwift/HandySwift.docc/Essentials/Extensions.md index f3d9ade..2bb9f62 100644 --- a/Sources/HandySwift/HandySwift.docc/Essentials/Extensions.md +++ b/Sources/HandySwift/HandySwift.docc/Essentials/Extensions.md @@ -113,6 +113,33 @@ Text(fractionCompleted.rounded(fractionDigits: 2).formatted(.percent)) > Note: There's also a mutating ``Swift/Double/round(fractionDigits:rule:)`` functions if you want to change a variable in-place. +#### JSON Snake Case Conversion + +Many APIs return responses using snake_case naming (e.g., `first_name`, `date_of_birth`), while Swift uses camelCase by convention (e.g., `firstName`, `dateOfBirth`). Converting between these naming conventions is a very common need when working with JSON APIs: + +```swift +struct User: Codable { + let firstName: String + let lastName: String + let dateOfBirth: Date + let profileImageUrl: String? +} + +func fetchUser(id: String) async throws -> User { + let (data, _) = try await URLSession.shared.data(from: apiURL) + + // Automatically converts snake_case JSON to camelCase Swift properties + return try JSONDecoder.snakeCase.decode(User.self, from: data) + + // Without this extension we'd need this every time: + // let decoder = JSONDecoder() + // decoder.keyDecodingStrategy = .convertFromSnakeCase + // return try decoder.decode(User.self, from: data) +} +``` + +Just use ``JSONDecoder.snakeCase`` to decode and ``JSONEncoder.snakeCase`` to encode instead of configuring a new instance each time! + #### Symmetric Data Cryptography ![](SharePuzzle) @@ -215,6 +242,11 @@ func downloadPuzzle(from url: URL) async throws -> Puzzle { - ``Swift/Int/times(_:)`` - ``Swift/Int/timesMake(_:)`` +### JSON Coding + +- ``Foundation/JSONDecoder/snakeCase`` +- ``Foundation/JSONEncoder/snakeCase`` + ### RandomAccessCollection - ``Swift/RandomAccessCollection/randomElements(count:)``