GithubHelp home page GithubHelp logo

Deserializer not work about vox HOT 4 CLOSED

aronbalog avatar aronbalog commented on May 23, 2024
Deserializer not work

from vox.

Comments (4)

Thiryn avatar Thiryn commented on May 23, 2024 2

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

from vox.

Thiryn avatar Thiryn commented on May 23, 2024

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

from vox.

andreaswidijargo avatar andreaswidijargo commented on May 23, 2024

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

from vox.

andreaswidijargo avatar andreaswidijargo commented on May 23, 2024

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

from vox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.