Just a simple Swift wrapper for https://github.com/benjamine/jsondiffpatch
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
Create a Package.swift
file.
dependencies: [
.Package(url: "https://github.com/PhillippBertram/JsonDiffPatchSwift.git", majorVersion: 0)
]
$ swift build
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.
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
]
}
}
}
}