How to decode a JSON property with different types? [duplicate]2019 Community Moderator ElectionWhat Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?Read JSON object with variable which is int and/or stringHow do you find out the type of an object (in Swift)?Swift: Print variable with different types (enum) from JSON in for-loopSafely turning a JSON string into an objectHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How do I test for an empty JavaScript object?Why does Google prepend while(1); to their JSON responses?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

What options are left, if Britain cannot decide?

Happy pi day, everyone!

Is "upgrade" the right word to use in this context?

Describing a chess game in a novel

Brexit - No Deal Rejection

Could the Saturn V actually have launched astronauts around Venus?

This word with a lot of past tenses

World War I as a war of liberals against authoritarians?

Custom alignment for GeoMarkers

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

Instead of a Universal Basic Income program, why not implement a "Universal Basic Needs" program?

Relationship between sampajanna definitions in SN 47.2 and SN 47.35

Are all passive ability checks floors for active ability checks?

How could a scammer know the apps on my phone / iTunes account?

How do I hide Chekhov's Gun?

Professor being mistaken for a grad student

Math equation in non italic font

Can I use USB data pins as a power source?

Is Manda another name for Saturn (Shani)?

Is it normal that my co-workers at a fitness company criticize my food choices?

Why does overlay work only on the first tcolorbox?

While on vacation my taxi took a longer route, possibly to scam me out of money. How can I deal with this?

Fastest way to pop N items from a large dict

Python if-else code style for reduced code for rounding floats



How to decode a JSON property with different types? [duplicate]



2019 Community Moderator ElectionWhat Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?Read JSON object with variable which is int and/or stringHow do you find out the type of an object (in Swift)?Swift: Print variable with different types (enum) from JSON in for-loopSafely turning a JSON string into an objectHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How do I test for an empty JavaScript object?Why does Google prepend while(1); to their JSON responses?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?










2
















This question already has an answer here:



  • What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?

    1 answer



I have a JSON




"tvShow":
"id": 5348,
"name": "Supernatural",
"permalink": "supernatural",
"url": "http://www.episodate.com/tv-show/supernatural",
"description": "Supernatural is an American fantasy horror television series created by Eric Kripke. It was first broadcast on September 13, 2005, on The WB and subsequently became part of successor The CW's lineup. Starring Jared Padalecki as Sam Winchester and Jensen Ackles as Dean Winchester, the series follows the two brothers as they hunt demons, ghosts, monsters, and other supernatural beings in the world. The series is produced by Warner Bros. Television, in association with Wonderland Sound and Vision. Along with Kripke, executive producers have been McG, Robert Singer, Phil Sgriccia, Sera Gamble, Jeremy Carver, John Shiban, Ben Edlund and Adam Glass. Former executive producer and director Kim Manners died of lung cancer during production of the fourth season.<br>The series is filmed in Vancouver, British Columbia and surrounding areas and was in development for nearly ten years, as creator Kripke spent several years unsuccessfully pitching it. The pilot was viewed by an estimated 5.69 million viewers, and the ratings of the first four episodes prompted The WB to pick up the series for a full season. Originally, Kripke planned the series for three seasons but later expanded it to five. The fifth season concluded the series' main storyline, and Kripke departed the series as showrunner. The series has continued on for several more seasons with Sera Gamble and Jeremy Carver assuming the role of showrunner.",
"description_source": "http://en.wikipedia.org/wiki/Supernatural_(U.S._TV_series)#Spin-off_series",
"start_date": "2005-09-13",
"end_date": null,
"country": "US",
"status": "Running",
"runtime": 60,
"network": "The CW",
"youtube_link": "6ZlnmAWL59I",
"image_path": "https://static.episodate.com/images/tv-show/full/5348.jpg",
"image_thumbnail_path": "https://static.episodate.com/images/tv-show/thumbnail/5348.jpg",
"rating": "9.6747",
"rating_count": "249",
"countdown": null




The value of rating in different serials is Int ("rating": 0) or String ("rating": "9.6747").



I am parsing JSON with Codable/Decodable protocols:



struct DetailModel : Decodable 

var id : Int?
var name : String?
var permalink : String?
var url : String?
var description : String
var description_source : String?
var start_date : String?
var end_date : String?
var country : String?
var status : String?
var runtime : Int?
var network : String?
var youtube_link : String?
var image_path : String?
var image_thumbnail_path : String?
var rating: String
var rating_count : String?
var countdown : String?
var genres : [String]?
var pictures : [String]?
var episodes : [EpisodesModel]?



If rating == String, my code work and I have all the variables from JSON, but if rating == Int, all is nil. What should I do to parse all the types of variable rating at once Int and String?



My decodable function:



 func searchSerialDetail(id: Int, completion: @escaping (DetailModel?) -> Void)

let parameters: [String: Any] = ["q": id]

Alamofire.request(DetailNetworkLayer.url, method: .get, parameters: parameters).response (jsonResponse) in

if let jsonValue = jsonResponse.data
let jsonDecoder = JSONDecoder()
let detail = try? jsonDecoder.decode(DetailModel.self, from: jsonValue)
completion(detail)





Thank you.










share|improve this question















marked as duplicate by Dávid Pásztor, Community May 25 '18 at 12:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Why are almost all of DetailModels properties optional? Only make them optional if they might not be present in the JSON. Moreover, make rating a Double and create a custom init(from:) method as explained in the linked Q&A to handle the conversion from String to Double. Also don't use responseJSON and don't mix JSONSerialization and JSONDecoder, simply take response from Alamofire as Data and parse that directly with JSONDecoder. With your current code you're parsing the JSON twice for no reason.

    – Dávid Pásztor
    May 25 '18 at 10:05












  • No you didn't follow along the answer provided there. You should first go through the whole answer. Then you will understand what to implement for your case. You just scrolled through the answer and picked the first example. But the first example doesn't really solve that problem at all. Read the whole answer.

    – nayem
    May 25 '18 at 11:45











  • thank you guys, I solved this problem in several ways :)

    – alpha corrado
    May 25 '18 at 12:35















2
















This question already has an answer here:



  • What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?

    1 answer



I have a JSON




"tvShow":
"id": 5348,
"name": "Supernatural",
"permalink": "supernatural",
"url": "http://www.episodate.com/tv-show/supernatural",
"description": "Supernatural is an American fantasy horror television series created by Eric Kripke. It was first broadcast on September 13, 2005, on The WB and subsequently became part of successor The CW's lineup. Starring Jared Padalecki as Sam Winchester and Jensen Ackles as Dean Winchester, the series follows the two brothers as they hunt demons, ghosts, monsters, and other supernatural beings in the world. The series is produced by Warner Bros. Television, in association with Wonderland Sound and Vision. Along with Kripke, executive producers have been McG, Robert Singer, Phil Sgriccia, Sera Gamble, Jeremy Carver, John Shiban, Ben Edlund and Adam Glass. Former executive producer and director Kim Manners died of lung cancer during production of the fourth season.<br>The series is filmed in Vancouver, British Columbia and surrounding areas and was in development for nearly ten years, as creator Kripke spent several years unsuccessfully pitching it. The pilot was viewed by an estimated 5.69 million viewers, and the ratings of the first four episodes prompted The WB to pick up the series for a full season. Originally, Kripke planned the series for three seasons but later expanded it to five. The fifth season concluded the series' main storyline, and Kripke departed the series as showrunner. The series has continued on for several more seasons with Sera Gamble and Jeremy Carver assuming the role of showrunner.",
"description_source": "http://en.wikipedia.org/wiki/Supernatural_(U.S._TV_series)#Spin-off_series",
"start_date": "2005-09-13",
"end_date": null,
"country": "US",
"status": "Running",
"runtime": 60,
"network": "The CW",
"youtube_link": "6ZlnmAWL59I",
"image_path": "https://static.episodate.com/images/tv-show/full/5348.jpg",
"image_thumbnail_path": "https://static.episodate.com/images/tv-show/thumbnail/5348.jpg",
"rating": "9.6747",
"rating_count": "249",
"countdown": null




The value of rating in different serials is Int ("rating": 0) or String ("rating": "9.6747").



I am parsing JSON with Codable/Decodable protocols:



struct DetailModel : Decodable 

var id : Int?
var name : String?
var permalink : String?
var url : String?
var description : String
var description_source : String?
var start_date : String?
var end_date : String?
var country : String?
var status : String?
var runtime : Int?
var network : String?
var youtube_link : String?
var image_path : String?
var image_thumbnail_path : String?
var rating: String
var rating_count : String?
var countdown : String?
var genres : [String]?
var pictures : [String]?
var episodes : [EpisodesModel]?



If rating == String, my code work and I have all the variables from JSON, but if rating == Int, all is nil. What should I do to parse all the types of variable rating at once Int and String?



My decodable function:



 func searchSerialDetail(id: Int, completion: @escaping (DetailModel?) -> Void)

let parameters: [String: Any] = ["q": id]

Alamofire.request(DetailNetworkLayer.url, method: .get, parameters: parameters).response (jsonResponse) in

if let jsonValue = jsonResponse.data
let jsonDecoder = JSONDecoder()
let detail = try? jsonDecoder.decode(DetailModel.self, from: jsonValue)
completion(detail)





Thank you.










share|improve this question















marked as duplicate by Dávid Pásztor, Community May 25 '18 at 12:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Why are almost all of DetailModels properties optional? Only make them optional if they might not be present in the JSON. Moreover, make rating a Double and create a custom init(from:) method as explained in the linked Q&A to handle the conversion from String to Double. Also don't use responseJSON and don't mix JSONSerialization and JSONDecoder, simply take response from Alamofire as Data and parse that directly with JSONDecoder. With your current code you're parsing the JSON twice for no reason.

    – Dávid Pásztor
    May 25 '18 at 10:05












  • No you didn't follow along the answer provided there. You should first go through the whole answer. Then you will understand what to implement for your case. You just scrolled through the answer and picked the first example. But the first example doesn't really solve that problem at all. Read the whole answer.

    – nayem
    May 25 '18 at 11:45











  • thank you guys, I solved this problem in several ways :)

    – alpha corrado
    May 25 '18 at 12:35













2












2








2


1







This question already has an answer here:



  • What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?

    1 answer



I have a JSON




"tvShow":
"id": 5348,
"name": "Supernatural",
"permalink": "supernatural",
"url": "http://www.episodate.com/tv-show/supernatural",
"description": "Supernatural is an American fantasy horror television series created by Eric Kripke. It was first broadcast on September 13, 2005, on The WB and subsequently became part of successor The CW's lineup. Starring Jared Padalecki as Sam Winchester and Jensen Ackles as Dean Winchester, the series follows the two brothers as they hunt demons, ghosts, monsters, and other supernatural beings in the world. The series is produced by Warner Bros. Television, in association with Wonderland Sound and Vision. Along with Kripke, executive producers have been McG, Robert Singer, Phil Sgriccia, Sera Gamble, Jeremy Carver, John Shiban, Ben Edlund and Adam Glass. Former executive producer and director Kim Manners died of lung cancer during production of the fourth season.<br>The series is filmed in Vancouver, British Columbia and surrounding areas and was in development for nearly ten years, as creator Kripke spent several years unsuccessfully pitching it. The pilot was viewed by an estimated 5.69 million viewers, and the ratings of the first four episodes prompted The WB to pick up the series for a full season. Originally, Kripke planned the series for three seasons but later expanded it to five. The fifth season concluded the series' main storyline, and Kripke departed the series as showrunner. The series has continued on for several more seasons with Sera Gamble and Jeremy Carver assuming the role of showrunner.",
"description_source": "http://en.wikipedia.org/wiki/Supernatural_(U.S._TV_series)#Spin-off_series",
"start_date": "2005-09-13",
"end_date": null,
"country": "US",
"status": "Running",
"runtime": 60,
"network": "The CW",
"youtube_link": "6ZlnmAWL59I",
"image_path": "https://static.episodate.com/images/tv-show/full/5348.jpg",
"image_thumbnail_path": "https://static.episodate.com/images/tv-show/thumbnail/5348.jpg",
"rating": "9.6747",
"rating_count": "249",
"countdown": null




The value of rating in different serials is Int ("rating": 0) or String ("rating": "9.6747").



I am parsing JSON with Codable/Decodable protocols:



struct DetailModel : Decodable 

var id : Int?
var name : String?
var permalink : String?
var url : String?
var description : String
var description_source : String?
var start_date : String?
var end_date : String?
var country : String?
var status : String?
var runtime : Int?
var network : String?
var youtube_link : String?
var image_path : String?
var image_thumbnail_path : String?
var rating: String
var rating_count : String?
var countdown : String?
var genres : [String]?
var pictures : [String]?
var episodes : [EpisodesModel]?



If rating == String, my code work and I have all the variables from JSON, but if rating == Int, all is nil. What should I do to parse all the types of variable rating at once Int and String?



My decodable function:



 func searchSerialDetail(id: Int, completion: @escaping (DetailModel?) -> Void)

let parameters: [String: Any] = ["q": id]

Alamofire.request(DetailNetworkLayer.url, method: .get, parameters: parameters).response (jsonResponse) in

if let jsonValue = jsonResponse.data
let jsonDecoder = JSONDecoder()
let detail = try? jsonDecoder.decode(DetailModel.self, from: jsonValue)
completion(detail)





Thank you.










share|improve this question

















This question already has an answer here:



  • What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?

    1 answer



I have a JSON




"tvShow":
"id": 5348,
"name": "Supernatural",
"permalink": "supernatural",
"url": "http://www.episodate.com/tv-show/supernatural",
"description": "Supernatural is an American fantasy horror television series created by Eric Kripke. It was first broadcast on September 13, 2005, on The WB and subsequently became part of successor The CW's lineup. Starring Jared Padalecki as Sam Winchester and Jensen Ackles as Dean Winchester, the series follows the two brothers as they hunt demons, ghosts, monsters, and other supernatural beings in the world. The series is produced by Warner Bros. Television, in association with Wonderland Sound and Vision. Along with Kripke, executive producers have been McG, Robert Singer, Phil Sgriccia, Sera Gamble, Jeremy Carver, John Shiban, Ben Edlund and Adam Glass. Former executive producer and director Kim Manners died of lung cancer during production of the fourth season.<br>The series is filmed in Vancouver, British Columbia and surrounding areas and was in development for nearly ten years, as creator Kripke spent several years unsuccessfully pitching it. The pilot was viewed by an estimated 5.69 million viewers, and the ratings of the first four episodes prompted The WB to pick up the series for a full season. Originally, Kripke planned the series for three seasons but later expanded it to five. The fifth season concluded the series' main storyline, and Kripke departed the series as showrunner. The series has continued on for several more seasons with Sera Gamble and Jeremy Carver assuming the role of showrunner.",
"description_source": "http://en.wikipedia.org/wiki/Supernatural_(U.S._TV_series)#Spin-off_series",
"start_date": "2005-09-13",
"end_date": null,
"country": "US",
"status": "Running",
"runtime": 60,
"network": "The CW",
"youtube_link": "6ZlnmAWL59I",
"image_path": "https://static.episodate.com/images/tv-show/full/5348.jpg",
"image_thumbnail_path": "https://static.episodate.com/images/tv-show/thumbnail/5348.jpg",
"rating": "9.6747",
"rating_count": "249",
"countdown": null




The value of rating in different serials is Int ("rating": 0) or String ("rating": "9.6747").



I am parsing JSON with Codable/Decodable protocols:



struct DetailModel : Decodable 

var id : Int?
var name : String?
var permalink : String?
var url : String?
var description : String
var description_source : String?
var start_date : String?
var end_date : String?
var country : String?
var status : String?
var runtime : Int?
var network : String?
var youtube_link : String?
var image_path : String?
var image_thumbnail_path : String?
var rating: String
var rating_count : String?
var countdown : String?
var genres : [String]?
var pictures : [String]?
var episodes : [EpisodesModel]?



If rating == String, my code work and I have all the variables from JSON, but if rating == Int, all is nil. What should I do to parse all the types of variable rating at once Int and String?



My decodable function:



 func searchSerialDetail(id: Int, completion: @escaping (DetailModel?) -> Void)

let parameters: [String: Any] = ["q": id]

Alamofire.request(DetailNetworkLayer.url, method: .get, parameters: parameters).response (jsonResponse) in

if let jsonValue = jsonResponse.data
let jsonDecoder = JSONDecoder()
let detail = try? jsonDecoder.decode(DetailModel.self, from: jsonValue)
completion(detail)





Thank you.





This question already has an answer here:



  • What Is Preventing My Conversion From String to Int When Decoding Using Swift 4’s Codable?

    1 answer







json swift alamofire swift4 decodable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 25 '18 at 12:57







alpha corrado

















asked May 25 '18 at 9:44









alpha corradoalpha corrado

135




135




marked as duplicate by Dávid Pásztor, Community May 25 '18 at 12:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Dávid Pásztor, Community May 25 '18 at 12:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    Why are almost all of DetailModels properties optional? Only make them optional if they might not be present in the JSON. Moreover, make rating a Double and create a custom init(from:) method as explained in the linked Q&A to handle the conversion from String to Double. Also don't use responseJSON and don't mix JSONSerialization and JSONDecoder, simply take response from Alamofire as Data and parse that directly with JSONDecoder. With your current code you're parsing the JSON twice for no reason.

    – Dávid Pásztor
    May 25 '18 at 10:05












  • No you didn't follow along the answer provided there. You should first go through the whole answer. Then you will understand what to implement for your case. You just scrolled through the answer and picked the first example. But the first example doesn't really solve that problem at all. Read the whole answer.

    – nayem
    May 25 '18 at 11:45











  • thank you guys, I solved this problem in several ways :)

    – alpha corrado
    May 25 '18 at 12:35












  • 1





    Why are almost all of DetailModels properties optional? Only make them optional if they might not be present in the JSON. Moreover, make rating a Double and create a custom init(from:) method as explained in the linked Q&A to handle the conversion from String to Double. Also don't use responseJSON and don't mix JSONSerialization and JSONDecoder, simply take response from Alamofire as Data and parse that directly with JSONDecoder. With your current code you're parsing the JSON twice for no reason.

    – Dávid Pásztor
    May 25 '18 at 10:05












  • No you didn't follow along the answer provided there. You should first go through the whole answer. Then you will understand what to implement for your case. You just scrolled through the answer and picked the first example. But the first example doesn't really solve that problem at all. Read the whole answer.

    – nayem
    May 25 '18 at 11:45











  • thank you guys, I solved this problem in several ways :)

    – alpha corrado
    May 25 '18 at 12:35







1




1





Why are almost all of DetailModels properties optional? Only make them optional if they might not be present in the JSON. Moreover, make rating a Double and create a custom init(from:) method as explained in the linked Q&A to handle the conversion from String to Double. Also don't use responseJSON and don't mix JSONSerialization and JSONDecoder, simply take response from Alamofire as Data and parse that directly with JSONDecoder. With your current code you're parsing the JSON twice for no reason.

– Dávid Pásztor
May 25 '18 at 10:05






Why are almost all of DetailModels properties optional? Only make them optional if they might not be present in the JSON. Moreover, make rating a Double and create a custom init(from:) method as explained in the linked Q&A to handle the conversion from String to Double. Also don't use responseJSON and don't mix JSONSerialization and JSONDecoder, simply take response from Alamofire as Data and parse that directly with JSONDecoder. With your current code you're parsing the JSON twice for no reason.

– Dávid Pásztor
May 25 '18 at 10:05














No you didn't follow along the answer provided there. You should first go through the whole answer. Then you will understand what to implement for your case. You just scrolled through the answer and picked the first example. But the first example doesn't really solve that problem at all. Read the whole answer.

– nayem
May 25 '18 at 11:45





No you didn't follow along the answer provided there. You should first go through the whole answer. Then you will understand what to implement for your case. You just scrolled through the answer and picked the first example. But the first example doesn't really solve that problem at all. Read the whole answer.

– nayem
May 25 '18 at 11:45













thank you guys, I solved this problem in several ways :)

– alpha corrado
May 25 '18 at 12:35





thank you guys, I solved this problem in several ways :)

– alpha corrado
May 25 '18 at 12:35












1 Answer
1






active

oldest

votes


















3














You will have to implement your own func encode(to encoder: Encoder) throws and init(from decoder: Decoder) throws which are both properties of the Codable protocol. Then change your rating variable into an enum



Which would look like this:



enum Rating: Codable 
case int(Int)
case string(String)

func encode(to encoder: Encoder) throws
var container = encoder.singleValueContainer()
switch self
case .int(let v): try container.encode(v)
case .string(let v): try container.encode(v)



init(from decoder: Decoder) throws
let value = try decoder.singleValueContainer()

if let v = try? value.decode(Int.self)
self = .int(v)
return
else if let v = try? value.decode(String.self)
self = .string(v)
return


throw Rating.ParseError.notRecognizedType(value)


enum ParseError: Error
case notRecognizedType(Any)




Then on your DetailModel just change rating: String to rating: Rating



This works, I have tested with these JSON strings.



// int rating

"rating": 200,
"bro": "Success"


// string rating

"rating": "200",
"bro": "Success"



Edit: I've found a better swiftier way of implementing init(from decoder: Decoder) throws, which produces a better error message, by using this you can now omit the ParseError enum.



init(from decoder: Decoder) throws 
let value = try decoder.singleValueContainer()
do
self = .int(try value.decode(Int.self))
catch DecodingError.typeMismatch
self = .string(try value.decode(String.self))







share|improve this answer

























  • Thank you, that solved my problem!!!

    – alpha corrado
    May 25 '18 at 11:19











  • Upvotes for this guy :D

    – alpha corrado
    May 25 '18 at 11:19

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You will have to implement your own func encode(to encoder: Encoder) throws and init(from decoder: Decoder) throws which are both properties of the Codable protocol. Then change your rating variable into an enum



Which would look like this:



enum Rating: Codable 
case int(Int)
case string(String)

func encode(to encoder: Encoder) throws
var container = encoder.singleValueContainer()
switch self
case .int(let v): try container.encode(v)
case .string(let v): try container.encode(v)



init(from decoder: Decoder) throws
let value = try decoder.singleValueContainer()

if let v = try? value.decode(Int.self)
self = .int(v)
return
else if let v = try? value.decode(String.self)
self = .string(v)
return


throw Rating.ParseError.notRecognizedType(value)


enum ParseError: Error
case notRecognizedType(Any)




Then on your DetailModel just change rating: String to rating: Rating



This works, I have tested with these JSON strings.



// int rating

"rating": 200,
"bro": "Success"


// string rating

"rating": "200",
"bro": "Success"



Edit: I've found a better swiftier way of implementing init(from decoder: Decoder) throws, which produces a better error message, by using this you can now omit the ParseError enum.



init(from decoder: Decoder) throws 
let value = try decoder.singleValueContainer()
do
self = .int(try value.decode(Int.self))
catch DecodingError.typeMismatch
self = .string(try value.decode(String.self))







share|improve this answer

























  • Thank you, that solved my problem!!!

    – alpha corrado
    May 25 '18 at 11:19











  • Upvotes for this guy :D

    – alpha corrado
    May 25 '18 at 11:19















3














You will have to implement your own func encode(to encoder: Encoder) throws and init(from decoder: Decoder) throws which are both properties of the Codable protocol. Then change your rating variable into an enum



Which would look like this:



enum Rating: Codable 
case int(Int)
case string(String)

func encode(to encoder: Encoder) throws
var container = encoder.singleValueContainer()
switch self
case .int(let v): try container.encode(v)
case .string(let v): try container.encode(v)



init(from decoder: Decoder) throws
let value = try decoder.singleValueContainer()

if let v = try? value.decode(Int.self)
self = .int(v)
return
else if let v = try? value.decode(String.self)
self = .string(v)
return


throw Rating.ParseError.notRecognizedType(value)


enum ParseError: Error
case notRecognizedType(Any)




Then on your DetailModel just change rating: String to rating: Rating



This works, I have tested with these JSON strings.



// int rating

"rating": 200,
"bro": "Success"


// string rating

"rating": "200",
"bro": "Success"



Edit: I've found a better swiftier way of implementing init(from decoder: Decoder) throws, which produces a better error message, by using this you can now omit the ParseError enum.



init(from decoder: Decoder) throws 
let value = try decoder.singleValueContainer()
do
self = .int(try value.decode(Int.self))
catch DecodingError.typeMismatch
self = .string(try value.decode(String.self))







share|improve this answer

























  • Thank you, that solved my problem!!!

    – alpha corrado
    May 25 '18 at 11:19











  • Upvotes for this guy :D

    – alpha corrado
    May 25 '18 at 11:19













3












3








3







You will have to implement your own func encode(to encoder: Encoder) throws and init(from decoder: Decoder) throws which are both properties of the Codable protocol. Then change your rating variable into an enum



Which would look like this:



enum Rating: Codable 
case int(Int)
case string(String)

func encode(to encoder: Encoder) throws
var container = encoder.singleValueContainer()
switch self
case .int(let v): try container.encode(v)
case .string(let v): try container.encode(v)



init(from decoder: Decoder) throws
let value = try decoder.singleValueContainer()

if let v = try? value.decode(Int.self)
self = .int(v)
return
else if let v = try? value.decode(String.self)
self = .string(v)
return


throw Rating.ParseError.notRecognizedType(value)


enum ParseError: Error
case notRecognizedType(Any)




Then on your DetailModel just change rating: String to rating: Rating



This works, I have tested with these JSON strings.



// int rating

"rating": 200,
"bro": "Success"


// string rating

"rating": "200",
"bro": "Success"



Edit: I've found a better swiftier way of implementing init(from decoder: Decoder) throws, which produces a better error message, by using this you can now omit the ParseError enum.



init(from decoder: Decoder) throws 
let value = try decoder.singleValueContainer()
do
self = .int(try value.decode(Int.self))
catch DecodingError.typeMismatch
self = .string(try value.decode(String.self))







share|improve this answer















You will have to implement your own func encode(to encoder: Encoder) throws and init(from decoder: Decoder) throws which are both properties of the Codable protocol. Then change your rating variable into an enum



Which would look like this:



enum Rating: Codable 
case int(Int)
case string(String)

func encode(to encoder: Encoder) throws
var container = encoder.singleValueContainer()
switch self
case .int(let v): try container.encode(v)
case .string(let v): try container.encode(v)



init(from decoder: Decoder) throws
let value = try decoder.singleValueContainer()

if let v = try? value.decode(Int.self)
self = .int(v)
return
else if let v = try? value.decode(String.self)
self = .string(v)
return


throw Rating.ParseError.notRecognizedType(value)


enum ParseError: Error
case notRecognizedType(Any)




Then on your DetailModel just change rating: String to rating: Rating



This works, I have tested with these JSON strings.



// int rating

"rating": 200,
"bro": "Success"


// string rating

"rating": "200",
"bro": "Success"



Edit: I've found a better swiftier way of implementing init(from decoder: Decoder) throws, which produces a better error message, by using this you can now omit the ParseError enum.



init(from decoder: Decoder) throws 
let value = try decoder.singleValueContainer()
do
self = .int(try value.decode(Int.self))
catch DecodingError.typeMismatch
self = .string(try value.decode(String.self))








share|improve this answer














share|improve this answer



share|improve this answer








edited May 26 '18 at 9:36

























answered May 25 '18 at 10:43









Zonily JameZonily Jame

1,65611129




1,65611129












  • Thank you, that solved my problem!!!

    – alpha corrado
    May 25 '18 at 11:19











  • Upvotes for this guy :D

    – alpha corrado
    May 25 '18 at 11:19

















  • Thank you, that solved my problem!!!

    – alpha corrado
    May 25 '18 at 11:19











  • Upvotes for this guy :D

    – alpha corrado
    May 25 '18 at 11:19
















Thank you, that solved my problem!!!

– alpha corrado
May 25 '18 at 11:19





Thank you, that solved my problem!!!

– alpha corrado
May 25 '18 at 11:19













Upvotes for this guy :D

– alpha corrado
May 25 '18 at 11:19





Upvotes for this guy :D

– alpha corrado
May 25 '18 at 11:19





Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived