Class JsonPatch
JsonNode.
Genkit uses JSON Patch to stream incremental changes to a session's custom state (
AgentStreamChunk.customPatch). The diff(com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.JsonNode) helper only emits add / remove
/ replace operations (a valid RFC 6902 subset; move / copy are
optimisations we deliberately skip from diff output), while apply(com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.JsonNode) understands the full
operation set for interoperability.
Ported from js/ai/src/json-patch.ts in the Genkit upstream repository.
-
Method Summary
Modifier and TypeMethodDescriptionstatic com.fasterxml.jackson.databind.JsonNodeapply(com.fasterxml.jackson.databind.JsonNode document, com.fasterxml.jackson.databind.JsonNode patch) Applies an RFC-6902 patch (a JSON array of ops) todocument, returning the new document.static com.fasterxml.jackson.databind.JsonNodediff(com.fasterxml.jackson.databind.JsonNode from, com.fasterxml.jackson.databind.JsonNode to) Computes a minimal RFC-6902 patch that transformsfromintoto.static com.fasterxml.jackson.databind.JsonNodewholeDocumentReplace(com.fasterxml.jackson.databind.JsonNode value) Returns a whole-document replace patch:[{"op":"replace","path":"","value":value}].
-
Method Details
-
apply
public static com.fasterxml.jackson.databind.JsonNode apply(com.fasterxml.jackson.databind.JsonNode document, com.fasterxml.jackson.databind.JsonNode patch) Applies an RFC-6902 patch (a JSON array of ops) todocument, returning the new document.The input document is not mutated; a deep copy is patched and returned.
- Parameters:
document- the document to patch (may benull/NullNode)patch- anArrayNodeof RFC-6902 operation objects- Returns:
- the patched document
- Throws:
IllegalArgumentException- if atestop fails or the patch is malformed
-
diff
public static com.fasterxml.jackson.databind.JsonNode diff(com.fasterxml.jackson.databind.JsonNode from, com.fasterxml.jackson.databind.JsonNode to) Computes a minimal RFC-6902 patch that transformsfromintoto.Only
add,remove, andreplaceops are emitted. Arrays that differ are replaced as a single atomic unit (onereplaceop for the whole array), matching the JS reference implementation injs/ai/src/json-patch.ts.- Parameters:
from- the source documentto- the target document- Returns:
- an
ArrayNodeof RFC-6902 operation objects
-
wholeDocumentReplace
public static com.fasterxml.jackson.databind.JsonNode wholeDocumentReplace(com.fasterxml.jackson.databind.JsonNode value) Returns a whole-document replace patch:[{"op":"replace","path":"","value":value}].- Parameters:
value- the value to place at the document root- Returns:
- a single-element
ArrayNode
-