Latest 1.0.0
Homepage https://github.com/tomquist/CoreJSON
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0
Authors

Build Status
codecov.io
CocoaPods Compatible
Swift Package Manager Compatible
Platform support
License MIT

CoreJSON provides a simple JSON data model and on top of that a collection of utilities.

Modules

CoreJSON consists of several modules:

  • Core: Provides a simple JSON data model which conforms to Equatable and Hashable
  • Convenience: Provides some convenient value accessors to extract JSON content without checking the JSON node type
  • Subscript: Allows subscripting JSON values by key (for objects) or index (for arrays)
  • Literals: Allows converting an Integer, Nil, String, Float, Boolean, Array and Dictionary literal into JSON
  • Foundation: Converts the result of (NS)JSONSerialization into CoreJSONs JSON model and vice-versa
  • Pointer: Implementation of JSON pointer (rfc6901)

Demo

Core

import CoreJSON

// CoreJson provides a very simple JSON data model
let simpleJson = JSON.array([
        JSON.bool(true),
        JSON.string("string"),
        JSON.null,
        JSON.number(.int(10)),
        JSON.object([:])
    ])

// JSON is equatable and hashable
simpleJson == simpleJson // -> true
JSON.bool(true) == .null // -> false
simpleJson.hashValue // -> Some hash value

Literals

import CoreJSONLiterals

var json: JSON = [
    "books": [
        [
            "name": "Sample Book",
            "pages": 141,
            "author": [
                "firstName": "John",
                "lastName": "Doe",
                "age": 43
            ]
        ],
        [
            "name": "How to not create code examples",
            "pages": 53,
            "author": [
                "firstName": "Mike",
                "lastName": "Miller",
                "age": 27
            ]
        ]
    ]
]

Subscript

import CoreJSONSubscript

var firstBook = json["books"]?[0] // -> JSON.object(...)
let bookName = firstBook?["name"] // -> JSON.string("Sample Book")
var jsonCopy = json
jsonCopy["books"]?[1] = .string("changed")
jsonCopy["books"]?[1] // -> JSON.string("changed")

Convenience

import CoreJSONConvenience

bookName?.stringValue // -> "Sample Book"
firstBook?["pages"]?.intValue // -> 141
firstBook?["pages"]?.intValue = 100
firstBook?["pages"]?.intValue // -> 100

Pointer

import CoreJSONPointer

json["/books/1/author/lastName" as JSONPointer]?.stringValue
json["/books/-" as JSONPointer] = [
            "name": "JSON for dummies",
            "pages": 612,
            "author": [
                "firstName": "Richard",
                "lastName": "Roe",
                "age": 34
            ]
        ]

Foundation

import CoreJSONFoundation
import Foundation
let jsonData = try! JSONSerialization.data(withJSONObject: json.toFoundation(), options: .prettyPrinted)
String(data: jsonData, encoding: .utf8) // -> JSON string
let parsedJson = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments)
try! JSON(foundation: parsedJson)

Latest podspec

{
    "name": "CoreJSON",
    "version": "1.0.0",
    "summary": "Core JSON data model and utilities",
    "description": "CoreJSON provides a simple JSON data model and on top of it serveral extension modulesnlike convenience value accessors, subscripting support, literal initializers, JSONnpointers (rfc6901) and conversion from/to Foundation JSON produced and consumed bynJSONSerialization.",
    "homepage": "https://github.com/tomquist/CoreJSON",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Tom Quist": ""
    },
    "social_media_url": "https://twitter.com/tomqueue",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/tomquist/CoreJSON.git",
        "tag": "1.0.0"
    },
    "default_subspecs": "Core",
    "pushed_with_swift_version": "3.0n",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Sources/CoreJSON/*.swift"
        },
        {
            "name": "Convenience",
            "source_files": "Sources/CoreJSONConvenience/*.swift",
            "dependencies": {
                "CoreJSON/Core": []
            }
        },
        {
            "name": "Foundation",
            "frameworks": "Foundation",
            "source_files": "Sources/CoreJSONFoundation/*.swift",
            "dependencies": {
                "CoreJSON/Core": []
            }
        },
        {
            "name": "Literals",
            "source_files": "Sources/CoreJSONLiterals/*.swift",
            "dependencies": {
                "CoreJSON/Core": []
            }
        },
        {
            "name": "Pointer",
            "frameworks": "Foundation",
            "source_files": "Sources/CoreJSONPointer/*.swift",
            "dependencies": {
                "CoreJSON/Core": []
            }
        },
        {
            "name": "Subscript",
            "source_files": "Sources/CoreJSONSubscript/*.swift",
            "dependencies": {
                "CoreJSON/Core": []
            }
        }
    ]
}

Pin It on Pinterest

Share This