Class JsonPatch

java.lang.Object
com.google.genkit.core.jsonpatch.JsonPatch

public final class JsonPatch extends Object
A tiny, dependency-free RFC 6902 (JSON Patch) implementation operating on Jackson 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 Type
    Method
    Description
    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) to document, returning the new document.
    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 transforms from into to.
    static com.fasterxml.jackson.databind.JsonNode
    wholeDocumentReplace(com.fasterxml.jackson.databind.JsonNode value)
    Returns a whole-document replace patch: [{"op":"replace","path":"","value":value}].

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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) to document, returning the new document.

      The input document is not mutated; a deep copy is patched and returned.

      Parameters:
      document - the document to patch (may be null / NullNode)
      patch - an ArrayNode of RFC-6902 operation objects
      Returns:
      the patched document
      Throws:
      IllegalArgumentException - if a test op 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 transforms from into to.

      Only add, remove, and replace ops are emitted. Arrays that differ are replaced as a single atomic unit (one replace op for the whole array), matching the JS reference implementation in js/ai/src/json-patch.ts.

      Parameters:
      from - the source document
      to - the target document
      Returns:
      an ArrayNode of 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