MYJSON is a simple Swift library to convert JSON to strongly typed objects.
-
Mapping JSON to objects.
-
Mapping objects to JSON.
-
Custom transformations.
-
Easy and safe using.
-
use_frameworks! pod 'MYJSON'
-
github "damonthecricket/my-json"
-
$ git submodule add https://github.com/damonthecricket/my-json.git
-
-
Copy MYJSON to the root of your project.
-
Add it in file inspector of your project.
-
Go to Targets -> Add MYJSON to Embedded Binaries.
-
-
iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+.
-
Xcode 8.
-
Swift 3.
MYJSON
is enum
, which represents JSON in two variations. MYJSON
takes on value dictionary
or array of dictionaries
.
You can initialize MYJSON
in the manner described below:
import MYJSON
let rawJSON: MYSJONType = [
"id": "sdfsf34324sdfdhg1"
"index": 98451
"first name": "Damon"
"last name": "Cricket"
]
let json: MYJSON = MYJSON(rawValue: rawJSON)
let jsonArray: MYJSON = MYJSON(rawValue: rawJSON)
switch json: {
case .value(let json):
// ...
// Code
// ...
default:
break
}
and
import MYJSON
let rawJSONArray: MYSJONType = [
["id": "sdfsf34324sdfdhg1",
"index": 98451,
"first name": "Damon",
"last name": "Cricket"],
["id": "fkgfg34kfg",
"index": 1234,
"first name": "Zee",
"last name": "Gor"]
]
let jsonArray: MYJSON = MYJSON(rawValue: rawJSONArray)
switch jsonArray: {
case .array(let jsonArray):
// ...
// Code
// ...
default:
break
}
or
import MYJSON
if let json = try? JSONSerialization.allowFragmentsJSON(with: data) {
// ...
// Code
// ...
}
-
In first case
MYJSON
instance is.value(let json)
, where value associated withJSON
instance (dictionary
, orMYJSONType
). -
In second case
MYJSON
instance is.array(let array)
, where value associated withJSON array
(orMYJSONArrayType
). -
In third case
MYJSON
instance initialized from rawJSON data
and hold associated value.
Note:
MYJSON init(rawValue: Any)
takes only two type of input parameter -MYJSONType
([String: Any]
) orMYJSONArrayType
([[String: Any]]
), otherwise initializer returns empty MYJSON.
MYJSON
has a useful properties and functions. For example, you can you can check wheter instance is dictionary
or array of dictionaries
:
import MYJSON
if json.isDictionary {
// Code
}
if json.isArray {
// Code
}
Also you can access to JSON associated value.
For dictionary
:
import MYJSON
var id: String = json["id"]
For array of dictionaries
:
import MYJSON
var json: MYJSONType = jsonArray[0]
Let's say we have a simple account class and gender enum:
enum Gender: String {
case male = "male"
case female = "female"
}
class Account {
var id: String = ""
var index: UInt = 0
var balance: String = ""
var age: UInt = 0
var gender: GenderTestEnum = .male
var name: String = ""
}
and JSON like this:
import MYJSON
let testJSON: MYJSONType =[
"id": "590c8ed16470876ae51b4bd8",
"index": 0,
"guid": "c0fcc2d9-415d-415f-a081-d9d9aedc4b9c",
"isActive": false,
"balance": "$1,897.29",
"picture": "http://placehold.it/32x32",
"age": 32,
"eyeColor": "green",
"name": "Fanny Hughes",
"gender": "female",
"company": "ZENTILITY"
]
We can add JSON deserialization to this class easily:
import MYJSON
extension Account: MYJSONDeserizlizable {
init(json: MYJSON) {
id <- json["id"]
index <- json.number(forKey: "index").uintValue
balance <- json["balance"]
age <- json.number(forKey: "index").uintValue
gender <- json["gender"]
name <- json["name"]
}
}
and after that we can deserialize JSON into Account instance very simple and easy:
import MYJSON
let json = MYJSON(rawValue: testJSON)
let account: Account = Account.self <- json
or in case of array of dictionaries
:
import MYJSON
let jsonArray = MYJSON(rawValue: testJSON)
let account: [Account] = Account.self <- jsonArray
Also MYJSON lib has a feature to serialize. For that we should to implement MYJSONSerizlizable protocol
, and specifically implement required protocol properry var json: MYJSON
.
Let's consider simple class Friend:
class Friend {
var id: UInt = 0
var name: String = ""
}
We can add JSON serialization to this class:
import MYJSON
extension Friend: MYJSONSerizlizable {
var json: MYJSON {
return MYJSON(rawValue: ["id": id, "name": name])
}
}
Now we can serialize Friend instance to MYJSON
:
import MYJSON
let friend: Friend = <...>
let json = friend.json
Then we have opportunity to serialize MYJSON
instance to Data
using JSONSerialization extension:
import MYJSON
if let data = try? JSONSerialization.prettyPrintedData(withJSON: json) {
// Code
}