aronbalog / Vox

Swift JSON:API client framework
http://jsonapi.org
MIT License
46 stars 19 forks source link

Deserializer not work #7

Closed andreaswidijargo closed 6 years ago

andreaswidijargo commented 6 years ago

Hi, I've tried to convert Data to a JSONAPIDocument struct, and it doesn't throw an error, but when I read documen.data, all the Person attributes return nil. Any suggestion for this problem? Thanks

Here is sample of my code:

let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
let deserializer = Deserializer.Collection<Person>()
do {
    let document = try deserializer.deserialize(data: jsonData)
    if let personData = document.data {   
        self?.person = personData
    } 
} catch JSONAPIError.API(let errors) {
    errors.forEach { error in

    }
} catch JSONAPIError.serialization {
    print("Given data is not valid JSONAPI document")
} catch {
    print("Something went wrong. Maybe `data` does not contain valid JSON?")
}

jsonObject type is [String:Any]

Thiryn commented 6 years ago

Hi, could you share your Person resource class and the prettyPrint of the jsonData variable ?

andreaswidijargo commented 6 years ago

Hi @Thiryn , thank you for response.

Actually Person is just dummy resource. This is my actual data.

class StyleStarVox: Resource {
    @objc dynamic var name: String?
    @objc dynamic var subtitle: String?
    @objc dynamic var imageString: String?

    override class var resourceType: String {
        return "rewards"
    }

    override class var codingKeys: [String : String] {
        return [
            "imageString": "image"
        ]
    }
}

And, this is the jsonData:

{
    "data": [
             {
                 "id": "1",
                 "type": "rewards",
                 "context": "Reward",
                 "domain": "User",
                 "attributes": {
                     "name": "Monthly Subscription",
                     "subtitle": "Earn stars every month as you stay subscribed.",
                     "image": "https://s3-ap-southeast-1.amazonaws.com/29e92364_67f28c6a-b5bd-4301-942d-e5b5967ba9f8.jpg",
                     "region": "all",
                     "created_at": "2018-04-13T04:32:57.744Z",
                     "updated_at": "2018-04-13T04:32:57.744Z"
                 }
             },
             {
                 "id": "2",
                 "type": "rewards",
                 "context": "Reward",
                 "domain": "User",
                 "attributes": {
                     "name": "Purchase an Item",
                     "subtitle": "Earn stars when you purchase an item.",
                     "image": "https://s3-ap-southeast-1.amazonaws.com/29e92364_67f28c6a-b5bd-4301-942d-e5b5967ba9f8.jpg",
                     "region": "sg",
                     "created_at": "2018-04-13T04:32:57.844Z",
                     "updated_at": "2018-04-13T04:32:57.844Z"
                 }
             }
             ]
}

When i do debugging, I found actually the data appear in attributes value (picture below), but I don't know why it return nil in every attribute that i set. Any suggestion?

screen shot 2018-04-18 at 18 18 30

Thiryn commented 6 years ago

I tried to reproduce, can't get any error. I took your json, transformed it as a dict [String:Any] and went directly through the deserializer like a charm. My test :

    func convertToDictionary(text: String) -> [String: Any]? {
        if let data = text.data(using: .utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
            } catch {
                print(error.localizedDescription)
            }
        }
        return nil
    }

    func test() {

        let str = "{ \"data\": [ { \"id\": \"1\", \"type\": \"rewards\", \"context\": \"Reward\", \"domain\": \"User\", \"attributes\": { \"name\": \"Monthly Subscription\", \"subtitle\": \"Earn stars every month as you stay subscribed.\", \"image\": \"https://s3-ap-southeast-1.amazonaws.com/29e92364_67f28c6a-b5bd-4301-942d-e5b5967ba9f8.jpg\", \"region\": \"all\", \"created_at\": \"2018-04-13T04:32:57.744Z\", \"updated_at\": \"2018-04-13T04:32:57.744Z\" } }, { \"id\": \"2\", \"type\": \"rewards\", \"context\": \"Reward\", \"domain\": \"User\", \"attributes\": { \"name\": \"Purchase an Item\", \"subtitle\": \"Earn stars when you purchase an item.\", \"image\": \"https://s3-ap-southeast-1.amazonaws.com/29e92364_67f28c6a-b5bd-4301-942d-e5b5967ba9f8.jpg\", \"region\": \"sg\", \"created_at\": \"2018-04-13T04:32:57.844Z\", \"updated_at\": \"2018-04-13T04:32:57.844Z\" } } ] }"

        let jsonData = try! JSONSerialization.data(withJSONObject: convertToDictionary(text: str)!, options: .prettyPrinted)
        let deserializer = Deserializer.Collection<StyleStarVox>()
        do {
            let document = try deserializer.deserialize(data: jsonData)
            print(document.data![0].name) // prints Optional("Monthly Subscription")
        } catch JSONAPIError.API(let errors) {
            errors.forEach { error in

            }
        } catch JSONAPIError.serialization {
            print("Given data is not valid JSONAPI document")
        } catch {
            print("Something went wrong. Maybe `data` does not contain valid JSON?")
        }

My understanding is that you do not see the attributes in the Resource because Vox overrides the backend functions (Setters and Getters) and not the data itself. For example if you want to access myPerson.name, it calls the getter (BaseResource::value) and after a few checks and dereferencing return the value for the name attribute

andreaswidijargo commented 6 years ago

Thank you for your answer @Thiryn . After I try again and reproduce with your code, its work. Appreciate your help!