Latest 1.0.0
Homepage https://github.com/damonthecricket/my-json
License MIT
Platforms osx 10.9, ios 8.0, watchos 2.0, tvos 9.0, requires ARC
Authors

Version
Travis CI
CocoaPods
Carthage compatible
GitHub license
Platform

MYJSON
Swift JSON framework.

MYJSON is a simple Swift library to convert JSON to strongly typed objects.

  1. Features
  2. Installation
  3. Requirements
  4. Usage

Features

  • Mapping JSON to objects.

  • Mapping objects to JSON.

  • Custom transformations.

  • Easy and safe using.

Installation

  • CocoaPods

    use_frameworks!
    
    pod 'MYJSON'
  • Carthage

    github "damonthecricket/MYJSON"
  • Git

    $ git submodule add https://github.com/damonthecricket/my-json.git
  • Manually

    • Copy MYJSON to the root of your project.

    • Add it in file inspector of your project.

    • Go to Targets -> Add MYJSON to Embedded Binaries.

Requirements

  • iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+.

  • Xcode 8.

  • Swift 3.

Usage

JSON

Initialization

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
      // ...
}
  1. In first case MYJSON instance is .value(let json), where value associated with JSON instance (dictionary, or MYJSONType).

  2. In second case MYJSON instance is .array(let array), where value associated with JSON array (or MYJSONArrayType).

  3. In third case MYJSON instance initialized from raw JSON data and hold associated value.

Note: MYJSON init(rawValue: Any) takes only two type of input parameter – MYJSONType ([String: Any]) or MYJSONArrayType ([[String: Any]]), otherwise initializer returns empty MYJSON.

Properties

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]

Deserializing

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

Serializing

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
}

Latest podspec

{
    "name": "MYJSON",
    "version": "1.0.0",
    "summary": "Simple Swift JSON framework.",
    "homepage": "https://github.com/damonthecricket/my-json",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Damon Cricket": "[email protected]"
    },
    "social_media_url": "https://www.facebook.com/damonthecricketprograms/",
    "platforms": {
        "osx": "10.9",
        "ios": "8.0",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "source": {
        "git": "https://github.com/damonthecricket/my-json.git",
        "tag": "1.0.0"
    },
    "source_files": "MYJSON/*.{h,swift}",
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "3.0"
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This