Latest 0.1.0
Homepage https://github.com/phillippbertram/JsonDiffPatchSwift
License MIT
Platforms ios 8.0
Authors

Swift Version
Swift Package Manager compatible
Version
License: MIT
Bitrise

Just a simple Swift wrapper for https://github.com/benjamine/jsondiffpatch

Installation

CocoaPods

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

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

target '<Your Target Name>' do
    pod 'JsonDiffPatchSwift', '~> 0.1.0'
end

Then, run the following command:

$ pod install

Swift Package Manager

Create a Package.swift file.

dependencies: [
    .Package(url: "https://github.com/PhillippBertram/JsonDiffPatchSwift.git", majorVersion: 0)
]
$ swift build

Usage

This library just uses apples native JavaScriptCore framework JSContext to wrap https://github.com/benjamine/jsondiffpatch into swift by evaluating javascript code.
Currently there is only one function provided by jsondiff.json called delta. That function requires two json strings and creates the appropriate delta generated by the jsondiffpatch library.

Example

left.json

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

right.json

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Save", "onclick": "SaveDoc()"}
    ]
  }
}}
import JsonDiffPatchSwift

let left: String = // ...
let right: String = // ...

let delta: [String: Any] = JsonDiffPatch.delta(source: left , target: right)

// or alternatively with error handling
do {
  let delta = try JsonDiffPatch.deltaOrFail(source: left, target: right)
} catch {
  // handle error
}

delta

{
  "menu": {
    "popup": {
      "menuitem": {
        "1": {
          "value": [
            "Open",
            "Save"
          ],
          "onclick": [
            "OpenDoc()",
            "SaveDoc()"
          ]
        },
        "_t": "a",
        "_2": [
          {
            "value": "Close",
            "onclick": "CloseDoc()"
          },
          0,
          0
        ]
      }
    }
  }
}

Latest podspec

{
    "name": "JsonDiffPatchSwift",
    "version": "0.1.0",
    "license": "MIT",
    "summary": "Simple jsondiffpatch wrapper for Swift",
    "homepage": "https://github.com/phillippbertram/JsonDiffPatchSwift",
    "authors": {
        "Phillipp Bertram": "[email protected]"
    },
    "source": {
        "git": "https://github.com/phillippbertram/JsonDiffPatchSwift.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Sources/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This