How can I decode partially double serialized json string using `Codable` protocol? - swift

How can I decode partially double serialized json string using Codable protocol?
class Person : Codable {
var name : String?
var hobby : String?
}
class Family : Codable {
var person: String?
var person_: Person?
}
class PerfectFamily : Codable {
var person: Person?
}
let jsonString = "{\"person\":\"{\\\"name\\\":\\\"Mike\\\",\\\"hobby\\\":\\\"fishing\\\"}\"}"
do {
// I could do this.
let family = try JSONDecoder().decode(Family.self, from: Data(jsonString.utf8))
family.person_ = try JSONDecoder().decode(Person.self, from: Data(family.person!.utf8))
print(family)
// However I want to write more simply like this. Do you have some idea?
let perfectFamily = try JSONDecoder().decode(PerfectFamily.self, from: Data(jsonString.utf8)) // error
print(perfectFamily)
} catch {
print(error)
}

If you can't fix your double encoded json you can provide your own custom decoder method to your PerfectFamily class but I recommend using a struct:
struct Person: Codable {
let name: String
let hobby: String
}
struct PerfectFamily: Codable {
let person: Person
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let person = try container.decode([String: String].self)["person"] ?? ""
self.person = try JSONDecoder().decode(Person.self, from: Data(person.utf8))
}
}
let json = "{\"person\":\"{\\\"name\\\":\\\"Mike\\\",\\\"hobby\\\":\\\"fishing\\\"}\"}"
do {
let person = try JSONDecoder().decode(PerfectFamily.self, from: Data(json.utf8)).person
print(person) // "Person(name: "Mike", hobby: "fishing")\n"
} catch {
print(error)
}

Related

Swift decodable with programatically provided coding keys

This is a simplified model that is decoded from JSON:
struct Info: Decodable {
var text: String
var num: Int
}
struct Root: Decodable {
let info: Info
}
Sometimes I need to decode Info.text or Info.num only but sometimes both of them and to support all options I've made similar structs for decoding e.g:
// For text only
struct InfoText: Decodable {
var text: String
}
struct RootText: Decodable {
let info: InfoText
}
// For num only
struct InfoNum: Decodable {
var num: Int
}
struct RootNum: Decodable {
let info: InfoNum
}
This approach produces much cloned code and runtime checks to process the structs so is it possible to decode provided coding keys only with the single struct?
It's possible to provide any contextual information to the decoder with userInfo property and in this case we can pass an array of coding keys and use this info in the decoding process:
struct Info: Decodable {
var text: String?
var num: Int?
static var keys = CodingUserInfoKey(rawValue: "keys")!
enum CodingKeys: String, CodingKey {
case text, num
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
guard let keys = decoder.userInfo[Self.keys] as? [CodingKeys] else {
return
}
if keys.contains(.text) {
text = try container.decode(String.self, forKey: .text)
}
if keys.contains(.num) {
num = try container.decode(Int.self, forKey: .num)
}
}
}
struct Root: Decodable {
let info: Info
}
let json = #"{ "info" : { "text": "Hello", "num": 20 } }"#.data(using: .utf8)!
let decoder = JSONDecoder()
let keys: [Info.CodingKeys] = [.text]
decoder.userInfo[Info.keys] = keys
let root = try decoder.decode(Root.self, from: json)
print(root)
// Outputs:
Root(info: Info(text: Optional("Hello"), num: nil))

How to decode JSON with objects as string values in Swift [duplicate]

How can I decode partially double serialized json string using Codable protocol?
class Person : Codable {
var name : String?
var hobby : String?
}
class Family : Codable {
var person: String?
var person_: Person?
}
class PerfectFamily : Codable {
var person: Person?
}
let jsonString = "{\"person\":\"{\\\"name\\\":\\\"Mike\\\",\\\"hobby\\\":\\\"fishing\\\"}\"}"
do {
// I could do this.
let family = try JSONDecoder().decode(Family.self, from: Data(jsonString.utf8))
family.person_ = try JSONDecoder().decode(Person.self, from: Data(family.person!.utf8))
print(family)
// However I want to write more simply like this. Do you have some idea?
let perfectFamily = try JSONDecoder().decode(PerfectFamily.self, from: Data(jsonString.utf8)) // error
print(perfectFamily)
} catch {
print(error)
}
If you can't fix your double encoded json you can provide your own custom decoder method to your PerfectFamily class but I recommend using a struct:
struct Person: Codable {
let name: String
let hobby: String
}
struct PerfectFamily: Codable {
let person: Person
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let person = try container.decode([String: String].self)["person"] ?? ""
self.person = try JSONDecoder().decode(Person.self, from: Data(person.utf8))
}
}
let json = "{\"person\":\"{\\\"name\\\":\\\"Mike\\\",\\\"hobby\\\":\\\"fishing\\\"}\"}"
do {
let person = try JSONDecoder().decode(PerfectFamily.self, from: Data(json.utf8)).person
print(person) // "Person(name: "Mike", hobby: "fishing")\n"
} catch {
print(error)
}

Swift Codable: Use a parent's key as as value

I have a JSON that has ID's in the root level:
{
"12345": {
"name": "Pim"
},
"54321": {
"name": "Dorien"
}
}
My goal is to use Codable to create an array of User objects that have both name and ID properties.
struct User: Codable {
let id: String
let name: String
}
I know how to use Codable with a single root level key and I know how to work with unknown keys. But what I'm trying to do here is a combination of both and I have no idea what to do next.
Here's what I got so far: (You can paste this in a Playground)
import UIKit
var json = """
{
"12345": {
"name": "Pim"
},
"54321": {
"name": "Dorien"
}
}
"""
let data = Data(json.utf8)
struct User: Codable {
let name: String
}
let decoder = JSONDecoder()
do {
let decoded = try decoder.decode([String: User].self, from: data)
decoded.forEach { print($0.key, $0.value) }
// 54321 User(name: "Dorien")
// 12345 User(name: "Pim")
} catch {
print("Failed to decode JSON")
}
This is what I'd like to do:
let decoder = JSONDecoder()
do {
let decoded = try decoder.decode([User].self, from: data)
decoded.forEach { print($0) }
// User(id: "54321", name: "Dorien")
// User(id: "12345", name: "Pim")
} catch {
print("Failed to decode JSON")
}
Any help is greatly appreciated.
You can use a custom coding key and setup User as below to parse unknown keys,
struct CustomCodingKey: CodingKey {
let intValue: Int?
let stringValue: String
init?(stringValue: String) {
self.intValue = Int(stringValue)
self.stringValue = stringValue
}
init?(intValue: Int) {
self.intValue = intValue
self.stringValue = "\(intValue)"
}
}
struct UserInfo: Codable {
let name: String
}
struct User: Codable {
var id: String = ""
var info: UserInfo?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CustomCodingKey.self)
if let key = container.allKeys.first {
self.id = key.stringValue
self.info = try container.decode(UserInfo.self, forKey: key)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CustomCodingKey.self)
if let key = CustomCodingKey(stringValue: self.id) {
try container.encode(self.info, forKey: key)
}
}
}
let decoder = JSONDecoder()
do {
let decoded = try decoder.decode(User.self, from: data)
print(decoded.id) // 12345
print(decoded.info!.name) // Pim
} catch {
print("Failed to decode JSON")
}

Extending a class model for generic type Swift

I am passing API response with Moya and getting this value. I am able to get the object but I extented a base response to handle extra parameters but the extended value does not seem to work. The data expected could be an array of objects and it could just be a regular object. After passing this values, It stopped working and data is not got but every other parameter like status , message are passed except data. Here is my Base response and how I used it
class MaxResponseBase: Codable {
var status: String?
var message: String?
var pagination: Pagination?
var isSucessful: Bool {
return status == "success"
}
struct ErrorMessage {
static let passwordInvalid = " Current password is invalid."
static let loginErrorIncorrectInfo = " Incorrect username/password."
static let loginErrorAccountNotExist = " Invalid request"
}
}
class MaxResponse<T: Codable>: MaxResponseBase {
var data: T?
}
class MaxArrayResponse<T: Codable>: MaxResponseBase {
var data = [T]()
}
Here is my API call for signin for example
func signin(email: String, password: String) -> Observable<MaxResponse<AuthResponse>> {
return provider.rx.request(.signin(username: email, password: password))
.filterSuccess()
.mapObject(MaxResponse<AuthResponse>.self)
.asObservable()
}
how can I tweak this to get data object also
JSON
{
"status" : "success",
"data" : {
"is_locked" : false,
"__v" : 0,
"created_at" : "2019-04-15T11:57:12.551Z"
}
}
It could also be an array of data
(Note: all the code below can be put in a Playground to show that it works.)
In order to solve this, you have to manually write all your initializers. I posted the code that does most of it below but I strongly recommend you use structs instead of classes. It is better in every way if you use structs and containment instead of classes and inheritance.
struct Pagination: Codable { }
struct AuthResponse: Codable {
let isLocked: Bool
let __v: Int
let createdAt: Date
}
class MaxResponseBase: Codable {
let status: String?
let message: String?
let pagination: Pagination?
var isSucessful: Bool {
return status == "success"
}
struct ErrorMessage {
static let passwordInvalid = " Current password is invalid."
static let loginErrorIncorrectInfo = " Incorrect username/password."
static let loginErrorAccountNotExist = " Invalid request"
}
enum CodingKeys: String, CodingKey {
case status, message, pagination
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
status = try container.decode(String?.self, forKey: .status)
message = try? container.decode(String?.self, forKey: .message) ?? nil
pagination = try? container.decode(Pagination?.self, forKey: .pagination) ?? nil
}
}
class MaxResponse<T: Codable>: MaxResponseBase {
let data: T?
enum DataCodingKeys: String, CodingKey {
case data
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DataCodingKeys.self)
data = try container.decode(T?.self, forKey: .data)
try super.init(from: decoder)
}
}
let json = """
{
"status" : "success",
"data" : {
"is_locked" : false,
"__v" : 0,
"created_at" : "2019-04-15T11:57:12.551Z"
}
}
"""
let data = json.data(using: .utf8)!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.SSZ"
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let response = try decoder.decode(MaxResponse<AuthResponse>.self, from: data)
print(response)
It is far simpler and less code to just use a struct:
struct AuthResponse: Codable {
struct ResponseData: Codable {
let isLocked: Bool
let __v: Int
let createdAt: Date
}
let status: String?
let data: ResponseData
}
let json = """
{
"status" : "success",
"data" : {
"is_locked" : false,
"__v" : 0,
"created_at" : "2019-04-15T11:57:12.551Z"
}
}
"""
let data = json.data(using: .utf8)!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.SSZ"
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let response = try decoder.decode(AuthResponse.self, from: data)
print(response)
And if you really need the MaxResponse type, then make it a protocol and have your other types conform to it. I'm almost willing to bet that you don't need it though.
In response to your comments, here is a generic solution using structs:
struct LoginResponseData: Codable {
let isLocked: Bool
let __v: Int
let createdAt: Date
}
struct BlogResponseData: Codable {
let xxx: Bool
let yyy: Int
let createdAt: Date
}
struct BaseRresponse<ResponseData: Codable>: Codable {
let status: String?
let data: ResponseData
}

How can I call `didSet` method using `Codable` protocol

How can I call didSet method using Codable protocol.
class Sample: Codable{
var text : String? {
didSet {
print("didSet") // do not call
extended_text = "***" + text! + "***"
}
}
var extended_text : String?
}
let sample_json = "{\"text\":\"sample text\"}"
let decoder = JSONDecoder()
let sample = try! decoder.decode(Sample.self, from: sample_json.data(using: .utf8)!)
print(sample.text!)
print(sample.extended_text ?? "")
Instead of using didSet you should just make extendedText a read only computed property. Note that it is Swift convention to use camelCase instead of snake_case when naming your properties:
struct Sample: Codable {
let text: String
var extendedText: String {
return "***" + text + "***"
}
}
let sampleJson = """
{"text":"sample text"}
"""
do {
let sample = try JSONDecoder().decode(Sample.self, from: Data(sampleJson.utf8))
print(sample.text) // "sample text\n"
print(sample.extendedText) // "***sample text***\n"
} catch {
print(error)
}
An alternative if your goal is to run a method when initializing your Codable struct is to write your own custom decoder:
class Sample: Codable {
let text: String
required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
text = try container.decode(String.self)
print("did set")
}
}
let sampleJson = "{\"text\":\"sample text\"}"
let decoder = JSONDecoder()
do {
let sample = try decoder.decode([String: Sample].self, from: Data(sampleJson.utf8))
print(sample["text"]?.text ?? "")
} catch {
print(error)
}
This will print:
did set
sample text