Skip to content

brody-0125/json_merge_patch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json_merge_patch

A Dart implementation of RFC 7396 — JSON Merge Patch.

pub package

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

dart pub add json_merge_patch

Usage

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.

RFC compliance

Implements the MergePatch algorithm from RFC 7396 Section 2. All 16 Appendix A test cases are included and pass.

License

MIT. See LICENSE.


한국어 README

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages