GithubHelp home page GithubHelp logo

vincedev / swiftyjsonpatch Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 31 KB

A Swift 5 implementation for IETF JSON-patch (RFC 6902)

License: MIT License

Swift 99.21% Objective-C 0.79%
swift json json-patch

swiftyjsonpatch's Introduction

SwiftyJSONPatch

Carthage compatible

A Swift 5 implementation for IETF JSON-patch (RFC 6902).
iOS 8+
macOS 10.9+

Features

SwiftyJSONPatch defines JSONPatcher value type.
JSONPatcher applies JSON-patches to JSON documents.

Patching

import SwiftyJSONPatch

let json = """
  { "foo": 1, "bar": "baz", "baz": null }
""".data(using: .utf8)!

let patch = """
  [
    { "op": "test", "value": "baz", "path": "/bar" },
    { "op": "remove", "path": "/baz" },
    { "op": "add", "path": "/toto", "value": [1, 2, 3] },
    { "op": "replace", "path": "/toto/2", "value": "baz" },
    { "op": "copy", "from": "/foo", "path": "/toto/0" },
    { "op": "move", "from": "/bar", "path": "/foo" },
  ]
""".data(using: .utf8)!

let patcher = JSONPatcher()
do {
    let patched = try patcher.apply(patch, on: json)
    if let result = String(data: patched, encoding: .utf8) {
        print(result) // {"foo":"baz","toto":[1,1,2,"baz"]}
    }
} catch {
    print("Error: \(error)")
}

Error Handling

The JSONPatcher.PatchingError enum represents error cases JSONPatcher may throw while applying a patch :

public enum PatchingError: Error {
    case document(Error)                    // failed to decode document JSON data
    case patch(Error)                       // failed to decode patch JSON data
    case operation(String, OperationError)  // failed to apply specific patch operation
}

The associated values gives you details about what have gone wrong. The JSONPatcher.OperationError describes error cases while trying to perform patch operations :

public enum OperationError: Error {
    case indexOutOfBounds(Int)          // index out of bounds (index)
    case memberNotFound(String)         // member not found (name)
    case notACollection                 // neither an array, nor an object
    case notAnObject                    // not an object
    case testFailure                    // test operation failed
    case illegalMove(String, String)    // wrong paths in move operation (from, path)
}

Getting Started

Installation

Carthage

Add github "vincedev/SwiftyJSONPatch" to your Cartfile.

Building SwiftyJSONPatch

You'll need SwiftLint to build the framework from source.
Clone the repository, open SwiftyJSONPatch.xworkspace.
Build the target for your preferred platform.

Getting Help

Open an issue if you have found a bug, or have a feature request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.