LoginSignup
5
1

More than 5 years have passed since last update.

【Swift4.1】JSONDecoderのスネークケース自動変換で気になったこと

Last updated at Posted at 2018-04-05

経緯

Swift4.1からJSONDecoderのKeyDecodingStrategyを設定することで
スネークケースからキャメルケースへの変換を自動でしてくれるようになったので確認していたところ、
ふと気になったことがあったのでメモします。

実装

struct Profile: Decodable {

    let id: Int
    let messageTitle: String
    let profileImageUrl: String
    let aaaBbbCcc: String // ??
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            let json = """
            {
                "id": 1,
                "message_title": "title",
                "profile_image_url": "hogehoge",
                "Aaa_bBb_ccC": "aaaa" //??
            }
            """.data(using: .utf8)!


            let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            let result = try decoder.decode(Profile.self, from: json)
            print(result)

        } catch {
            print(error)
        }
    }
}

変換結果

Profile(id: 1, 
        messageTitle: "title", 
        profileImageUrl: "hogehoge", 
        aaaBbbCcc: "aaaa") // ??

疑問

なんで"Aaa_bBb_ccC"がaaaBbbCccになるんだろうと思い、
わざとエラーにした場合のコンソールの出力を見ていると

"No value associated with 
key CodingKeys(stringValue: \"aaaBBbCcC\", intValue: nil) (\"aaaBBbCcC\"), converted to aaa_b_bb_cc_c." 

と出ていたので、
Decodableの中で宣言した変数の大文字部分を_(アンダーバー)で区切って全部を小文字に変換したものと
JSON文字列の方も全て小文字に変換したものを比較して
値を設定しているような動きをしています。
どういう仕組みなのか時間あるときに詳しく調べたいです。

スネークケースの中に大文字が混ざることはあまりないかもしれませんが、
気になったので記録しました。

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1