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

Deserializer not work #7

Closed
andreaswidijargo opened this issue Apr 18, 2018 · 4 comments
Closed

Deserializer not work #7

andreaswidijargo opened this issue Apr 18, 2018 · 4 comments

Comments

@andreaswidijargo
Copy link

andreaswidijargo commented Apr 18, 2018

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
Copy link

Thiryn commented Apr 18, 2018

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

@andreaswidijargo
Copy link
Author

andreaswidijargo commented Apr 18, 2018

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
Copy link

Thiryn commented Apr 18, 2018

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
Copy link
Author

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

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

2 participants