src/jsonpatch

This module implements JSON Patch according to RFC 6902.

Types

JsonPatchError = object of CatchableError
OperationKind {.pure.} = enum
  Add = "add", Remove = "remove", Replace = "replace", Move = "move",
  Copy = "copy", Test = "test"
Operation = ref object of RootObj
  kind*: OperationKind
  path*: JsonPointer
AddOperation = ref object of Operation
  value*: JsonNode
RemoveOperation = ref object of Operation
ReplaceOperation = ref object of Operation
  value*: JsonNode
MoveOperation = ref object of Operation
  fromPath*: JsonPointer
TestOperation = ref object of Operation
  value*: JsonNode
CopyOperation = ref object of Operation
  fromPath*: JsonPointer
JsonPatch = object
  operations: seq[Operation]

Procs

func patch(doc: JsonNode; op: Operation): JsonNode {....raises: [KeyError],
    tags: [].}
Apply patch operation to source document doc.

Example:

import json
let src = %* {"foo": "bar"}
let operation = newReplaceOperation("/foo".toJsonPointer, %* "baz")
let dst = %* {"foo": "baz"}
assert dst == src.patch(operation)
proc newAddOperation(path: JsonPointer; value: JsonNode): AddOperation {.
    ...raises: [], tags: [].}
proc newRemoveOperation(path: JsonPointer): RemoveOperation {.
    ...raises: [JsonPatchError], tags: [].}
proc newReplaceOperation(path: JsonPointer; value: JsonNode): ReplaceOperation {.
    ...raises: [], tags: [].}
proc newMoveOperation(path: JsonPointer; fromPath: JsonPointer): MoveOperation {.
    ...raises: [], tags: [].}
proc newTestOperation(path: JsonPointer; value: JsonNode): TestOperation {.
    ...raises: [], tags: [].}
proc newCopyOperation(path: JsonPointer; fromPath: JsonPointer): CopyOperation {.
    ...raises: [], tags: [].}
proc initJsonPatch(operations: seq[Operation]): JsonPatch {....raises: [], tags: [].}
proc initJsonPatch(operations: varargs[Operation]): JsonPatch {....raises: [],
    tags: [].}
func len(p: JsonPatch): Natural {....raises: [], tags: [].}
func patch(doc: JsonNode; operations: seq[Operation]): JsonNode {.
    ...raises: [KeyError], tags: [].}

Applies sequence of operations to doc.

The operations are applied in the order they appear in the sequence.

func patch(doc: JsonNode; patch: JsonPatch): JsonNode {....raises: [KeyError],
    tags: [].}
Applies patch to doc and returns the resulting JSON document.

Example:

import json
let src = %* {"foo": "bar"}
let dst = %* {"foo": "baz"}
let patch = src.diff(dst)
assert dst == src.patch(patch)
func diff(src, dst: JsonNode): JsonPatch {....raises: [JsonPointerError,
    ValueError, JsonPatchError, KeyError, Exception], tags: [RootEffect].}
Diffs the JSON document src with dst and returns the resulting JSON patch.

Example:

import json
let src = %* {"foo": "bar"}
let dst = %* {"foo": "baz"}
let patch = src.diff(dst)
assert dst == src.patch(patch)
proc to[T: Operation](node: JsonNode; t: typedesc[T]): T
proc to[T: JsonPatch](node: JsonNode; t: typedesc[T]): T
proc `%`(op: Operation): JsonNode {....raises: [], tags: [].}
proc `%`(patch: JsonPatch): JsonNode {....raises: [], tags: [].}