Swift Siesta edit fetched entity - json

I'm building an API client using Siesta and Swift 3 on Xcode 8. I want to be able to fetch an entity using a Siesta resource, then update some of the data and do a patch to the API.
The issue is that having an entity, if I save the JSON arrays in my entity fields I can't send them back to the server, I get the following error:
▿ Siesta.RequestError
- userMessage: "Cannot send request"
- httpStatusCode: nil
- entity: nil
▿ cause: Optional(Siesta.RequestError.Cause.InvalidJSONObject())
- some: Siesta.RequestError.Cause.InvalidJSONObject
- timestamp: 502652734.40489101
My entity is:
import SwiftyJSON
import Foundation
struct Order {
let id: String?
let sessionId: String?
let userId: Int?
let status: String?
let comment: String?
let price: Float?
let products: Array<JSON>?
init(json: JSON) throws {
id = json["id"].string
sessionId = json["sessionId"].string
userId = json["userId"].int
status = json["status"].string
comment = json["comment"].string
price = json["price"].float
products = json["products"].arrayValue
}
/**
* Helper method to return data as a Dictionary to be able to modify it and do a patch
**/
public func toDictionary() -> Dictionary<String, Any> {
var dictionary: [String:Any] = [
"id": id ?? "",
"sessionId": sessionId ?? "",
"userId": userId ?? 0,
"status": status ?? "",
"comment": comment ?? ""
]
dictionary["products"] = products ?? []
return dictionary
}
}
What I'm doing is:
MyAPI.sessionOrders(sessionId: sessionId).request(.post, json: ["products": [["product": productId, "amount": 2]], "comment": "get Swifty"]).onSuccess() { response in
let createdObject : Order? = response.typedContent()
expect(createdObject?.sessionId).to(equal(sessionId))
expect(createdObject?.comment).to(equal("get Swifty"))
expect(createdObject?.products).to(haveCount(1))
expect(createdObject?.price).to(equal(product.price! * 2))
if let createdId = createdObject?.id {
var data = createdObject?.toDictionary()
data?["comment"] = "edited Swifty" // can set paid because the user is the business owner
MyAPI.order(id: createdId).request(.patch, json: data!).onSuccess() { response in
result = true
}.onFailure() { response in
dump(response) //error is here
}
}
}
Resources:
func sessionOrders( sessionId: String ) -> Resource {
return self
.resource("/sessions")
.child(sessionId)
.child("orders")
}
func order( id: String ) -> Resource {
return self
.resource("/orders")
.child(id)
}
Transformers:
self.configureTransformer("/sessions/*/orders", requestMethods: [.post, .put]) {
try Order(json: ($0.content as JSON)["data"])
}
self.configureTransformer("/orders/*") {
try Order(json: ($0.content as JSON)["data"])
}
I've managed to circle this by creating dictionary structures like:
let products: Array<Dictionary<String, Any>>?
products = json["products"].arrayValue.map({
["product": $0.dictionaryValue["product"]!.stringValue, "amount": $0.dictionaryValue["amount"]!.intValue]
})
But I live in a hell of downcasts if I need to modify anything:
var data = createdObject?.toDictionary()
data?["comment"] = "edited Swifty"
//if I want to modify the products...
var products = data?["products"] as! Array<Dictionary<String, Any>>
products[0]["amount"] = 4
data?["products"] = products
How can I send those original JSON arrays with Siesta? They're really easy to modify and read! I've browsed the siesta docs and github issues with no success...

Your problem is a mismatch between SwiftyJSON and Foundation’s JSONSerialization; Siesta just happens to be in the middle of it.
InvalidJSONObject is Siesta telling you that Foundation doesn’t understand the thing you gave it — which would be the value returned by your toDictionary() method. Most of the things in that dictionary look fine: strings, ints, a float. (Careful about using float for money, BTW.)
The culprit is that products array: it’s [JSON], where JSON is a SwiftyJSON type that Foundation doesn’t know what to do with. You should be in the clear if you turn the JSON values back into simple dictionaries:
dictionary["products"] = (products ?? []).map { $0.dictionaryObject }
If that doesn’t do it, or if you need to diagnose a similar error in the future, remove all the values from the offending dictionary and then add them back in one at a time to see which one is tripping up JSONSerialization.

Related

Swift code can not find the symbol of the data

I am new to swift . I am trying to convert json into model by using swift . I am using generic functions to complete the functions . Here is the structure of the json .
Here is the model I created based on jason .
import Foundation
// MARK: - Welcome
struct Welcome: Codable {
let photos: [Photo]
}
// MARK: - Photo
struct Photo: Codable {
let id, sol: Int
let camera: Camera
let imgSrc: String
let earthDate: String
let rover: Rover
enum CodingKeys: String, CodingKey {
case id, sol, camera
case imgSrc = "img_src"
case earthDate = "earth_date"
case rover
}
}
// MARK: - Camera
struct Camera: Codable {
let id: Int
let name: String
let roverID: Int
let fullName: String
enum CodingKeys: String, CodingKey {
case id, name
case roverID = "rover_id"
case fullName = "full_name"
}
}
// MARK: - Rover
struct Rover: Codable {
let id: Int
let name, landingDate, launchDate, status: String
enum CodingKeys: String, CodingKey {
case id, name
case landingDate = "landing_date"
case launchDate = "launch_date"
case status
}
}
Here is the code in generic function.
func getModel<Model: Codable>(_ type: Model.Type, from url: String, completion: #escaping (Result<Model, NetworkError>) -> ()) {
guard let url = URL(string: url) else {
completion(.failure(.badURL))
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
completion(.failure(.other(error)))
return
}
if let data = data {
do {
let response = try JSONDecoder().decode(type, from: data)
completion(.success(response))
} catch let error {
completion(.failure(.other(error)))
}
}
}
.resume()
}
I am trying to call this function form controller but it is showing the error Value of type 'Post' has no member 'data'
Here is the code to call the function.
class ViewModel {
private let networkManager = NetworkManager()
private var rovers = [Post]()
func getStories (){
networkManager
.getModel(Post.self, from: NetworkURLs.baseURL) {[weak self]result in
switch result{
case .success(let response):
self?.rovers = response.data.camera.map{$0.data} **// error on this line**
case .failure( let error):
print( error.localizedDescription)
}
}
}
Your response is of type Post which has no property data. You'll need to extract your photos array from the response, and then map across that array and retrieve the rovers property from it.
I think what you meant to write was
self?.rovers = response.photos.camera.map{$0.rover}
However even that won't work as your data structures don't match your JSON. From what can be seen, rover is a property on photo not on camera.
You will need to validate the JSON -> Model mapping
EDIT after JSON linked in comment below:
Using the JSON from the API, it confirms that camera and rover sit at the same level in the JSON:
{
"photos": [
{
"id": 102693,
"sol": 1000,
"camera": {
"id": 20,
"name": "FHAZ",
"rover_id": 5,
"full_name": "Front Hazard Avoidance Camera"
},
"img_src": "http://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01000/opgs/edr/fcam/FLB_486265257EDR_F0481570FHAZ00323M_.JPG",
"earth_date": "2015-05-30",
"rover": {
"id": 5,
"name": "Curiosity",
"landing_date": "2012-08-06",
"launch_date": "2011-11-26",
"status": "active"
}
},
....
So you will need to change your data model:
struct Photo : Codable{
let id : Int
let sol : Int
let camera : Camera
let imgSrc: String
let earthDate: String
let rover: Rover
}
and then to decode it
self?.rovers = response.photos.map{$0.rover}
nb. in Swift all struct types should be capitalised by convention.
Your struct of type Post does not have a member called "data", indeed.
You seem to be assuming, that your response object is of type Photo - but the error message is telling you, that it is of type Post, which only holds an array of Photo objects.
Try something like:
response.photos[0] to get the first Photo object out of the array - if there is one.
Then, assuming you got one response.photos[0].data gives you a Camera object already - you seem to be calling via the type, instead of the member name.
So in case you want to go one step further and access a Rover object, you need to do: response.photos[0].data.data
I see, that you want to extract several Rovers, supposedly one from each Post, but this will clash with your initial rovers variable being assigned a type of an array of Posts - this means you have to change it to [Rover]. I'm not sure if the map-function is actually suitable for what you want to do here.
Using a loop, iterating through the Posts and appending Rover objects to the Rover array would be the "manual" way to do it.
Hope this helps.
Edit: because you have edited your model mid-question, I can't see where "Post" has gone now. My reply might only fit the way the original question was posted.

Automatic parsing JSON with Swift 5 and Alomofire

I am calling an API via Alomofire in Swift 5 and get the following Result Set in JSON format :
Here is the call :
AF.request("https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemSearch", parameters: parameters).responseJSON { response in
print(response.result)
}
I have the following info in the result : Content-Encoding: gzip, the solution may come from here)
Here is the JSON Response body :
{"ResultSet":{"totalResultsAvailable":"3","totalResultsReturned":3,"firstResultPosition":"1","0":{"Result":{"Request":{"Query":""},"Modules":"","0":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_4901330197711f","Condition":"new","Image":{"Id":"ejapan_4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"121"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"121","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"121","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"1","Times":"1","PremiumAmount":"1","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11871","Total":"51092","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"1"}},"1":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711f","Condition":"new","Image":{"Id":"ejapan_012-4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"1398"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"1398","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"1398","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"13","Times":"1","PremiumAmount":"13","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11875","Total":"51100","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"2"}},"2":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u3010\u9001\u6599\u7121\u6599\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711fm.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711fm","Condition":"new","Image":{"Id":"ejapan_012-4901330197711fm","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711fm","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711fm"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711fm"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"2278"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"2278","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"2278","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"22","Times":"1","PremiumAmount":"22","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11874","Total":"51099","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"3"}},"_container":"Hit"}}}}
So I would like to find the way to automatically parse this JSON body in order to exploit the data later,
Does the solution come from the encoding format ?
I have not been successful implementing decompressing using Gzip (link here)
Would it be possible to do it automatically via Decodable or the data is maybe directly accessible in Alomofire ?
Thank you in advance,
Charles
When it comes to decoding and holding data from JSON responses from REST APIs, I have used the following approach:
Include the bellow two PODS
1) Alamofire
2) TRON
Now, you'll have to create a class you'll use as a datasource interface between the API and the actual class model. In the following example, I'm pulling data from a movie database service and I have the bellow class for the data sourcing:
class MovieDataSource: JSONDecodable {
var movies: [Movie] = []
let cellid = "cellid"
required init(json: JSON) throws {
guard let moviesArray = json["results"].array else {
print("Error on the JSON Format")
return
}
self.movies = moviesArray.map({return Movie(json: $0)})
}
}
class JSONError: JSONDecodable {
required init(json: JSON) throws {
print("JSON error\n", json)
}
}
Then I have the actual data holding class/structure:
struct Movie {
let title: String
let movieId: Int
let overview: String
let releaseDate: String
let posterPathURL: String
let genres: [Int]
let vote_average: Float
let popularity: Float
let isForAdults: Bool
init(json: JSON) {
self.title = json["title"].stringValue
self.movieId = json["id"].intValue
self.overview = json["overview"].stringValue
self.releaseDate = json["release_date"].stringValue
self.posterPathURL = json["poster_path"].stringValue
self.genres = json["genre_ids"].arrayObject as! [Int]
self.vote_average = json["vote_average"].floatValue
self.popularity = json["popularity"].floatValue
self.isForAdults = json["adult"].boolValue
}
}
I think the above information should be enough to get you an idea on how to move forward, but If you need further guidance, I found this video series very educative on this subject: https://www.youtube.com/watch?v=tt67VikEovY&list=PL0dzCUj1L5JE1wErjzEyVqlvx92VN3DL5&index=13
Good luck!

Parsing JSON using codable and ignoring first layer of JSON

I have JSON like this:
{
"success": true,
"message": "",
"result": {
"buy": [
{
"Quantity": 0.0056,
"Rate": 18527
},
{
"Quantity": 0.11431426,
"Rate": 18526
}
],
"sell":[
{
"Quantity": 8.20604116,
"Rate": 18540
},
{
"Quantity": 0.95600491,
"Rate": 18574.99999998
}
]
}
}
and another set of JSON like this:
{
"lastUpdateId": 1027024,
"bids": [
[
"4.00000000", // PRICE
"431.00000000", // QTY
[] // Can be ignored
]
],
"asks": [
[
"4.00000200",
"12.00000000",
[]
]
]
}
What is the best way to parse these two responses using codable. They both need to be parsed using the same struct or need to be converted to the same struct (whatever will do the job faster). I don't want to create a struct for the entire first response because I am not going to use keys like "success" and "message". I basically want to ignore those and get directly to the "result" key But in the second response, I will being using all the data so I have created a struct for that called TotalOrderBook. What is the best way to do this?
What is confusing me is ignoring the keys "success" and "message" in the first JSON response and getting straight to the value for the key "result". Is it possible to do that without creating an additional struct?
This is what I have right now. I would like to avoid adding another struct since the only thing I really need is the values under buy/bid and sell/sell.
struct TotalOrderBook:Codable{
var buy:[UniversalOrder]?
var sell:[UniversalOrder]?
var bid:[UniversalOrder]?
var ask:[UniversalOrder]?
var buyOrderBook: [UniversalOrder] {
return bid ?? buy ?? [UniversalOrder]()
}
var sellOrderBook: [UniversalOrder] {
return ask ?? sell ?? [UniversalOrder]()
}
var updatedTime:Date
}
struct UniversalOrder:Codable{
var price : Double {
return Double(rate ?? binPrice ?? 0)
}
var size : Double {
return Double(quantity ?? binQuantity ?? 0 )
}
//let numOrders : Int
var quantity:Double?
var rate:Double?
var binPrice:Double?
var binQuantity:Double?
private enum CodingKeys: String, CodingKey {
case rate = "Rate"
case quantity = "Quantity"
//case numOrders, binPrice,
}
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
binPrice = Double(try container.decode(String.self)) ?? nil
binQuantity = Double(try container.decode(String.self)) ?? nil
quantity = nil
rate = nil
}
}
This is how I am decoding:
let decoder = JSONDecoder()
let data = try! JSONSerialization.data(withJSONObject: value) //value is the response from Alamofire
var theWholeOrderBook:UniversalOrder!
do {
theWholeOrderBook = try decoder.decode(UniversalOrder.self, from: data)
} catch let error {
//print ("error is \(e) ** \(value)")
}
To answer your questions directly, yes it is very easy to ignore the success and message key-value pairs and head straight to results.
Despite this it will be a bit complicated to have a single struct to parse both of these JSON responses.
Both of them have a very different structure which will make it easier to use two different structs to use encoding. To highlight some differences :
buy, sell are nested inside results. bids, asks aren't.
The keys are completely different.
buy, sell have an array of key-value pairs while bids, asks simple have an array of values.
Codable structs should be simple and clear. It's better to have two of those corresponding to each response.

Unpacking NSArrays into String swift

I want to unpack an array of users to a single string.
For instance in my app there are group chats and each time a user enters a group chat their uid is added to a list of members. I want to return all members in a single group chat as a string in app.
Each room is represented in code as the following data object
class Room: NSObject {
var owner: String?
var groupChatName: String?
var groupChatDescription: String?
var members: [NSArray]?
}
Below is where I retrieve each room from the database.
func fetchAllRooms(){
Database.database().reference().child("rooms").observe(.childAdded, with: {(snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let room = Room()
room.setValuesForKeys(dictionary)
self.rooms.append(room)
print(snapshot)
print(room.members?.joined())
print(self.rooms.count)
DispatchQueue.main.async(execute: {
self.collectionView?.reloadData()
})
}
print("end of snap")
}, withCancel: nil)
}
print(room.members?.joined()) Is what currently crashes the app and throws 'NSInvalidArgumentException', reason:
'-[__NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x60800146ab80'
I realize it could possibly be how my data is structured so here's a look at the json
"A632CA68-40D9-4F3A-B8C8-245457057443" : {
"groupChatDescription" : "Test Description",
"groupChatName" : "Test Name",
"members" : {
"WuqCAt4mM3h0P0X1m7hVZ7NQyLC2" : {
"username" : "Steve"
}
},
"owner" : "Steve"
},
Any answers, suggestions, and or references are greatly appreciated.
Seems like you are setting dictionary on your class. So first you need to extract the value from dictionary and set it to your Room model class properties. After that try invoke joined method on your array then it should work.

swift error: Invalid top-level type in JSON write

I have a structure within my swift class that shall describe a card and I want to have it as a JSON which shall be written out to a file on save.
My problem is, I get an error during the NSJSONSerialization stating that top level typ is wrong. This is the exact error:
NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write
Below is my struct and my toJSON() function, which gives the named error.
struct CardStructure {
var UUID: NSUUID = NSUUID.init()
var Chapter: Int = 0
var Card: Int = 0
var CorrectAnswers: Int = 0
var WrongAnswers: Int = 0
var Unknown: Int = 0
var LastAsked: String = ""
var Question: String = ""
var Answer: String = ""
var Hint: String = ""
var Tags: [String] = []
var Links: [String] = []
var Picture: String = ""
func toJSON() -> String? {
let props = ["UUID": String(describing: UUID),
"Chapter": Chapter,
"Card": Card,
"Question": Question,
"Answer": Answer,
"Hint": Hint,
"Tags": Tags,
"Links": Links,
"Picture": Picture
] as [String : Any]
do {
let jsonData = try JSONSerialization.data(withJSONObject: props,
options: .prettyPrinted)
return String(data: jsonData, encoding: String.Encoding.utf8)
} catch let error {
print("error converting to json: \(error)")
return nil
}
}
}
I'm fairly new to swift and have no clue why this error is caused. Can anyone help me??? I tried to understand tips in other posts regarding this error, but so far, my knowledge on swift is too low, to adapt solutions provided, like this one in the link:
invalid-top-level-type-in-json-write
Best regards,
Chris
I originally missed that the question was about serializing the properties of CardStructure instead of an instance of CardStrucure itself ... Leaving the original answer below since the comments refer to it ...
I just tried the above code with the following two lines in both a macOS and an iOS Playground. The code ran fine without any issues. Which version of Xcode, Swift etc. are you using?
let cs = CardStructure()
let str = cs.toJSON()
-- Original answer
If you refer to the JSONSerialization documentation here:
https://developer.apple.com/reference/foundation/jsonserialization
You will notice that it mentions the following:
An object that may be converted to JSON must have the following
properties:
The top level object is an NSArray or NSDictionary.
All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
All dictionary keys are instances of NSString.
Numbers are not NaN or infinity.
Since your top-level object, CardStructure, is not an NSArray or NSDictionary, the error would seem to be correct.