Skip to content

About

JSON Patch RFC6902 implementation in Swift

Resources

Stars

25 stars

Watchers

1 watching

Forks

Repository files navigation

JSONPatch — Swift 6 JSON Patch implementation

Apache 2 License Supported Platforms Build System

JSONPatch is a Swift library that implements JSON Patch RFC6902. JSONPatch uses JSONSerialization from Foundation, and has no dependencies on third-party libraries.

The implementation uses the JSON Patch Tests project for unit tests to validate its correctness.

Requirements

Use Swift 6.0 or later. Apple development requires Xcode 16 or later. The library supports macOS 10.15+, iOS 13+, tvOS 13+, watchOS 6+, and Linux. Tests run on macOS 14+ or iOS/tvOS 17+ hosts; these test requirements do not change the library's deployment targets.

Release

Version 2.0.0 introduces the Swift 6 migration. See the migration guide for the breaking error-payload change and the changelog for release notes. Version 1.0.6 added Linux support.

Installation

Swift Package Manager (recommended)

Use Swift Package Manager for new integrations and future releases. See the Swift Package Manager installation guide for Xcode and Package.swift instructions.

CocoaPods

Existing CocoaPods users can install version 2.0 directly from the v2.0.0 Git tag. This bypasses public Trunk, which is scheduled to become read-only on December 2, 2026. See the CocoaPods installation guide for the Podfile configuration and guidance for users staying on version 1.x.

Carthage

See Carthage.md

Usage

A more detailed explanation of JSONPatch is given in Usage.md.

Applying Patches

import JSONPatch

let sourceData = Data("""
                      {"foo": "bar"}
                      """.utf8)
let patchData = Data("""
                     [{"op": "add", "path": "/baz", "value": "qux"}]
                     """.utf8)

let patch = try! JSONPatch(data: patchData)
let patched = try! patch.apply(to: sourceData)

Generating Patches

import JSONPatch

let sourceData = Data("""
                      {"foo": "bar"}
                      """.utf8)
let targetData = Data("""
                      {"foo": "bar", "baz": "qux"}
                      """.utf8)
let patch = try! JSONPatch(source: sourceData, target: targetData)
let patchData = try! patch.data()

Concurrency

JSONPointer, JSONPatch.ApplyOption, and JSONError can cross actor boundaries. Keep JSONPatch, its operations, and JSONElement within one actor or task because they can retain mutable Foundation objects. Transfer JSON Data between actors and create the patch inside the receiving actor. See the concurrency example.

Development

Swift Testing is supplied by the toolchain. Run both configurations with compiler warnings treated as errors:

swift test -Xswiftc -warnings-as-errors
swift test --configuration release -Xswiftc -warnings-as-errors

On macOS, validate the standalone project and framework builds:

bash Scripts/check-apple.sh tests
bash Scripts/check-apple.sh frameworks
pod lib lint RMJSONPatch.podspec --fail-fast
carthage build --no-skip-current --use-xcframeworks --platform iOS,macOS,tvOS,watchOS
python3 Scripts/check-xcframework.py Carthage/Build/JSONPatch.xcframework ios macos tvos watchos

The Xcode test check requires an installed iPhone simulator runtime. CocoaPods and Carthage must be installed for their respective checks. GitHub Actions runs Swift 6.0 and 6.2 tests on macOS and Linux, plus Apple distribution checks.

License

Apache License v2.0

About

JSON Patch RFC6902 implementation in Swift

Resources

Stars

25 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages