An open API service indexing awesome lists of open source software.

https://github.com/brody-0125/json_merge_patch

RFC 7396 JSON Merge Patch for Dart. apply and generate merge patches
https://github.com/brody-0125/json_merge_patch

dart json json-merge-patch patch rfc7396

Last synced: about 1 month ago
JSON representation

RFC 7396 JSON Merge Patch for Dart. apply and generate merge patches

Awesome Lists containing this project

README

          

# json_merge_patch

A Dart implementation of [RFC 7396 — JSON Merge Patch](https://datatracker.ietf.org/doc/html/rfc7396).

[![pub package](https://img.shields.io/pub/v/json_merge_patch.svg)](https://pub.dev/packages/json_merge_patch)

## Features

- **`mergePatch(target, patch)`** — Apply a merge patch to a JSON value.
- **`generate(source, target)`** — Create a merge patch that turns `source` into `target`.
- No external dependencies.
- Dart 3.0+.
- Passes all 16 test cases from RFC 7396 Appendix A.

## Install

```sh
dart pub add json_merge_patch
```

## Usage

```dart
import 'package:json_merge_patch/json_merge_patch.dart';

void main() {
// Apply a merge patch
final target = {'a': 'b', 'c': {'d': 'e', 'f': 'g'}};
final patch = {'a': 'z', 'c': {'f': null}};
final result = mergePatch(target, patch);
print(result); // {a: z, c: {d: e}}

// Generate a merge patch
final source = {'a': 'b', 'c': 'd'};
final goal = {'a': 'z', 'e': 'f'};
final generated = generate(source, goal);
print(generated); // {a: z, c: null, e: f}
}
```

## How it works

RFC 7396 defines a recursive algorithm for partial JSON updates:

- If the patch is an object, merge it recursively with the target.
- If a patch value is `null`, remove that key from the target.
- If the patch is not an object, replace the target entirely.

Arrays are always replaced wholesale (not merged element-by-element), and `null` cannot be set as a value — it always means "delete".

## Limitations

These come from the RFC 7396 spec itself:

- **`null` means delete** — you can't set a field's value to `null`.
- **No array merging** — arrays are replaced entirely.
- **No conditional updates** — there's no `test` operation like RFC 6902.

If you need those, see [rfc_6902](https://pub.dev/packages/rfc_6902).

## RFC compliance

Implements the MergePatch algorithm from [RFC 7396 Section 2](https://datatracker.ietf.org/doc/html/rfc7396#section-2). All 16 Appendix A test cases are included and pass.

## License

MIT. See [LICENSE](LICENSE).

---

[한국어 README](README_ko.md)