Adding nested dictionary causes JSONSerialization to return nil - swift

I have the following structure that is used to pass JSON data to a REST endpoint. Originally the object contained only one level of key-value pairs. In that scenario, serializing to a JSON object worked properly.
Now I need to add a dictionary as a parameter, which should create a nested dictionary in the resulting JSON. However, adding the nested dictionary causes JSONSerialization to return nil.
Code:
struct ServicePayload:Codable {
private var name:String
private var type:String
private var deviceId:String
private var clientType:String
private var appInstanceId:String
private var version:String
private var addParams:[String:String] // causes failure
init(name:String, type:String, version:String, params:[String:String]) {
self.name = name
self.type = type
self.deviceId = Constants.Device.identifier!
self.version = version
self.clientType = "1"
self.appInstanceId = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
self.addParams = params
}
// json mapper
private enum CodingKeys:String, CodingKey {
case name = "name"
case type = "contentTypes"
case deviceId = "x-DeviceId"
case clientType = "x-ClientType"
case appInstanceId = "x-InstanceId"
case version = "version"
case addParams = "optionalParams"
}
func getJsonObject() -> [String:String]? {
do {
let encoded = try JSONEncoder().encode(self)
if let json = try JSONSerialization.jsonObject(with: encoded, options: []) as? [String : String] {
return json
}
} catch (let error) {
print("Error building JSON: \(error.localizedDescription)")
}
return nil
}
}
Without the the addParams field, the JSONSerialization works as expected. When I add the addParams field, which adds a nested dictionary to the object, the JSONSerialization fails and returns nil.
Can anyone give me a clue as to why I can't add a nested dictionary in this scenario?
Thanks!

It fails as one key (here it's the added addParams ) 's value isn't a String so the cast
as? [String : String] // causes failure
Won't occur , hence a nil json , so Replace
if let json = try JSONSerialization.jsonObject(with: encoded, options: [])
as? [String : String] {
with
if let json = try JSONSerialization.jsonObject(with: encoded, options: [])
as? [String : Any] {
Any encapsulates String and [String:String]

Related

Dictionary with Coding keys in Swift, Codale to String [duplicate]

This question already has answers here:
How can I make a Decodable object from a dictionary?
(2 answers)
Closed 6 months ago.
I have valid, working code, but I want to find out if there is a way to make it simpler and smaller.
I have a custom class Response which can be initialised from Json or text (depends on response from server)
public class Response: Codable {
let responseP1: String?
let responseP2: String?
let responseP3: String?
enum CodingKeys: String, CodingKey {
case responseP1 = "someResponseCode1"
case responseP2 = "someResponseCode2"
case responseP3 = "someResponseCode3"
}
required init(_ response: [String: String]) {
self.responseP1 = response["someResponseCode1"]
self.responseP3 = response["someResponseCode2"]
self.responseP2 = response["someResponseCode3"]
}
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.responseP1 = try container.decode(String.self, forKey: .responseP2)
self.responseP3 = try container.decode(String.self, forKey: .responseP1)
self.responseP2 = try container.decode(String.self, forKey: .responseP3)
}
}
can I combine Coding keys and initialisation with Dictionary somehow?
e.g. self.responseP1 = response[.responseP1]
but self.responseP1 = response[CodingKeys.responseP1.rawValue] is working but looks like I am winning nothing in this case
Also I need to parse it all to String, but
public func encodeAsString() -> String? {
do {
let encodedResponse = try self.encoded()
return String(decoding: encodedResponse, as: UTF8.self)
} catch {
return nil
}
}
does not work for me (returns "{}" even when was initialised from Json not Text), can you give advise why?
If your goal is just to be able to write self.responseP1 = response[.responseP1], then you need to convert the Dictionary to the right type [CodingKeys: String].
private static func convertKeys(from response: [String: String]) -> [CodingKeys: String] {
Dictionary(uniqueKeysWithValues: response.compactMap {
guard let key = CodingKeys.init(stringValue: $0) else { return nil }
return (key: key, value: $1)
})
}
required init(_ response: [String: String]) {
let response = Self.convertKeys(from: response)
self.responseP1 = response[.responseP1]
self.responseP3 = response[.responseP2]
self.responseP2 = response[.responseP3]
}

Swift Invalid type in JSON write error using JSONSerialization.data

I have an array with elements of a custom type. Here is the type :
public class RequestElemDataBody: Codable {
public var name: String
public var value: String
public init(name: String, value: String) {
self.name = name
self.value = value
}
}
This is how I declare my array :
var elementsInForm = [RequestElemDataBody]()
I use a function to convert this array to Data then to String :
func json(from object: [Any]) -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
return nil
}
return String(data: data, encoding: String.Encoding.utf8)
}
When executing I get this error message :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Data.RequestElemDataBody)'
I don't know what is wrong with my custom type since it is Codable.
How can I parse my array with my function without it throwing an error ?
You should use JSONEncoder when serializing Codable.
The example from the documentation page:
struct GroceryProduct: Codable {
var name: String
var points: Int
var description: String?
}
let pear = GroceryProduct(name: "Pear", points: 250, description: "A ripe pear.")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try encoder.encode(pear)
print(String(data: data, encoding: .utf8)!)
/* Prints:
{
"name" : "Pear",
"points" : 250,
"description" : "A ripe pear."
}
*/
Problem with your approach is that you try to encode the whole array. But your codable object is not an array at the moment.
Try passing single element to your method and return the string to work with it outside;
func json(from element: RequestElemDataBody) -> String {
...
}
As mentioned here the top level object can be Array or Dictionary and all objects can be instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
. In your case they are instances of an custom object.
So you can try converting the objects to dictionary first and then use JSONSerialization
And to convert to dictionary you can create an extension on Encodable
extension Encodable {
var dict : [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String:Any] else { return nil }
return json
}
}
and while calling function you can use ArrayName.compactMap({$0.dict})
Also you can use ObjectMapper library.
Reference from: How to convert a Swift object to a dictionary

Info.plist Dictionary to Dictionary of type Enum:String

I'm trying to take a dictionary from my Info.plist and save it as a Dictionary of type Enum: String
To better explain, I'm trying to convert the urlDict below to be a Dictionary of type but keep getting fatal error.
I can't figure out why
let enumDict = urlDict["keys"] as? Dictionary<URLKeys, String> does not work
The enum has a raw type of String and has a case for keyOne.
enum URLKeys: String {
case keyOne = "keyOne"
}
var keysDictionary = Dictionary<String, Any>()
keysDictionary["keyOne"] = "abc"
var urlDict = Dictionary<String, Any>()
urlDict["keys"] = keysDictionary
guard let enumDict = urlDict["keys"] as? Dictionary<URLKeys, String> else {
fatalError()
}
Use forEach and create a URLKeys for each key and cast each value to string
var enumDict:[URLKeys: String] = [:]
keysDictionary.forEach {
if let key = URLKeys(rawValue: $0.0), let value = $0.1 as? String {
enumDict[key] = value
}
}

Can't extract the dictionary as [String : Any] from vapor request context

I'm new to server side swift. I'm using vapor for server side swift. In vapor request I need to get JSON as [String: Any] to check a data type of the value like String, Int or Float. But in request the I can't find the exact data type of the value.
drop.post("post") { (request) -> ResponseRepresentable in
guard let name = request.data["value"]?.string else {
throw Abort.badRequest
}
return value
}
In above method, it directly converts and returns the value as String. I need to check it is a String or Int (some other data types too). I can't check by let condition which is given as below.
guard let name = data["value"] as? String else {
\\do something
}
I need to check it is a String or Int (some other data types too). If anyone has the solution, please let me know.
if you try to get Type of data using a type(of:) function, in result you will get optional any like this.
let dic = ["key":"string"] as [String : Any]
let str = dic["key"] //"string"
type(of: str) //Optional<Any>.Type
you have to check with " if let " for all case.
let dic = ["key":"string"] as [String : Any]
if let str = dic["key"] as? String {
print("String value")
} else if let intvalue = dic["key"] as? Int {
print("Int value")
} else if let boolvalue = dic["key"] as? Bool {
print("Bool value")
}
Hope it will help you
Since you're accessing the JSON, I'd do it like this.
guard let json = request.json else {
throw ...
}
if let string = json.string {
// JSON is a string
} else if let object = json.object {
// JSON is a [String: JSON]
}
// continue unwrapping for different data types
I wouldn't recommend using the type(of:) function for this purpose.

Is there a way to use guard statements more concisely?

I'm using Gloss for my JSON instantiation. Here is a sample class:
public class MyObj: Decodable
{
let id_user : String
let contact_addr1 : String
let contact_addr2 : String?
let contact_city : String
let contact_state : String
let contact_zip : String
let points : Int
// Deserialization
required public init?(json: JSON)
{
guard let id_user : String = "somekey" <~~ json else {
assertionFailure("MyObj - invalid JSON. Missing key: wouldbenicetonotwritethisforeachmember")
return nil
}
guard let contact_addr1 : String = "somekey" <~~ json else {
assertionFailure("MyObj - invalid JSON. Missing key: wouldbenicetonotwritethisforeachmember")
return nil
}
guard let contact_city : String = "somekey" <~~ json else {
assertionFailure("MyObj - invalid JSON. Missing key: wouldbenicetonotwritethisforeachmember")
return nil
}
guard let contact_state : String = "somekey" <~~ json else {
assertionFailure("MyObj - invalid JSON. Missing key: wouldbenicetonotwritethisforeachmember")
return nil
}
guard let contact_zip : String = "somekey" <~~ json else {
assertionFailure("MyObj - invalid JSON. Missing key: wouldbenicetonotwritethisforeachmember")
return nil
}
guard let points : Int = "somekey" <~~ json else {
assertionFailure("MyObj - invalid JSON. Missing key: wouldbenicetonotwritethisforeachmember")
return nil
}
self.id_user = id_user
self.contact_addr1 = contact_addr1
self.contact_addr2 = "somekey" <~~ json
self.contact_city = contact_city
self.contact_state = contact_state
self.contact_zip = contact_zip
self.contact_points = points
}
}
I have a lot of model classes. Hundreds of members between them. Writing a multi-line guard statement for each one really junks up my code. Is there any way I can encapsulate the guard functionality into something more concise? Maybe a function or something like:
shortGuard("memberName", "jsonKey")
Maybe there is a way to guard against an array of string keys?
There are a huge variety of ways to accomplish this. They all boil down to writing a wrapper function to map your keys to values. Here are a couple quick examples I thought of, but as I say there are many ways to do this depending on what you're after:
enum JSONError: Error {
case keyNotFound(String)
}
extension JSON {
func values<T>(for keys: [String]) throws -> [T] {
var values = [T]()
for key in keys {
guard let value: T = key <~~ self else {
throw JSONError.keyNotFound(key)
}
values.append(value)
}
return values
}
func values<T>(for keys: [String], closure: ((_ key: String, _ value: T) -> Void)) throws {
for key in keys {
guard let value: T = key <~~ self else {
throw JSONError.keyNotFound(key)
}
closure(key, value)
}
}
}
The first validates all keys before you can use any of them and will throw if one isn't present. You'd use it like so:
do {
let keys = ["foo", "bar"]
// The type of the values constant is important.
// In this example we're saying look for values of type Int.
let values: [Int] = try json.values(for: keys)
for (index, key) in keys.enumerated() {
print("value for \(key): \(values[index])")
}
} catch JSONError.keyNotFound(let key) {
assertionFailure("key not found \(key)")
}
The second one will pass back key, value pairs to a closure as they appear in your keys array and will throw at the first one it finds that doesn't exist.
do {
let keys = ["foo", "bar"]
// The type of the closure's value argument is important.
// In this example we're saying look for values of type String.
try json.values(for: keys) { (key, value: String) in
print("value for key \(key) is \(value)")
}
} catch JSONError.keyNotFound(let key) {
assertionFailure("key not found \(key)")
}
Using the first version in an init?() function for your class, we have something like this:
public struct MyObj: Decodable {
public let id_user : String
public let contact_addr1 : String
public let contact_addr2 : String?
public let points : Int
public init?(json: S) {
do {
let stringKeys = ["id_user", "contact_addr1"]
let stringValues: [String] = try json.values(for: stringKeys)
id_user = stringValues[0]
contact_addr1 = stringValues[1]
// this isn't required, so just extract with no error if it fails
contact_addr2 = "contact_addr2" <~~ json
let intKeys = ["points"]
let intValues: [Int] = try json.values(for: intKeys)
points = intValues[0]
} catch JSONError.keyNotFound(let key) {
assertionFailure("key \(key) not found in JSON")
return nil
} catch {
return nil
}
}
}
I have not used Gloss, and it mostly seems to be unnecessary considering that it is simple enough to parse JSON safely without needing an extra library, or using unfamiliar syntax.
Option 1:
You can group the optional unwrapping in a single guard statement.
Example:
public struct MyObj {
let id_user : String
let contact_addr1 : String
let contact_addr2 : String?
let points : Int
public init?(json: Any) {
guard
let entities = json as? [String : Any],
let id_user = entities["some key"] as? String,
let contact_addr1 = entities["some key"] as? String,
let points = entities["some key"] as? Int
else {
assertionFailure("...")
return nil
}
self.id_user = id_user
self.contact_addr1 = contact_addr1
self.contact_addr2 = entities["some key"] as? String
self.contact_points = points
}
}
Option 2:
Another approach would be to eliminate the guard statements altogether, and let the parser throw an error during parsing, and use an optional try to convert the result to nil.
Example:
// Helper object for parsing values from a dictionary.
// A similar pattern could be used for arrays. i.e. array.stringAt(10)
struct JSONDictionary {
let values: [String : Any]
init(_ json: Any) throws {
guard let values = json as? [String : Any] else {
throw MyError.expectedDictionary
}
self.values = values
}
func string(_ key: String) throws -> String {
guard let value = values[key] as? String else {
throw MyError.expectedString(key)
}
return value
}
func integer(_ key: String) throws -> Int {
guard let value = values[key] as? Int else {
throw MyError.expectedInteger(key)
}
return value
}
}
Parser:
public struct MyObj {
let id_user : String
let contact_addr1 : String
let contact_addr2 : String?
let points : Int
public init(json: Any) throws {
// Instantiate the helper object.
// Ideally the JSONDictionary would be passed by the caller.
let dictionary = try JSONDictionary(json),
self.id_user = try dictionary.string("some key"),
self.contact_addr1 = try dictionary.string("some key"),
self.points = try dictionary.integer("some key")
// Results in an optional if the string call throws an exception
self.contact_addr2 = try? dictionary.string("some key")
}
}
Usage:
// Instantiate MyObj from myJSON.
// myObject will be nil if parsing fails.
let myObject = try? MyObj(json: myJSON)