-
Notifications
You must be signed in to change notification settings - Fork 19
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
Comments
Hi, could you share your |
Hi @Thiryn , thank you for response. Actually Person is just dummy resource. This is my actual data.
And, this is the
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? |
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 |
Thank you for your answer @Thiryn . After I try again and reproduce with your code, its work. Appreciate your help! |
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 thePerson
attributes returnnil
. Any suggestion for this problem? ThanksHere is sample of my code:
jsonObject
type is[String:Any]
The text was updated successfully, but these errors were encountered: