5085

RFC 7386, Faithfully

Algorithm
Hard
Pro
json
merge-patch
recursion
rfc7386

RFC 7386, Faithfully

Your extension keeps a JSON document per record — a product configuration synced with an e-commerce platform — and the platform sends changes as application/merge-patch+json bodies, the format defined by RFC 7386. The previous implementation "applied" a patch by copying every property it could read, defaults for the rest; the first patch that tried to remove a property put the default back instead, and nobody noticed for a month. You are writing the real thing: a merge-patch engine that passes the RFC's own test-case appendix, row by row.

Requirements

Create a codeunit named "Json Merge Patch" with exactly this public procedure:

procedure ApplyMergePatch(Target: JsonToken; Patch: JsonToken): JsonToken

Both parameters can hold any JSON value — an object, an array, a string, a number, a boolean or null — and so can the returned token. The semantics are RFC 7386, nothing more and nothing less:

  1. If the patch is not a JSON object (an array, a string, a number, a boolean or null), the result is the patch itself: it replaces the whole target, no matter what the target was.
  2. If the patch is a JSON object, the result is an object built from the target: when the target is not an object, its contents are discarded and you start from an empty object.
  3. Each member of the patch object is then applied by name. A member whose value is null deletes the member with that name from the result — and if no such member exists, the null does nothing at all; a null is never stored.
The rest of the statement and 3 hints Unlock with Pro
ALBusiness Central 28.5
Pro
Pro problem

Solve it with Pro

You’re reading the opening of the statement. Pro unlocks the whole problem:

  • The full statement
  • Starter code in the browser editor
  • 18 grading tests, run on Business Central 28.5
  • 3 hints
  • The reference solution

Pro also opens all 92 Pro problems and lifts the daily limits on compile checks and submissions.

$10 / month
or $100 / year
Upgrade
This problem is part of Pro
Pro unlocks the full statement, tests, hints and solution. The code editor works on a computer.
Upgrade