In Swift, when JSON purge, it is passed in the snake case and troublesome coping method

1.First of all

――As the title says, it is described as a memorandum. ――For camel case and snake case, please check with Google teacher. --Example: https://designsupply-web.com/media/developmentlab/4052/

2. version

3. 3. When received in camel case

When the API format is camel case when received by API. Simply put, if you can use it as it is.

import Foundation

struct Response: Codable {
    ///name
    let firstName: String
    ///Last name
    let lastName: String
}

///received data
var json = """
{
"firstName": "Yamada",
"lastName": "Taro"
}
"""

let data = json.data(using: .utf8)!

///Decoding process
let model = try! JSONDecoder().decode(Response.self, from: data)

print(model)

4. There is a way to receive in the snake case in the parameter of JSONDecoder

let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase

Encode above. However, this alone cannot be used automatically, so cut ʻenum, which is a derivative of CodingKey`, in the receiving structure. It looks like the following

struct Response: Codable {
    ///name[first_name]
    let firstName: String
    ///Last name[last_name]
    let lastName: String

    private enum CodingKeys: String, CodingKey {
        case firstName
        case lastName
    }
}

5. Summary

With the following implementation, even if the API automatically returns in the snake case, it can be used in the camel case structure on the code side. Ah convenient (If you know it, it's not a big story, though)

import Foundation

struct Response: Codable {
    ///name[first_name]
    let firstName: String
    ///Last name[last_name]
    let lastName: String

    private enum CodingKeys: String, CodingKey {
        case firstName
        case lastName
    }
}

///received data
var json = """
{
"first_name": "Yamada",
"last_name": "Taro"
}
"""

let data = json.data(using: .utf8)!

///Decoding process
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
let model = try! jsonDecoder.decode(Response.self, from: data)

print(model)

The end

Recommended Posts

In Swift, when JSON purge, it is passed in the snake case and troublesome coping method
[Ruby] Thinking when there is no receiver in front of the method (it looks like)
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
Is it possible to put the library (aar) in the Android library (aar) and use it?
[Swift] Rather, it is meaningful to avoid var and declare it with let only when there is a branch in the assignment of the initial value.
What is the main method in Java?
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
If it is Ruby, it is efficient to make it a method and stock the processing.
Make the JSON of the snake case correspond to the field of the camel case in Java (JVM)
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
When the project is not displayed in eclipse
Get the value from the array and find out what number it is included in
Asynchronous processing executed in Future in Swift Combine is sinking, but it is canceled in the middle