Generalized JSON parser for Swift - ios

In our iOS project, we are using SwiftyJSON and ObjectMapper to parse JSON responses and store them in models. However, for the models, we have to manually specify the mapping. eg; if I have a model class called User, which has name and age as properties, then while parsing, I have to specify the following in the User class:
func mapping(map: Map) {
name <- map["Name"]
age <- map["Age"]
}
Doing the same for all models is tedious and time consuming. Isn't there an approach to generalize the parsing? Like I pass any JSON to a function and specify the model, and the function should return me the model object with the parsed values, if they're available. I don't want to write a separate mapping for each model.

I think you should take a look at EVReflection, a Swift 3 library which does what you are looking for.
Update:
Swift 4 (now in Beta) seems to be bringing some changes which are relevant to this question.
struct MyStruct: Codable {
var str: String
var num: Int
}
let myStruct = MyStruct(str: "test", num:5)
let encoder = JSONEncoder()
let jsonData = try encoder.encode(myStruct)
let json = String(data: jsonData, encoding: .utf8)
print(json) // prints {"str": "test", "num": 5}
// Also decoding
let decoder = JSONDecoder()
let decoded = try decoder.decode(MyStruct.self, from: jsonData)
// decoded is a structure of type MyStruct
print(decoded.str) // test

Related

Decode JSON array inside a JSON dictionary without creating Boilerplate Types

The JSON:
let jsonString = """
{
"groups": [
{
"id": "oruoiru",
"testProp": "rhorir",
"name": "* C-Level",
"description": "C-Level"
},
{
"id": "seese",
"testProp": "seses",
"name": "CDLevel",
"description": "CDLevel"
}
],
"totalCount": 41
}
"""
Type:
struct Group: Codable {
var id: String
var name: String
}
I would like to decode this JSON to only output an array of Group type without having to create boilerplate type like:
struct GroupsResponse: Codable {
var groups: [Group]
var totalCount: Int
}
and use:
let data = jsonString.data(using: .utf8)
let decoded = try! JSONDecoder().decode([Group].self, from: data!)
I tried getting the containers from inside the initialiser of the Group type, but the program already crashes outside at the decoding line with Swift.DecodingError.typeMismatch error
One solution that does work is doing something like:
let topLevel = try! JSONSerialization.jsonObject(with: data) as? [String: Any]
let groupsNode = topLevel?["Groups"] as? [[String: Any]]
let groups = try! JSONSerialization.data(withJSONObject: groupsNode!)
let decoded = try! JSONDecoder().decode([Group].self, from: groups)
but this seems very hacky. Is there an elegant way to handle this?
You cannot avoid the top level response struct using JSONDecoder. There has to be a type for it to work on. And you can't use Dictionary as the top level object (ie [String: [Group]]), since there's a totalCount field that doesn't have an array of Group. All the comments are correct. Just write the little wrapper. It's one line long:
struct GroupsResponse: Codable { var groups: [Group] }
There's no need to decode fields you don't care about.
But you said "for education," and it's all code, so of course you can replace JSONDecoder with something that can do this. You tried to do that with NSJSONSerialization, but that's extremely clunky. You can also just write your own version of JSONDecoder, and then do it like this:
let decoder = RNJSONDecoder()
let response = try decoder.decode(JSON.self, from: json)
let groups = try decoder.decode([Group].self, from: response.groups)
This avoids any re-encoding (RNJSONDecoder can decode directly from a JSON data structure; it doesn't have to convert it back to Data first). It also requires about 2600 lines of incredibly tedious boilerplate code, mostly copy and pasted out of stdlib. Implementing your own Decoder implementation is obnoxious.
If you wanted to get fancier, you could scan the data for the section that corresponds to the property you want, and decode just that part. While implementing the Decoder protocol is very hard, parsing JSON is quite straight-forward. I'm currently doing a bunch of JSON experiments, so I may try writing that and I'll update this if I do.
But the answers are: "just write the tiny, simple, fast, easy to understand response wrapper," or "replace JSONDecoder with something more powerful."
UPDATE:
I went ahead and built the scanner I mentioned, just to show how it could work. It's still a bit rough, but it allows things like:
let scanner = JSONScanner()
let groupJSON = try scanner.extractData(from: Data(jsonString.utf8),
forPath: ["groups", 1])
let group = try JSONDecoder().decode(Group.self, from: groupJSON)
XCTAssertEqual(group.id, "seese")
So you can just extract the part of the data you want to parse, and not worry about parsing the rest.

How to model data from JSON array with no property name into swift so It can be parsed? SWIFT

Im stuck trying to model a JSON array which has no property name into my swift project with the goal of parsing the data and using it in my app. I know how to do this when there is a NAME for the array but I don't know how to make swift and this lackluster JSON understand each other. The path to the first "Company" in the JSON is "0.Company". The error I get is "Value of type 'WorkData' has no member '0'"
Im including pictures of my full project so it is easier to understand the structure of the code and what im trying to do. Please look at the picture for a clearer understanding I apologize if Im not explaining it well i'm new to programming.
import Foundation
class WorkData: Codable {
let WorkData: [WorkData]
let Company: String
let worklogDate: String
let issue: String
}
func parseData(jsonDataInput: Data) {
let decoder = JSONDecoder() // an object that decodes JSON data
do {
let decodedData = try decoder.decode(WorkData.self, from: jsonDataInput)
let Company = decodedData.0.Company
let worklogDate = decodedData.0.worklogDate
let issue = decodedData.0.issue
} catch {
print (error)
}
}
}
json
Trying to model JSON in Swift
Parsing JSON
You cannot start JSON with an array because JSON itself is an object {}
See example below:
{
"WorkData" : [
{"Company" : ""},
{"Company" : ""},
{"Company" : ""}
]
}
let decodedData = try decoder.decode(LocalizationsResponse.self, from: jsonDataInput)
decodedData will be an array

How do I stop Swift from injecting escaped single quotes in my [String]?

I'm attempting to print out a string array without escaped single quotes. For some reason, Swift is injecting escaped single quotes when printing my array. This has a trickle down problem when I use the array to build JSON. JSON ends up not being able to parse due to the escaped single quotes.
I thought this was a problem with my code, but I've distilled this down to a single usecase that should be straightforward.
let names = ["Tommy 'Tiny' Lister", "Tom Hanks"]
print(names)
The output is:
["Tommy \'Tiny\' Lister", "Tom Hanks"]
Note: I did not include escaped single quotes in my names array.
How do I prevent this from happening?
Here is the what I'm doing later in code to create JSON. For purposes of brevity, this is a really dumbed down version of what I'm doing:
var names = ["Tommy Tiny Lister", "Tom Hanks"]
var jsonString = """
{"names": \(names)}
"""
var jsonData = jsonString.data(using: .utf8)
if let json = try? JSONSerialization.jsonObject(with: jsonData!) as? [String: Any] {
let jsonData = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let string = String(data: jsonData, encoding: .utf8)
print(string!)
}
What you are doing is using Swift arrays' description to generate JSON:
var jsonString = """
{"names": \(names)}
""" // your JSON will contain name.description
You are relying on the fact that the implementation of description just so happens to result in the same format as JSON most of the time. As you can see, when there are ' in the array elements, the description is not valid JSON.
Basically, you should not rely on the description to generate JSON. Instead, you should use Codable to generate JSON data from a Swift array.
For the JSON you want to produce:
{
"names": ["Tommy 'Tiny' Lister", "Tom Hanks"]
}
You can use a struct like this:
struct Names : Codable {
let names: [String]
}
And then you can produce JSON Data like this:
let encoder = JSONEncoder()
do {
let obj = Names(names: ["Tommy 'Tiny' Lister", "Tom Hanks"])
let data = try encoder.encode(obj)
} catch { ... }

Swift: Converting from _PFEncodedString to String (export of Core Data attributes using JSONEncoder)

I’m coding Swift for iOS 11, and in some context (described in part 2) I end up with a variable of type String. However, when examining the variable in Xcode’s debugger the type is shown as _PFEncodedString (which I have read elsewhere is an internal subclass of String). For some reason (described below, but not directly related to the first part of my question) I want to transform the _PFEncodedString variable to a plain String variable. The only way I’ve found to do that is as follows:
var attributeName : String
// ... attributeName is assigned a value and ends up being _PFEncodedString
let data = attributeName.data(using: String.Encoding.utf8)!
let plainName = String(data: data, encoding: String.Encoding.utf8)! // this is type String not _PFEncodedString
The first part of my question is: Is this conversion safe or is there is simpler or better way to do it?
Part 2. For the second part of my question I will first describe the context where the above becomes an issue. I’m using Core Data and want to export attribute names (and values) to JSON. Using reflection I can find Core Data entity names and find the attribute names that should be exported to JSON. The problem is that when I use JSONEncoder with this data, the generated JSON contains Chinese characters whereas the Core Data model attribute names do not. I have found that JSONEncoder produces partly Chinese output when Strings in the exported object is of type _PFEncodedString, but works fine when handling "plain" String types. Therefore the first part of my questions.
Here is code that illustrates the problem, boiled down to a near minimum. QPTVideo is a subclass of NSManagedObject with two attributes ‘qaUUID’ and ‘thumbnailData’.
// First define a struct to be used with JSONEncoder
struct AttributeForJSON : Codable {
var name : String
}
let entityDescription = QPTVideo.entity()
let attributes = entityDescription.attributesByName // this is an array of (key: String, value: NSAttributeDescription).
var allAttributes : [AttributeForJSON] = [] // this will be an array of all the attribute names for export to JSON.
for attribute in attributes {
let attributeName = attribute.key // NOTE: debugger shows this as: attributeName String class name = _PFEncodedString
let aj = AttributeForJSON(name: attributeName)
print(aj.name) // this works just fine, printing “qaUUID” or “thumbnailData”
allAttributes.append(aj) // when exported, the attribute names look nothing like they should, containing Chinese characters.
// now use the work-around from first part of my question:
let data = attributeName.data(using: String.Encoding.utf8)!
let plainName = String(data: data, encoding: String.Encoding.utf8)! // NOTE: debugger shows this as: plainName String "thumbnailData"
let ajplain = AttributeForJSON(name: plainName)
allAttributes.append(ajplain) // when exported, the attribute names are correct.
}
// the rest is just use of JSONEncoder to generate the JSON
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
do {
let data = try encoder.encode(allAttributes)
try data.write(to: URL(fileURLWithPath: “/Users/someone/so.json"), options: Data.WritingOptions.atomic)
} catch {
print(error.localizedDescription)
}
The exported JSON looks like this (where I would have expected both of the first two name values to be "qaUUID", and the next two to be "thumbnailData"):
[
{
"name" : "慱啕䑉\u0000戨্"
},
{
"name" : "qaUUID"
},
{
"name" : "桴浵湢楡䑬瑡a\u0000戨্\u0001\u0000\u0000"
},
{
"name" : "thumbnailData"
}
]
So, my here question is: Why do I get Chinese characters? What am I doing wrong in how I use the Core Data reflection API and/or JSONEncoder? How do I do this without resorting to the work-around from the first part of my question?

How to pass model class data from one page another in iOS?

Here I won't have any connection for both the pages and here I need to save the model class globally and to use anywhere in all pages till app was in use and after it may can clear the data having in array but I can able to access anywhere in all pages in app and I tried using to save in UserDefaults it crashed. Can anyone help me how to implement this?
var sortModel = [Sort]()
for (_, value) in sortJson as! [String: Any] {
self.sortModel.append(Sort.init(dict: value as! [String : Any]))
}
UserDefaults.standard.set(self.sortModel, forKey: "sorts")
Get your Sort struct to conform to Codable like:
struct Sort: Codable {
//...
}
then you can quickly get away with:
//Encode Sort to json data
if let jsonData = try? JSONEncoder().encode(sortModel) {
print("To Save:", jsonData)
//Save as data
UserDefaults.standard.set(jsonData,
forKey: "sorts")
}
//Read data for "sorts" key and decode to array of "Sort" structs
if let data = UserDefaults.standard.data(forKey: "sorts"),
let sorts = try? JSONDecoder().decode([Sort].self,
from: data) {
print("Retrieved:", sorts)
}
Basically, we make a json out of the array and save it as a data object.
We then can get it back as data and recreate the sort struct array.
NOTE: This may not work if the struct has nested within itself some types that prevent it from getting encoded as a json.
In this case, read:
http://swiftandpainless.com/nscoding-and-swift-structs

Resources