Here's my code:
class LoginUserResponse : Codable {
var result: String = ""
var data: LoginUserResponseData?
var mess: [String] = []
}
public class LoginUserResponseData : Codable {
var userId = "0"
var name = ""
}
Now, calling the server API I'm parsing response like this (using Stuff library to simplify parsing):
do {
let loginUserResponse = try LoginUserResponse(json: string)
} catch let error {
print(error)
}
When I enter the correct password I'm getting an answer like this:
{"result":"success","data":{"userId":"10","name":"Foo"},"mess":["You're logged in"]}
This is fine, the parser is working correctly.
While providing wrong password gives the following answer:
{"result":"error","data":{},"mess":["Wrong password"]}
In this situation, the parser is failing. It should set data to nil, but instead, it tries to decode it to the LoginUserResponseData object.
I'm using the same approach on Android using retrofit and it works fine. I rather don't want to make all fields as optional.
Is there a way to make parser treat empty json {} as nil? Or make LoginUserResponseData as non-optional and it'll just have default values? I know I can create a custom parser for this, but I have tons of requests like this and it'll require too much additional work.