CoreSync is an Objective-C diff/patch framework that supports the JSONPatch specification.
First, CoreSync can efficiently calculate the difference (the deltas) between two NSDictionary objects (of which can be represented as JSON). This is the diff
part of CoreSync, which is also known as a binary log.
The second part of CoreSync is the patch
functionality. Once the diff
is calculated, CoreSync can apply each patch transaction to the original dictionary, bringing it up-to-date.
It’s particularly useful for syncing (as the name suggests).
Note: I’m also working on a fully Swift version of CoreSync. I’m excited about it, but CoreSync leverages a significant amount of Objective-C’s dynamism, which Swift doesn’t provide.
CoreSync is flexible, modular, and lightweight. It’s also extremely easy to use in your projects, and comes with no dependencies.
- Download CoreSync.
- Try it out by opening the Xcode project and running the tests via the
CoreSyncTests
target.CoreSyncTests.m
also provides an easy example of how to use the framework. - Install it in your own project by simply adding the CoreSync folder into your target, and
#import "CoreSync.h"
.
In the simplest case, suppose we have two NSDictionary
s (or JSON blobs) A
and B
.
A
{
"a" : "aValue1",
"c" : "cValue1"
}
B
{
"a" : "aValue2",
"b" : "bValue1"
}
In this trivial example, we can calculate the delta ourselves. Three changes must take place for A
to mirror B
:
- Replace the value of key
a
fromaValue1
toaValue2
- Remove key
c
- Add key
b
with valuebValue1
Now let’s have CoreSync calculate the delta (and print it as a JSON blob), knowing what output to expect:
#import "CoreSync.h"
...
NSDictionary* A = ...
NSDictionary* B = ...
NSString* JSONChanges = [CoreSync diffAsJSON:A :B];
NSLog(@"%@", JSONChanges);
As expected, this will print:
[
{
"op" : "replace",
"path" : "/a",
"value" : "aValue2"
},
{
"op" : "remove",
"path" : "/c"
},
{
"op" : "add",
"path" : "/b",
"value" : "bValue1"
}
]
With this JSON diff, we can now patch A
and verify that A
and B
are now identical in value:
A = [CoreSync patch:A withJSON:JSONChanges];
assert([A isEqualToDictionary:B]);
Obviously, CoreSync will also efficiently and intelligently handle complex diffs, nested collections, value type changes, and more. It’s pretty powerful!
- If you need help, just ask! Email and Twitter both work.
- If you found a bug, or have a feature request, open an issue.
- If you would like to contribute, that’s awesome! Open up a pull request!
Enjoy!