Latest 1.0
Homepage https://github.com/peterringset/JSONPatch
License MIT
Platforms ios 9.0, osx 10.10
Authors

JSONPatch

CocoaPods Compatible
Carthage Compatible
MIT Licence

A Swift μ-framework for creating RFC6902 compliant JSON patch objects

Requirements

  • iOS 8.0+ / macOS 10.10+
  • Swift 4.2+

IMPORTANT!

The framework relies on the Objective C runtime for converting keypaths into strings, and so it is crucial that the properties the keypaths point to are representable in Objective C. To achieve this, you will have to add the @objc annotation at each variable declaration.

class Patch: NSObject {
    @objc var baz: String!
    @objc var foo: String!
    @objc var hello: [String]!
}

Installation

Carthage

To integrate JSONPatch into your Xcode project using Carthage, specify it in your Cartfile:

github "peterringset/JSONPatch" ~> 1.0

CocoaPods

To integrate JSONPatch into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'PRJSONPatch', '~> 1.0'
end

Usage

Create JSON patch objects using keypath’s in Swift:

import JSONPatch

let stringChanges: [JSONPatch<Patch, String>] = [
    .replace(.baz, value: "boo"),
    .remove(.foo)
]
let arrayChanges: [JSONPatch<Patch, [String]>] = [
    .add(.hello, value: ["world"])
]

Then, once you’ve created a collection of changes you can use JSONEncoder to convert it to json data:

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

let data = try! encoder.encode(changes)
print(String(data: data, encoding: .utf8)!)

This will print the following json:

[
  {
    "op" : "replace",
    "path" : "/baz",
    "value" : "boo"
  },
  {
    "op" : "remove",
    "path" : "/foo"
  },
  {
    "op" : "add",
    "path" : "/hello",
    "value" : [
       "world"
    ]
  }
]

JSONPatch supports all the verbs specified in RFC6902, add, remove, replace, move, copy and test.

Latest podspec

{
    "name": "PRJSONPatch",
    "version": "1.0",
    "summary": "A Swift u03bc-framework for creating RFC6902-compliant JSON patch objects",
    "description": "A Swift u00b5-framework for creating RFC6902-compliant JSON patch objects.nnThe framework uses the built in Codable protocol to convert from Swift enum to json objects.",
    "homepage": "https://github.com/peterringset/JSONPatch",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "peterringset": "[email protected]"
    },
    "source": {
        "git": "https://github.com/peterringset/JSONPatch.git",
        "tag": "1.0"
    },
    "source_files": [
        "JSONPatch",
        "JSONPatch/**/*.{h,swift}"
    ],
    "swift_version": "4.2",
    "platforms": {
        "ios": "9.0",
        "osx": "10.10"
    }
}

Pin It on Pinterest

Share This