Comments (4)
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.
Hi, could you share your Person
resource class and the prettyPrint of the jsonData
variable ?
from vox.
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?
from vox.
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)
- JSONAPI error handling HOT 1
- Library not compatible with iOS 10 HOT 1
- Allow basic Alamofire client to be subclassed
- Request::execute is throwable but does not throw
- Allow ErrorObject to be stringified
- Relationship object not deserialized HOT 8
- relationship subset
- Filtering Objects
- Links field missing from Resource.swift HOT 1
- Pagination struct is not visible HOT 1
- Relationships missing on deserialization HOT 4
- Creating Resource - Issue unwrapping response HOT 1
- Exception: EXC_BAD_INSTRUCTION When accessing nested relationship HOT 1
- Relationships not deserialize
- initializer
- Write functions just on time
- ssl pinning in vox HOT 4
- Won't build keep getting same error "Must override `static var resourceType: String`:"
- Context handling error response as unknown incorrectly. HOT 1
- Set meta in the resource
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vox.