I am attempting to submit the following request through Alamofire and I am receiving the following error:
2020-01-13 09:41:05.912103-0600 AFNetworkingDemo[29720:604258] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'
My assumption is that it is the way I am defining the arrays within the object (I was following some of the material found here: https://benscheirman.com/2017/06/swift-json)
struct ProgramRequest: Codable {
var userID: Int
var programData: ProgramData
var json: Constants.Json {
return [
"userID": userID,
"programData": programData.json,
]
}
}
struct ProgramData: Codable {
var airhotel: [AirHotel]
var security: [Security]
var json: Constants.Json {
return [
"airhotel": airhotel,
"security": security
]
}
}
struct AirHotel: Codable {
var id: Int
var loyaltyNumber: String
var json: Constants.Json {
return [
"id": id,
"loyaltyNumber": loyaltyNumber
]
}
}
struct Security: Codable {
var vendorCode: String
var loyaltyNumber: String
var json: [String: Any] {
return [
"vendorCode": vendorCode,
"loyaltyNumber": loyaltyNumber
]
}
}
The json dictionary at each level is to render the objects appropriately for Alamofire. For a given example, if I print it out using:
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try jsonEncoder.encode(programRequest)
if let jsonString = String(data: jsonData, encoding: .utf8) {
print(jsonString) #1
}
print("IsValidJSON: ", JSONSerialization.isValidJSONObject(programRequest)) #2
print(programRequest.json) #3
urlRequest = try! JSONEncoding.default.encode(urlRequest, with: programRequest.json) #4 causes error
1 output:
{
"userID" : 10021,
"programData" : {
"airhotel" : [],
"security" : [
{
"vendorCode" : "sty",
"loyaltyNumber" : "Loyal1"
}
]
}
}
2 output
IsValidJSON: false
3 output - I noticed the AFNetworkingDemo.Security within the output, is that what Alamofire JSONEncoding on:
["programData": ["security": [AFNetworkingDemo.Security(vendorCode: "sty", loyaltyNumber: "Loyal1")], "airhotel": []], "userID": 10021]
My question would be, what changes to I need to make in the AirHotel and Security sections of ProgramRequest in order to resolve my issues with Alamofire?
Step 1: Stop reinventing Codable
The json computed property is not needed and wrongly used.
Remove it.
struct ProgramRequest: Codable {
var userID: Int
var programData: ProgramData
}
struct ProgramData: Codable {
var airhotel: [AirHotel]
var security: [Security]
}
struct AirHotel: Codable {
var id: Int
var loyaltyNumber: String
}
struct Security: Codable {
var vendorCode: String
var loyaltyNumber: String
}
Step 2: Start using Codable
Now given a programRequest, eg. like this
let programRequest = ProgramRequest(userID: 1, programData: ProgramData(airhotel: [AirHotel(id: 1, loyaltyNumber: "loyaltyNumber")], security: [Security(vendorCode: "loyaltyNumber", loyaltyNumber: "loyaltyNumber")]))
You can generate the correct Data value representing the JSON simply writing
let data = try JSONEncoder().encode(programRequest)
And (if you want) you can convert data intro a String
let json = String(data: data, encoding: .utf8)
Output
Optional("{\"userID\":1,\"programData\":{\"airhotel\":[{\"id\":1,\"loyaltyNumber\":\"loyaltyNumber\"}],\"security\":[{\"vendorCode\":\"loyaltyNumber\",\"loyaltyNumber\":\"loyaltyNumber\"}]}}")
That's it
Related
I'm making a POST request with URLSession. I need to send the following string in the request body:
{"rebajados": false, "text": "pantalon", "municipios": [], "departamentos": []}
so i define a struct to use codable to send data as request body. the struct is this.
struct filter: Codable {
var text: String?
var departamentos: [String]?
var municipios: [String]?
var rebajados = false
}
but what I send is this:
{
"departamentos": [
""
],
"municipios": [
""
],
"rebajados": false,
"text": "pantalon"
}
The backend returns no result because [""] makes it lost.
So what's a posible way to make the array a empty array without the double ""?
Note: I can't modifiy the backend to accept the array with empty string.
First of all please name structs with a starting capital letter.
An empty string array is encoded as empty JSON array
struct Filter : Codable {
let text : String
let departamentos : [String]
let municipios : [String]
let rebajados : Bool
}
let filter = Filter(text: "pantalon", departamentos: [], municipios: [], rebajados: false)
do {
let data = try JSONEncoder().encode(filter)
let string = String(data: data, encoding: .utf8)!
print(string) // {"rebajados":false,"municipios":[],"departamentos":[],"text":"pantalon"}
} catch {
print(error)
}
Try
struct filter:Codable {
var text: String?
var departamentos: [String] = []
var municipios: [String] = []
var rebajados = false
}
I was trying convert struct to Dictionary in Swift. This was my code:
extension Encodable {
var dictionary: [String: Any]? {
if let data = try? JSONEncoder().encode(self) {
if let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
return dict
}
return nil
}
return nil
}
}
This works in most situation. But when I try to convert a nested structure which contains unicode characters such as Chinese, this happened:
struct PersonModel: Codable {
var job: String?
var contacts: [ContactSimpleModel]
var manager: ManagerSimpleModel?
}
struct ContactSimpleModel: Codable {
var relation: String
var name: String
}
struct ManagerSimpleModel: Codable {
var name: String
var age: Int
}
let contact1 = ContactSimpleModel(relation: "朋友", name: "宙斯")
let contact2 = ContactSimpleModel(relation: "同学", name: "奥丁")
let manager = ManagerSimpleModel(name: "拉斐尔", age: 31)
let job = "火枪手"
let person = PersonModel(job: job, contacts: [contact1, contact2], manager: manager)
if let dict = person.dictionary {
print(dict)
}
The result of this code is this:
["contacts": <__NSArrayI 0x600002471980>(
{
name = "\U5b99\U65af";
relation = "\U670b\U53cb";
},
{
name = "\U5965\U4e01";
relation = "\U540c\U5b66";
}
)
, "manager": {
age = 31;
name = "\U62c9\U6590\U5c14";
}, "job": 火枪手]
You can see the result. The Chinese characters in those nested structures were become a utf-8 encoding string. The top-level property "job": 火枪手 is right. But the values in those nested structures were not the original string.
Is this a bug of JSONSerialization? Or how to make it right?
More information. I used the result like this:
var sortedQuery = ""
if let dict = person.dictionary {
sortedQuery = dict.sorted(by: {$0.0 < $1.0})
.map({ "\($0)\($1)" })
.joined(separator: "")
}
It was used to check whether the query was legal. The result is not the same as Java or other platform.
The result is perfectly fine. That's the internal string representation – a pre-Unicode legacy – of an array or dictionary when you print it.
Assign the values to a label or text view and you will see the expected characters.
I am trying decode Json data into my Model.
This is my model
struct Devices : Codable {
var id :String?
var description :String?
var status : Int?
}
var heroes = Devices()
print(DeviceId)
let loginParam: [String: Any] = [
"id": DeviceId
]
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 5
manager.request("http://13.13.13.004/website/api/Customer/DeviceControl", method: .post , parameters: loginParam, encoding: JSONEncoding.prettyPrinted)
.responseData { response in
let json = response.data
do{
let decoder = JSONDecoder()
//using the array to put values
heroes = try decoder.decode(Devices.self, from: json!)
}catch let err{
print(err)
}
this code doesn't get in catch block.
But heroes values return nill.
When ı try use NsDictionary
Its give result.
This is a common mistake: You forget the root object
struct Root : Decodable {
private enum CodingKeys: String, CodingKey { case resultCount, devices = "results" }
let resultCount : Int
let devices : [Device]
}
And name the device struct in singular form (devices is an array of Device instances) and declare the members as non-optional
struct Device : Decodable {
var id : String
var description : String
var status : Int
}
...
var heroes = [Device]()
...
let result = try decoder.decode(Root.self, from: json!)
heroes = result.devices
In Vapor 1.5 I used to convert Dictionary to JSON as shown below.
How should I do it in Vapor 3.1?
In docs it says I need to create struct type and conform it to codable protocol.
Is there another method that would enable to convert the existing dictionary without creating new struct?
func makeCustomJSON(clientData: DataFromClientChargeWithCard, paymentID:String,customer: String) throws -> JSON {
var dictionaryOfStrings = [String:String]()
dictionaryOfStrings["BookingState"] = "Active"
dictionaryOfStrings["BookingStateTimeStamp"] = clientData.TimeStampBookingSavedInDB
dictionaryOfStrings["ChangesMadeBy"] = "User"
dictionaryOfStrings["PaymentID"] = paymentID
dictionaryOfStrings["DateAndTime"] = clientData.DateAndTime
dictionaryOfStrings["RatePriceClient"] = clientData.RatePriceClient
dictionaryOfStrings["Locality"] = clientData.Locality
dictionaryOfStrings["StripeCustomerID"] = customer
//some more properties below...
let response:JSON
do {
response = try JSON(node: dictionaryOfStrings)
}catch let error as NSError {
let message = "dictionaryOfStrings can't be converted in JSON"
drop.log.error(message)
logErr.prints(message: message, code: error.code)
throw NSError(domain: "errorDictionary", code: 400, userInfo: ["error" : error.localizedDescription])
}
return response
}
If you have a type like the struct that I have defined here you can simply return it, as long as it conforms to Content.
import Vapor
import FluentSQLite
struct Employee: Codable {
var id: Int?
var name: String
init(name:String) {
self.name = name
}
}
extension Employee: SQLiteModel {}
extension Employee: Migration {}
extension Employee: Parameter {}
extension Employee: Content { }
and then you can simply define a route and return it.
router.get("test") { req in
return Employee(name: "Alan")
}
if you want to use dictionary you can use the JSONSerialization and return a string.
router.get("test2") { req -> String in
let employeeDic: [String : Any] = ["name":"Alan", "age":27]
do {
let data = try JSONSerialization.data(withJSONObject: employeeDic, options: .prettyPrinted)
let jsonString = String(data: data, encoding: .utf8)
return jsonString ?? "FAILED"
}catch{
return "ERROR"
}
}
I have a JSON format which I do not decodable with Alamofire.
Here is my json:
"data":[
{
"id":37,
"status":"A\u00e7\u0131k",
"department":"Muhasebe",
"title":"Y\u00f6netim Panelinden Deneme 4 - Mail Kontrol",
"message":"<p>Y\u00f6netim Panelinden Deneme 4 - Mail Kontrol<br><\/p>",
"file":null,
"created_at":{
"date":"2018-01-13 01:59:49.000000",
"timezone_type":3,
"timezone":"UTC"
},
"replies":[
{
"id":6,
"ticket_id":37,
"admin_id":null,
"user_id":8593,
"message":"<p>test<\/p>",
"file":"uploads\/tickets\/8593-P87wd8\/GFV6H5M94y5Pt27YAxZxHNRcVyFjD554i80og3xk.png",
"created_at":"2018-01-18 11:16:55",
"updated_at":"2018-01-18 11:16:55"
}
]
},
Here is my model for the JSON:
struct TeknikDestek : Decodable {
var id: Int?
var status: String?
var title: String?
var department: String?
var message: String?
var replies: [Replies]?
}
struct Replies: Decodable {
var replyid: Int?
var ticket_id: Int?
var admin_id: Int?
var user_id: Int?
var message: String?
}
I called it Alamofire, but it does not come back when I do response.data.
Alamofire.request("https://myurl.com.tr/api/tickets/\(userid)").responseJSON { (response) in
switch response.result {
case .success:
if((response.result) != nil) {
let jsonData = response.data
print("jsonData: \(test)")
do{
self.allReplies = try JSONDecoder().decode([TeknikDestek].self, from: jsonData!)
print(self.allReplies)
for reply in self.allReplies {
print("Reply: \(reply)")
}
}catch {
print("Error: \(error)")
}
self.view.dismissNavBarActivity()
}
case .failure(let error):
print(error)
}
}
This is the error console:
How can I make it work? I've spent several hours now but without success. Please help me. Many Thanks.
The question is not related to Alamofire. It's only related to JSONDecoder / Decodable
You need an umbrella struct for the root object, which is a dictionary containing the data key, not an array. That's what the error message states.
struct Root : Decodable {
let data : [TeknikDestek]
}
Then decode Root
let root = try JSONDecoder().decode(Root.self, from: jsonData!)
and get the replies with
self.allReplies = root.data.first?.replies // returns `nil` if data is empty
Note: It's highly recommended to name data structs in singular form (e.g. Reply), semantically you have a collection of singular items