Created
March 8, 2018 04:51
-
-
Save hachinobu/76c3528b961046669e5c14a40b8db8c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Address : Codable { | |
var street: String | |
var zipCode: String | |
var city: String | |
var state: String | |
} | |
struct AnyCodingKey : CodingKey { | |
var stringValue: String | |
var intValue: Int? | |
init(_ base: CodingKey) { | |
self.init(stringValue: base.stringValue, intValue: base.intValue) | |
} | |
init(stringValue: String) { | |
self.stringValue = stringValue | |
} | |
init(intValue: Int) { | |
self.stringValue = "\(intValue)" | |
self.intValue = intValue | |
} | |
init(stringValue: String, intValue: Int?) { | |
self.stringValue = stringValue | |
self.intValue = intValue | |
} | |
} | |
extension JSONEncoder.KeyEncodingStrategy { | |
static var convertToUpperCamelCase: JSONEncoder.KeyEncodingStrategy { | |
return .custom { codingKeys in | |
var key = AnyCodingKey(codingKeys.last!) | |
// uppercase first letter | |
if let firstChar = key.stringValue.first { | |
let i = key.stringValue.startIndex | |
key.stringValue.replaceSubrange( | |
i ... i, with: String(firstChar).uppercased() | |
) | |
} | |
return key | |
} | |
} | |
} | |
extension JSONDecoder.KeyDecodingStrategy { | |
static var convertFromUpperCamelCase: JSONDecoder.KeyDecodingStrategy { | |
return .custom { codingKeys in | |
var key = AnyCodingKey(codingKeys.last!) | |
// lowercase first letter | |
if let firstChar = key.stringValue.first { | |
let i = key.stringValue.startIndex | |
key.stringValue.replaceSubrange( | |
i ... i, with: String(firstChar).lowercased() | |
) | |
} | |
return key | |
} | |
} | |
} | |
let address = Address(street: "Apple Bay Street", zipCode: "94608", | |
city: "Emeryville", state: "California") | |
do { | |
let encoder = JSONEncoder() | |
encoder.keyEncodingStrategy = .convertToUpperCamelCase | |
let encoded = try encoder.encode(address) | |
print(String(decoding: encoded, as: UTF8.self)) | |
} catch { | |
print(error) | |
} | |
let jsonString = """ | |
{"Street":"Apple Bay Street","City":"Emeryville","State":"California","ZipCode":"94608"} | |
""" | |
do { | |
let decoder = JSONDecoder() | |
decoder.keyDecodingStrategy = .convertFromUpperCamelCase | |
let decoded = try decoder.decode(Address.self, from: Data(jsonString.utf8)) | |
print(decoded) | |
} catch { | |
print(error) | |
} | |
// Address(street: "Apple Bay Street", zipCode: "94608", | |
// city: "Emeryville", state: "California") |
Hey hachinobu 👋
We would like to use your code for a commercial product, but we couldn't identify any license information. Can you please tell us the terms of use (e.g. commercial use, distribution, modification etc.) or under which license we can use this snippet?
Thank you very much and kind regards.
Roman
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks brother for this awesome solution