apply static method

dynamic apply(
  1. dynamic json,
  2. Iterable<Map<String, dynamic>> patches, {
  3. bool strict = true,
})

Applies JSON Patch operations to a JSON-like object.

Returns the result of json after applying each patch of patches in order. The original json object is left unchanged.

If test operations fail, a JsonPatchTestFailedException is thrown.

If strict is false, adding a value that already exists is equivalent to replacing it, replacing a value that does not exist is equivalent to adding it, and removing a child that does not exist from a parent is legal and does nothing. Copying or moving will replace the value at to if necessary.

Throws a JsonPatchError if something goes wrong.

See https://tools.ietf.org/html/rfc6902 for more information.

Implementation

static dynamic apply(dynamic json, Iterable<Map<String, dynamic>> patches,
    {bool strict = true}) {
  try {
    // Deep copy the input.
    json = _deepCopy(json);
    // Apply each patch in order.
    for (final patch in patches) {
      json = _applyOne(json, patch, strict);
    }
    return json;
  } catch (e) {
    if (e is JsonPatchError || e is JsonPatchTestFailedException) rethrow;
    throw JsonPatchError('An unknown error occurred.');
  }
}