Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem JSON encoding and decoding in Swift with JSONCodable #79

Open
CPiersigilli opened this issue Apr 24, 2017 · 3 comments
Open

Problem JSON encoding and decoding in Swift with JSONCodable #79

CPiersigilli opened this issue Apr 24, 2017 · 3 comments

Comments

@CPiersigilli
Copy link

I have this code for os x simple app, but it does not work:

import Cocoa

struct GeoFolderData {
    var nomeCartella:String
    var nomeCommittente:String?
    var dataCreazioneCartella:NSDate
    var dataModificaCartella:NSDate
    var isActive:Bool
}

extension GeoFolderData: JSONDecodable {
    init(object: JSONObject) throws {
        let decoder = JSONDecoder(object: object)
        nomeCartella = try decoder.decode("Nome_Cartella")
        nomeCommittente = try decoder.decode("Nome_Committente")
        dataCreazioneCartella = try decoder.decode(key: "dataCreazioneCartella")
        dataModificaCartella = try decoder.decode(key: "dataModificaCartella")
        isActive = try decoder.decode("Is_Active")
    }
}

extension GeoFolderData: JSONEncodable {
    func toJSON() throws -> Any {
        return try JSONEncoder.create({ (encoder) -> Void in
            try encoder.encode(nomeCartella, key: "Nome_Cartella")
            try encoder.encode(nomeCommittente, key: "Nome_Committente")
            try encoder.encode(dataCreazioneCartella, key: "Data_Creazione_Cartella")
            try encoder.encode(dataModificaCartella, key: "Data_Modifica_Cartella")
            try encoder.encode(isActive, key: "Is_Active")
        })
    }
}

extension JSONDecoder {
    public func decode(key: String) throws -> NSDate {
        return try decode(String, transformer: JSONTransformer<StringToNSDate, StringToNSDate>)
    }
}

With these errors:
screenshot 2017-04-24 23 24 56

Is there anyone who can help me?
Thank you.

@Mozahler
Copy link

Did you trying asking this question on stackoverflow.com?

@CPiersigilli
Copy link
Author

No. Now, see JSON encoding and decoding with “matthewcheok/JSONCodable” on stackoverflow.com

@pronebird
Copy link

You don't seem to import JSONCodable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants