Sunday, 18 November 2018

JSON-Patch: Applying deltas to JSON Documents in Swift

I recently started looking at saving bandwidth between a client and server that communicates with JSON. Some of these messages send large JSON documents that do not change very frequently and when there are changes, they are small relative to the whole document. My search lead me to JSON-Patch  a IETF proposed standard (RFC 6902).  JSON-Patch is a patch format which itself is specified in JSON, it can perform operations on a target JSON document to transform it to the intended state. The standard has the following operations: -

  • add - Insert / Replace a value at the given location with a new value.
  • remove - Remove a value at the given location (value must exist).
  • replace - Replace a value at the given location with a new value (value must exist).
  • move - Move a value at the given location to a new location. Logically equivalent to a remove then add. (value must exist)
  • copy - Copies a value at the given location to a new location (value must exist).
I looked for implantations of JSON-Patch in Swift, and found one but it had dependences on SwiftlyJSON another swift module. Which is great if you're already using it in your project, but I wanted to work with the JSONSerialization that comes in Foundation directly. So I wrote my own JSONPatch swift module. This initial version can only apply patches, it does not currently generate patches based on two JSON documents. Project available here on GitHub.

Usage

JSONPatch can work with the Data representations of JSON.
However if you are already parsing the JSON with JSONSerialization within your project then JSONPatch can work with the parsed objects you already have. This can be especially useful if you want to apply a patch relative to sub-element of the whole JSON document.

6 comments:

  1. I need to communicate my deference of your composing aptitude and capacity to make perusers read from the earliest starting point as far as possible. I might want to peruse more up to date presents and on share my musings with you.

    360DigiTMG

    ReplyDelete
  2. This post is incredibly simple to examine and recognize without disregarding any nuances. Inconceivable work!

    hrdf scheme

    ReplyDelete
  3. You re in motivation behind fact an on-target site administrator. The site stacking speed is amazing. It kind of feels that you're doing a specific trick. What's more, The substance is a masterpiece. you have done a marvelous development concerning this issue!
    360DigiTMG Data Analytics Course

    ReplyDelete
  4. It is the expect to give noteworthy information and best takes a shot at, including a perception of the regulatory cycle.
    https://360digitmg.com/course/certification-program-in-data-science

    ReplyDelete
  5. an extremely wonderful post this is. Genuinely, perhaps the best post I've at any point seen to find in as long as I can remember. Goodness, simply keep it up.
    what is hrdf claimable

    ReplyDelete
  6. This is a great motivational article. In fact, I am happy with your good work. They publish very supportive data, really. Continue. Continue blogging. Hope you explore your next post
    HRDF training

    ReplyDelete