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
Related
An API I deal with returns either:
{
"title": "Hello World"
}
or
{
"title": [{
"text": "Hello World"
}]
}
My idea would be to have a struct TitleStringValue which has a custom decoder, like this:
struct TitleStringValue: Decodable {
let text: String
struct TitleStringValueInner: Decodable {
let text: String
}
init(from decoder: Decoder) throws {
if let stringContainer = try? decoder.singleValueContainer() {
text = try stringContainer.decode(String.self)
} else {
var arrayContainer = try decoder.unkeyedContainer()
text = try arrayContainer.decode(TitleStringValueInner.self).text
while !arrayContainer.isAtEnd {
_ = try? arrayContainer.decode(TitleStringValueInner.self)
}
}
}
}
struct MyResult: Decodable {
let title: TitleStringValue
}
But in the case where title is composed of an array, the TitleStringValue init(from decoder: Decoder) is never called as the decoder fails before, encountering an array where it expects a single value type.
Is there a way to solve this?
I could implement decode on the level of my MyResult struct, but that means that each struct that has TitleStringValues needs a custom decoder, so I would much rather implement it on TitleStringValue-level.
The issue here is that singleValueContainer can be used to decode also an array. So, the error that you are getting is produced by the second try inside init(from:) function of TitleStringValue and not before.
Having said that, you can further simplify your custom decoding like this:
struct TitleStringValue: Decodable {
let text: String
struct TitleStringValueInner: Decodable {
let text: String
}
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let string = try? container.decode([TitleStringValueInner].self).first?.text {
text = string
} else {
text = try container.decode(String.self)
}
}
}
My suggestion is to keep the title property for both cases.
First try to decode a String. If this fails decode an array of Text (I kept the names simple) get the first item und assign the text value to title.
struct Title: Decodable {
struct Text : Decodable { let text : String }
let title : String
private enum CodingKeys : String, CodingKey { case title }
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
title = try container.decode(String.self, forKey: .title)
} catch DecodingError.typeMismatch {
let textArray = try container.decode([Text].self, forKey: .title)
title = textArray.first?.text ?? ""
}
}
}
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)
}
I want to parse JSON data into a struct object but i can't do it.
Actually the code is in different files but here i'm posting it as a whole.
Here is my code :
import Foundation
struct dataResponse: Decodable {
var results: [userData]
init(from decoder: Decoder) throws {
var results = [userData] ()
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
if let route = try? container.decode(userData.self) {
results.append(route)
}
else {
_ = try? container.decode(dummyData.self)
}
}
self.results = results
}
}
private struct dummyData: Decodable { }
enum dataError: Error {
case dataUnavailable
case cannotProcessData
}
struct userData: Codable {
var avatar: String
var city: String
var contribution: Int
var country: String
var friendOfCount: Int
var handle: String
var lastOnlineTimeSeconds: Int
var maxRank: String
var maxRating: Int
var organization: String
var rank: String
var rating: Int
var registrationTimeSeconds: Int
var titlePhoto: String
}
struct dataRequest {
let requestUrl: URL
init(){
self.requestUrl = URL(string: "https://codeforces.com/api/user.info?handles=abhijeet_ar")!
}
func getData(completionHandler: #escaping(Result<[userData], dataError>) -> Void) {
URLSession.shared.dataTask(with: self.requestUrl) { (data,response, error) in
guard let data = data else {
completionHandler(.failure(.dataUnavailable))
print("-------bye-bye--------")
return
}
do {
print("-------entered--------")
// let dataresponse = try JSONDecoder().decode([userData].self, from: data)
// print(type(of: dataresponse))
// completionHandler(.success(dataresponse))
let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(jsonResult)
completionHandler(.success(jsonResult as! [userData]))
}
catch {
completionHandler(.failure(.cannotProcessData))
}
}.resume()
}
}
here userData is my struct
the error says : Could not cast value of type '__NSDictionaryM' (0x7fff8fe2dab0) to 'NSArray' (0x7fff8fe2dd30).
I would appreciate if anyone helps, thanks.
You are making a very common mistake.
You are ignoring the root object which is a dictionary and causes the error.
struct Root: Decodable {
let status : String
let result: [UserData]
}
struct UserData: Decodable {
let avatar: String
let city: String
let contribution: Int
let country: String
let friendOfCount: Int
let handle: String
let lastOnlineTimeSeconds: Int
let maxRank: String
let maxRating: Int
let organization: String
let rank: String
let rating: Int
let registrationTimeSeconds: Int
let titlePhoto: String
}
Forget JSONSerialization and use only JSONDecoder
And it's not a good idea to return meaningless enumerated errors. Use Error and return the real error.
You get the array with dataresponse.result
struct DataRequest { // name structs always with starting capital letter
let requestUrl: URL
init(){
self.requestUrl = URL(string: "https://codeforces.com/api/user.info?handles=abhijeet_ar")!
}
func getData(completionHandler: #escaping(Result<Root, Error>) -> Void) {
URLSession.shared.dataTask(with: self.requestUrl) { (data,response, error) in
guard let data = data else {
completionHandler(.failure(error!))
print("-------bye-bye--------")
return
}
do {
print("-------entered--------")
let dataresponse = try JSONDecoder().decode(Root.self, from: data)
completionHandler(.success(dataresponse))
}
catch {
completionHandler(.failure(error))
}
}.resume()
}
}
And consider that if status is not "OK" the JSON response could be different.
Your problem is you are using the wrong struct. Create and use a Response struct for decoding. You need to keep your Types to have the first letter in capital to avoid confusion. You could use the JSONDecoder() maybe you are using something that doesn't have the correct format. Try the below code.
struct Response: Codable {
var result: [UserData]
}
enum DataError: Error {
case dataUnavailable, cannotProcessData
}
struct DataRequest {
let requestUrl: URL
init(){
self.requestUrl = URL(string: "https://codeforces.com/api/user.info?handles=abhijeet_ar")!
}
func getData(completionHandler: #escaping(Result<Response, DataError>) -> Void) {
URLSession.shared.dataTask(with: self.requestUrl) { (data,response, error) in
guard let data = data else {
completionHandler(.failure(.dataUnavailable))
return
}
do {
let dataresponse = try JSONDecoder().decode(Response.self, from: data)
completionHandler(.success(dataresponse))
} catch {
completionHandler(.failure(.cannotProcessData))
}
}.resume()
}
}
Note: Parameters in UserData always needs to right. Try with and Empty struct to check if everything else is working then proceed to adding the variable one-by-one.
I am trying to implement field level custom decoding, so that a decoder function can be supplied to map a value. This was originally intended to solve automagically turning string values of "Y" and "N" into true / false. Is there a less verbose way I can do this?
This was intended to be used for a single field of a fairly decent sized record... but has gotten somewhat out of hand.
The main objective was not to have to manually implement decoding of every single
field in the record, but to enumerate through them and use the result of the default decoder for anything that did not have a custom decoder (which probably should not be called a "decoder").
Current attempt shown below:
class Foo: Decodable {
var bar: String
var baz: String
init(foo: String) {
self.bar = foo
self.baz = ""
}
enum CodingKeys: String, CodingKey {
case bar
case baz
}
static func customDecoder(for key: CodingKey) -> ((String) -> Any)? {
switch key {
case CodingKeys.baz: return { return $0 == "meow" ? "foo" : "bar" }
default:
return nil
}
}
required init(from decoder: Decoder) throws {
let values: KeyedDecodingContainer = try decoder.container(keyedBy: CodingKeys.self)
if let cde = Foo.customDecoder(for: CodingKeys.bar) {
self.bar = (try cde(values.decode(String.self, forKey: .bar)) as? String)!
} else {
self.bar = try values.decode(type(of: self.bar), forKey: .bar)
}
if let cde = Foo.customDecoder(for: CodingKeys.baz) {
self.baz = (try cde(values.decode(String.self, forKey: .baz)) as? String)!
} else {
self.baz = try values.decode(type(of: self.baz), forKey: .baz)
}
}
}
Example of use:
func testFoo() {
var foo: Foo?
let jsonData = """
{"bar": "foo", "baz": "meow"}
""".data(using: .utf8)
if let data = jsonData {
foo = try? JSONDecoder().decode(Foo.self, from: data)
if let bar = foo {
XCTAssertEqual(bar.bar, "foo")
} else {
XCTFail("bar is not foo")
}
} else {
XCTFail("Could not coerce string into JSON")
}
}
For example we have an example json:
let json = """
{
"id": 1,
"title": "Title",
"thumbnail": "https://www.sample-videos.com/img/Sample-jpg-image-500kb.jpg",
"date": "2014-07-15"
}
""".data(using: .utf8)!
If we want parse that, we can use Codable protocol and simple NewsCodable struct:
public struct NewsCodable: Codable {
public let id: Int
public let title: String
public let thumbnail: PercentEncodedUrl
public let date: MyDate
}
PercentEncodedUrl is our custom Codable wrapper for URL, which adding percent encoding to url string. Standard URL does not support that out of the box.
public struct PercentEncodedUrl: Codable {
public let url: URL
public init(from decoder: Decoder) throws {
let urlString = try decoder.singleValueContainer().decode(String.self)
guard
let encodedUrlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL.init(string: encodedUrlString) else {
throw PercentEncodedUrlError.url(urlString)
}
self.url = url
}
public enum PercentEncodedUrlError: Error {
case url(String)
}
}
If some strange reasons we need custom decoder for date string(Date decoding has plenty of support in JSONDecoder), we can provide wrapper like PercentEncodedUrl.
public struct MyDate: Codable {
public let date: Date
public init(from decoder: Decoder) throws {
let dateString = try decoder.singleValueContainer().decode(String.self)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
guard let date = dateFormatter.date(from: dateString) else {
throw MyDateError.date(dateString)
}
self.date = date
}
public enum MyDateError: Error {
case date(String)
}
}
let decoder = JSONDecoder()
let news = try! decoder.decode(NewsCodable.self, from: json)
So we provided field level custom decoder.
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)
}