Skip to content

Commit

Permalink
Document JSON coder extensions in related article
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Nov 17, 2024
1 parent 49dc8d5 commit f392789
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/HandySwift/Extensions/JSONDecoderExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension JSONDecoder {
/// let user = try JSONDecoder.snakeCase.decode(User.self, from: jsonData)
/// // Results in: User(firstName: "John", lastName: "Doe")
/// ```
static var snakeCase: JSONDecoder {
public static var snakeCase: JSONDecoder {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
Expand Down
2 changes: 1 addition & 1 deletion Sources/HandySwift/Extensions/JSONEncoderExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension JSONEncoder {
/// let jsonData = try JSONEncoder.snakeCase.encode(user)
/// // Results in: {"first_name": "John", "last_name": "Doe"}
/// ```
static var snakeCase: JSONEncoder {
public static var snakeCase: JSONEncoder {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
return encoder
Expand Down
32 changes: 32 additions & 0 deletions Sources/HandySwift/HandySwift.docc/Essentials/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:)``
Expand Down

0 comments on commit f392789

Please sign in to comment.