【Swift】 JSONEncoder / JSONDecorder の基本的な使い方

Apple 公式リファレンスから。サンプルも抜粋。


func encode<T>(_ value: T) throws -> Data where T : Encodable


func decode<T>(
    _ type: T.Type,
    from data: Data
) throws -> T where T : Decodable

👉 encode(_:) | Apple Developer Documentation hatena-bookmark
👉 decode(_:from:) | Apple Developer Documentation hatena-bookmark


struct GroceryProduct: Codable {
  var name: String
  var points: Int
  var description: String?
}

let pear = GroceryProduct(name: "Pear", points: 250, description: "A ripe pear.")

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

let data = try encoder.encode(pear)
print(String(data: data, encoding: .utf8)!)

/*
{
  "name" : "Pear",
  "points" : 250,
  "description" : "A ripe pear."
}
*/


struct GroceryProduct: Codable {
  var name: String
  var points: Int
  var description: String?
}

let json = """
{
    "name": "Durian",
    "points": 600,
    "description": "A fruit with a distinctive scent."
}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let product = try decoder.decode(GroceryProduct.self, from: json)

print(product.name) 

// Prints "Durian"

👉 JSONEncoder | Apple Developer Documentation hatena-bookmark
👉 JSONDecoder | Apple Developer Documentation hatena-bookmark

Codable というのは、Encodable + Decodable のこと。


typealias Codable = Decodable & Encodable

👉 Codable | Apple Developer Documentation hatena-bookmark

 

🧑🏻‍💻 まとめ

まとめると以下のイメージ。

Data と String の相互変換は以下。


さらに、キー名のカスタムをしたい場合は以下から。

👉 Encoding and Decoding Custom Types | Apple Developer Documentation hatena-bookmark



【Swift】URLSession メソッドの使い分け

以前、通信部分の書き方を調べていたときに、

👉 【Swift】URLSession.shared.dataTask() をうまく使いこなせない hatena-bookmark

古い API の公での dis がワロエタ。

👉 Use async/await with URLSession - WWDC21 - Videos - Apple Developer hatena-bookmark

先人を敬えよ。

でも分かりやすくなって非常に良い。

 

🧑🏻‍💻 新しいAPIの使い分け

新しい API として以下が上げられている。


URLSession.shared.data() 
URLSession.shared.download()
URLSession.shared.upload()
URLSession.shared.bytes()

ドキュメントを見ながら、引数と戻り値がわかりやすように並べてみる。


func data(
    for request: URLRequest,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (Data, URLResponse)


func data(
    from url: URL,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (Data, URLResponse)


func download(
    for request: URLRequest,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (URL, URLResponse)


func download(
    from url: URL,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (URL, URLResponse)


func download(
    resumeFrom resumeData: Data,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (URL, URLResponse)


func upload(
    for request: URLRequest,
    from bodyData: Data,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (Data, URLResponse)


func upload(
    for request: URLRequest,
    fromFile fileURL: URL,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (Data, URLResponse)


func bytes(
    for request: URLRequest,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (URLSession.AsyncBytes, URLResponse)


func bytes(
    from url: URL,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (URLSession.AsyncBytes, URLResponse)

なんとなく見えてきます。

 

🧑🏻‍💻 使い分けの基準

使い分けの基準としては、

「受け取りたいデータ形式」

ですね。

受け取ってからの利用するデータ形式が、


テキスト → .data()

ファイル → .download()

バイトバッファー → .bytes()

となります。

あとは、その他として


ファイルを送りたい → .upload()

となります。

リクエスト後のレスポンスは、すべてにある戻り値 URLResponse を使って


urlresponse as? HTTPURLResponse

のようにダウンキャストして、レスポンスコードなどを確認します。

 

🧑🏻‍💻 URL か URLRequest か

それぞれのメソッドに引数には、URL か URLRequest があります。

これらの使い分けとしては、

リクエストメソッド・ヘッダーのカスタムが


必要でない → URL

必要 → URLRequest

というかんじでしょうか。

リクエストヘッダーのカスタムは以下のような感じで書けるようです。


var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer 123456ABC", forHTTPHeaderField: "Authorization")
request.httpBody = try! JSONEncoder().encode(User(name: "John", age: 15))

let (data, response) = try await session.data(for: request)
print((response as! HTTPURLResponse).statusCode)
print(String(data: data, encoding: .utf8)!)

 

🧑🏻‍💻 まとめ

「リクエストヘッダーを常に意識しながら、JSONを受け取る。」

という感じの以下が最も使える基本的なメソッドとなるように思います。


func data(
    for request: URLRequest,
    delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (Data, URLResponse)

このメソッドだけで、

リクエストメソッドとヘッダー次第で、

ほとんどできるのではないかと思ったりもする。

👉 【Swift】ファイルやディレクトリのパスが長すぎていやだ – URL.shortPath() hatena-bookmark

最後に、便利な無料確認サービスをどうぞ。


👉 httpbin.org hatena-bookmark


【Swift】 文字コードの変換 を整理してみた

前にやってみた、これ。


2つの型(プロトコル)の間を相互に変換するだけでも、いろんな書き方があるのだな。

などと思いつつ気になってたのが文字コードの変換。

👉 文字コード - Wikipedia hatena-bookmark

今回、おおまかに整理しておきたい。

 

🧑🏻‍💻 String の内部エンコーディングは UTF-8

Swift 5 switches the preferred encoding of strings from UTF-16 to UTF-8 while preserving efficient Objective-C-interoperability.

👉 Swift.org - UTF-8 String hatena-bookmark

String 内部のエンコーディングは「UTF-8」になったらしいです。

 

🧑🏻‍💻 String、 Data とファイル間での相互変換

図で。大体こんな。

まずは、エンコーディングの引数名が、encoding:, using:, as: と多様なので初見、分かりづらかった。


「String から」または「Stringへ」変換する場合に

「相手」 (「変換先」または「変換元」) のエンコーディングを指定する。

String 自体は「UTF-8」として意識。

Data と ファイル間の変換にはエンコーディングの指定は不要。

Data はそれぞれに変換済みのバイトシーケンス。

 

🧑🏻‍💻 変換してみる

コードを書いて試してみます。

👉 【Swift】よく使いそうな String の format hatena-bookmark


"あいうえお"
  .data(using: .shiftJIS)!.map { String(format: "%02X", $0) }.joined()
// 10 bytes 82A082A282A482A682A8

からスタートして一通り。

以前書いた extension を使っています。

👉 【Swift】ファイルやディレクトリ操作するための extension をまずは作った hatena-bookmark

これで大体いけるはず。

注意としては、「変換できない文字もある」ということ。


o  Shift_JIS → UTF-8 
x  UTF-8     → Shift_JIS

対応する文字がない。

 

🧑🏻‍💻 参考

👉 String.Encoding | Apple Developer Documentation hatena-bookmark
👉 shiftJIS | Apple Developer Documentation hatena-bookmark
👉 init(data:encoding:) | Apple Developer Documentation hatena-bookmark
👉 init(contentsOf:encoding:) | Apple Developer Documentation hatena-bookmark
👉 data(using:allowLossyConversion:) | Apple Developer Documentation hatena-bookmark
👉 write(to:atomically:encoding:) | Apple Developer Documentation hatena-bookmark
👉 init(contentsOf:options:) | Apple Developer Documentation hatena-bookmark
👉 write(to:options:) | Apple Developer Documentation hatena-bookmark