CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

JSONNode 1.2.5

JSONNode 1.2.5

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Keith Moon.



JSONNode 1.2.5

JSONNode

A simple, Swifty way to interact with JSON.

JSONNode is simple, useful, and under 150 lines of code.

Advantage over traditional JSON handling

Let’s take the response from the ImgFlip Meme API

{
    "success": true,
    "data": {
        "memes": [
              {
                  "id": "61579",
                  "name": "One Does Not Simply",
                  "url": "http://i.imgflip.com/1bij.jpg",
                  "width": 568,
                  "height": 335
              },
              {
                  "id": "101470",
                  "name": "Ancient Aliens",
                  "url": "http://i.imgflip.com/26am.jpg",
                  "width": 500,
                  "height": 437
              }
          ]
    }
}

To retrieve the name for the first meme, you would do the following:

var jsonObject = try! JSONSerialization.jsonObject(with: jsonData, options: [.allowFragments])

guard
    let jsonDictionary = jsonObject as? [String: Any],
    let dataDictionary = jsonDictionary["data"] as? [String: Any],
    let memesArray = dataDictionary["memes"] as? [[String: Any]],
    let firstMeme = memesArray.first,
    let memeName = firstMeme["name"] as? String else {

        fatalError()
}

print(memeName)

With JSONNode this becomes the following:

var jsonNode = JSONNode(data: jsonData)!
guard let name = jsonNode["data"]["memes"][0]["name"].string else { fatalError() }

print(name)

How it works

Typically when JSON is deserialised, it is turned into nested arrays and dictionary, and since these collections need a type and the JSON could contain any of multiple types, the non-specific Any protocol is used.

This is not a true representation of the situation though. There can’t be “anything” in the collection. Each node in the JSON structure can only be one of the following:

  • String
  • Boolean
  • Integer
  • Floating point number
  • Array containing something in this list
  • Dictionary with a string key and value as something in this list

With this finite list of possible values for a JSON node, a enum is the best way to represent that. Inspiration was taken from the way that Swift Playgrounds passes information been the playground page and the live view.

Requirements

  • iOS 8.0+ | macOS 10.7+ | tvOS 9.0+ | watchOS 2.0+
  • Swift 3
  • Xcode 8

Integration

Manually (iOS 8+, OS X 10.9+)

To use this library in your project manually you may:

  1. for Projects, just drag JSONNode.swift to the project tree
  2. for Workspaces, include the whole JSONNode.xcodeproj