I have a struct like bellow:
struct MyStruct: Codable {
var id: Int?
}
and the JSON that i was receiving from server was like this:
{
"id": 12345
}
But now server-side decided to send all numbers as quoted numbers like this:
{
"id": "12345"
}
When decoding this json using JSONDecoder().decode i got an error,
The data couldn’t be read because it isn’t in the correct format
Is there any way, (except writing custom Encodable and Decodable implementation for every struct that i created up to now) to solve this problem? For example doing something on JSONDecoder()
You can do it by implementing Decodable protocol's required initializer, init(from:):
extension MyStruct {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let idString = try values.decode(String.self, forKey: .id)
id = Int(idString)
}
}
And don't forget to decode values of other properties.
Related
In my custom initializer I'd like to decode a dictionary from JSON and then assign its values to properties in the class. To my surprise, compiler does not allow me to decode the dictionary, I get the error:
Value of protocol type 'Any' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols
If I try to decode dictionary of type [String: Decodable] the error message says:
Value of protocol type 'Decodable' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols
My initializer looks like this:
public let total: Int
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
...
if let dict = try container.decodeIfPresent([String: Any].self, forKey: .tracks),
let value = dict["total"] as? Int { // Error is displayed at this line
total = value
} else {
total = 0
}
...
}
When I looked for the answer I found this answer and according to it the code above should not cause any problems.
What you are looking for is nestedContainer. It helps you "skip" a hierarchy level in your JSON. Ie: let's imagine that in your code, it's all in the same level (one struct), but in the JSON, it's not, there is a sub dictionary.
For test purpose, your JSON might look like:
{
"title": "normal",
"tracks": {
"name": "myName",
"total": 3
}
}
If we want in our model:
struct TestStruct: Codable {
let title: String
let name: String
let total: Int
}
We need to use nestedContainer(keyedBy: forKey:):
extension TestStruct {
enum TopLevelKeys: String, CodingKey {
case title
case tracks
}
enum NestedLevelCodingKeys: String, CodingKey {
case name
case total
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: TopLevelKeys.self)
self.title = try container.decode(String.self, forKey: .title)
let subcontainer = try container.nestedContainer(keyedBy: NestedLevelCodingKeys.self, forKey: TopLevelKeys.tracks)
self.name = try subcontainer.decode(String.self, forKey: .name)
self.total = try subcontainer.decode(Int.self, forKey: .total) //You can use here a `decodeIfPresent()` if needed, use default values, etc.
}
}
In your sample, you used decodeIfPresent() for the subdictionary. It's unclear if it was for test purpose, or if the sub dictionary was sometimes not present.
If that's the case and you can have a JSON like this:
{
"title": "normal"
}
Then, before calling nestedContainer(keyedBy: forKey:), use if container.contains(TopLevelKeys.tracks) {} and set default values if needed in the else case.
I'm trying to parse a json that looks like this using decodable:
{
"count": 1,
"results": [
{
"title": 1,
"content": "Bla"
} ]
}
My problem is that I don't want to make a class that has a count property just to be able to use the decoder. I want to parse only the results part I don't care about the count.
So my question is, can decodable.decode somehow only parse a part of the result json. I mean a certain key path instead of the whole json ? And I want to do it using Decodable.
In a nutshell I don't want this:
class IncidentWrapper: Codable{
var count: Int
var incident: [Incident]
}
What I would Imagine is to have this:
decodable.decode([Incident].self, from: response.data, forKey: "results")
Thanks
let me see what I can suggest:
struct Result: Codeable {
var id: Int
var message: String
var color: String
var type: String
enum CodingKeys: String, CodingKey {
case results
}
enum NestedResultKeys: String, CodingKey {
case id, message, color, type
}
}
extension Result: Decodable {
init(from decoder: Decoder) throws {
let result = try decoder.container(keyedBy: CodingKeys.self)
let nestedResult = try result.nestedContainer(keyedBy: NestedResultKeys.self, forKey: .result)
id = try nestedResult.decode(Int.self, forKey: .id)
message = try nestedResult.decode(String.self, forKey: .message)
color = try nestedResult.decode(String.self, forKey: .color)
type = try nestedResult.decode(String.self, forKey: .id)
}
}
See this documentation for more insight
https://developer.apple.com/documentation/swift/swift_standard_library/encoding_decoding_and_serialization
Hope it helps your project!
You probably is looking for JSONSerialization class. This is an example how it works:
if let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] {
if let results = json["results"] as? [Incident] {
print(results.count)
}
}
You can define a generic wrapper for once and use everywhere.
It will work as a generic wrapper for results key only.
protocol ResultsDecodable: Decodable{
associatedtype T: Decodable
var results: [T] {get}
}
struct Result<Element: Decodable>: ResultsDecodable{
typealias T = Element
var results: [Element]
}
Extend JSONDecoder to get results output.
extension JSONDecoder {
func resultDecode<T>(_ type: Result<T>.Type, from data: Data) throws -> [T] where T : Decodable{
let model = try! decode(type, from: data)
return model.results
}
}
And use like this
var str = #"{"count": 1,"results": [{"title": 1,"content": "Bla"}, {"title": 2,"content": "Bla"} ]}"#
class Incident: Decodable{
let title: Int
let content: String
}
let indicents = (try! JSONDecoder().resultDecode(Result<Incident>.self, from: str.data(using: .utf8)!))
See how it makes everything more complex. BETTER USE IncidentWrapper!!!
You only need to use the keys you care about. Just leave off the count. Don't make it part of your struct.
You will only get errors if you can't find a key in the json that you are expecting in the struct. You can avoid this too, though, if you make it an optional in the struct.
I have the following json string:
{"weight":[{"bmi":24.75,"date":"2020-01-20","logId":1000,"source":"API","time":"23:59:59","weight":200}]}
I want to convert it to a Swift object in order to access the different values. Here is what I am trying to do, I have these structs setup:
struct FitbitResponseModel: Decodable {
let weight: [FitbitResponseData]
}
struct FitbitResponseData: Decodable {
let bmi: Int
let date: String
let logId: Int
let source: String
let time: String
let weight: Int
}
And then I have this method to decode the json string:
func parseJSON(data: Data) -> FitbitResponseModel? {
var returnValue: FitbitResponseModel?
do {
returnValue = try JSONDecoder().decode(FitbitResponseModel.self, from: data)
} catch {
print("Error took place: \(error.localizedDescription).")
}
return returnValue
}
However when I try to run it I get the error that the data couldn’t be read because it isn’t in the correct format. What am I doing wrong? Any help is appreciated.
Thanks in advance!
change
let bmi: Int
to
let bmi: Double
beacuse it's value is coming out to be 24.75 in your response if any variable type doesn't match to JSON response whole model wouldn't map in Codable protocol (Encodable and Decodable)
Talk to your API developer. 000 is not a valid representation of a number for json. It needs to be either 0 or 0.0. You can lint your json at https://jsonlint.com . If you really need to work around this I suggest doing a string replacement on 000, with 0, before you parse the data.
Json is n't valid because logId value in your json is n't valid.
{
"weight": [{
"bmi": 24.75,
"date": "2020-01-20",
"logId": 100,
"source": "API",
"time": "23:59:59",
"weight": 200
}]
}
One really neat feature of this auto-generated conformance is that if you define an enum in your type called "CodingKeys" (or use a type alias with this name) that conforms to the CodingKey protocol – Swift will automatically use this as the key type. This therefore allows you to easily customise the keys that your properties are encoded/decoded with.
struct Base: Codable {
let weight : [Weight]?
enum CodingKeys: String, CodingKey {
case weight = "weight"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
weight = try values.decodeIfPresent([Weight].self, forKey: .weight)
}
}
struct Weight : Codable {
let bmi : Double?
let date : String?
let logId : Int?
let source : String?
let time : String?
let weight : Int?
enum CodingKeys: String, CodingKey {
case bmi = "bmi"
case date = "date"
case logId = "logId"
case source = "source"
case time = "time"
case weight = "weight"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
bmi = try values.decodeIfPresent(Double.self, forKey: .bmi)
date = try values.decodeIfPresent(String.self, forKey: .date)
logId = try values.decodeIfPresent(Int.self, forKey: .logId)
source = try values.decodeIfPresent(String.self, forKey: .source)
time = try values.decodeIfPresent(String.self, forKey: .time)
weight = try values.decodeIfPresent(Int.self, forKey: .weight)
}
}
Hope that will help!
or you can use SwiftyJSON lib: https://github.com/SwiftyJSON/SwiftyJSON
I am trying to transform "Object Mapper" to "Codable". My response which comes from service includes NSArray which includes custom objects. I need to use NSArray and NSDictionary in Codable Class. But, i failed.I tried to use third party library like AnyCodable, but, i failed again. I can't change the response on server side. It must come as Array. I must use Array. Do you have any suggestion or information?
class Person : Codable { //Error 1
var name: String?
var data: NSArray?
private enum CodingKeys : String, CodingKey {
case name
case data
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(data, forKey: .data) //Error 2
}
func decode(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.data = try container.decode(NSArray.self, forKey: .data) //Error 3
}
}
Error 1: "Type 'Person' does not conform to protocol 'Decodable'"
Error 2: "Argument type 'NSArray' does not conform to expected type 'Encodable'"
Error 3: "Instance method 'decode(_:forKey:)' requires that 'NSArray' conform to 'Decodable'"
Sample response is here.All items in the array don't have the same contents. Every item in array has different type.
{
"data": [
{
"languageCode": "EN",
"deviceInformation": {
"screenSize": "height:812.0 width:375.0",
"connectionType": "wifi",
"deviceType": "iPhone",
"deviceCode": "D01D304C-D05C-4443-9A92-031C55D14XC7",
"operatingSystemVersion": "12.2",
"applicationVersion": "1.0"
},
"lastUpdatedParamDate": "13.05.2019 14:44:24",
"skipOptionalUpdate": 0
}
]
}
When you make it
var data: NSArray?
it's a bridged objective - c type that have Any as the type of elements and Any doesn't conform to Codable so explicitly make it the type like
var data:[SomeModel]
or use JSONSerialization instead
struct Root: Codable {
let data: [Model]
}
struct Model: Codable {
let languageCode: String
let deviceInformation: DeviceInformation
let lastUpdatedParamDate: String
let skipOptionalUpdate: Int
}
struct DeviceInformation: Codable {
let screenSize, connectionType, deviceType, deviceCode: String
let operatingSystemVersion, applicationVersion: String
}
let eventData = try JSONDecoder().decode(Root.self, from: data)
if you have more attributes you can add them , if some returns nil in some cases make them optional , but if the type changes then no way with Codable
I have a struct that parse JSON using Codable.
struct Student: Codable {
let name: String?
let amount: Double?
let adress: String?
}
Now if the amount value is coming as null the JSON parsing is failing.
So should I manually handle the null cases for all the Int and Double that are present in the Student struct?
The String values coming as null is automatically handled.
Let me do this Playground for you since an example shows you more than a hundred words:
import Cocoa
struct Student: Codable {
let name: String?
let amount: Double?
let adress: String?
}
let okData = """
{
"name": "here",
"amount": 100.0,
"adress": "woodpecker avenue 1"
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
let okStudent = try decoder.decode(Student.self, from:okData)
print(okStudent)
let nullData = """
{
"name": "there",
"amount": null,
"adress": "grassland 2"
}
""".data(using: .utf8)!
let nullStudent = try decoder.decode(Student.self, from:nullData)
print(nullStudent)
null is handled just fine if you define your structs using optionals. I would however advise against it if you can avoid it. Swift provides the best support I know to help me not forgetting to handle nil cases wherever they may occur, but they are still a pain in the ass.
Was browsing through Codable and got this issue.
So to be very clear here it is,
If the JSON/response would contain null as the value, its interpreted as nil. And hence for that reason one of the model's property which might contain null should be marked as optional.
For Example, consider the below JSON response,
{
"name": "Steve",
"amount": null,
"address": "India"
}
The model should be as below, cos amount is returning null.
struct Student: Codable {
let name: String
let amount: Double?
let address: String
}
Suggestion: In case, if you would write a init(from decoder: Decoder) throws, always use something like below, for optional properties.
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
amount = try values.decodeIfPresent(String.self, forKey: .amount)
//so on...
}
Even if you add do-catch block with try? decoder.... it can be captured if fails. Hope thats clear!! Its simple but very difficult to find the issue even if the model is containing 5 or more properties with some containing null values