json

package
v0.7.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 7 Imported by: 0

README

JSON Codec for JSON Patch Operations

The json codec converts object-shaped JSON Patch operations to executable operations and back.

Use this package when you need direct control over JSON operation decoding or encoding. Most callers should use jsonpatch.CompileJSON for raw JSON bytes or jsonpatch.CompileOperations for Operation values.

Wire Shape

JSON operations use JSON Pointer strings at the JSON boundary:

{"op": "add", "path": "/profile/name", "value": "Ada"}

Raw JSON/map decoding owns field-presence checks. Missing required fields are rejected, while present null, 0, and empty-string values remain real payload values.

Usage

package main

import (
    "fmt"
    "log"

    jsoncodec "github.com/kaptinlin/jsonpatch/codec/json"
)

func main() {
    raw := []map[string]any{
        {"op": "test", "path": "/name", "value": "Ada"},
        {"op": "replace", "path": "/name", "value": "Grace"},
    }

    ops, err := jsoncodec.Decode(raw, jsoncodec.PatchOptions{})
    if err != nil {
        log.Fatal(err)
    }

    encoded, err := jsoncodec.EncodeJSON(ops)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(len(encoded) > 0)
}

API

func Decode(operations []map[string]any, opts PatchOptions) ([]jsonpatch.Op, error)
func DecodeOperations(operations []Operation, opts PatchOptions) ([]jsonpatch.Op, error)
func DecodeJSON(data []byte, opts PatchOptions) ([]jsonpatch.Op, error)

func Encode(ops []jsonpatch.Op) ([]Operation, error)
func EncodeJSON(ops []jsonpatch.Op) ([]byte, error)

PatchOptions configures JSON decoding. Its main use is providing the matcher factory for matches predicates. Matcher factories return (RegexMatcher, error) so invalid or unsupported patterns can be rejected during compilation.

EncodeJSON emits the minimal object for each operation. Required null, empty-string, and zero-valued fields are preserved; unrelated fields from the shared Go DTO are not emitted.

Operation Families

The JSON codec decodes the same operation language used by the root package:

  • RFC 6902 operations: add, remove, replace, move, copy, test
  • Predicate operations: defined, undefined, contains, starts, ends, matches, type, test_type, test_string, test_string_len, in, less, more
  • Composite predicates: and, or, unary not
  • Extended operations: flip, inc, str_ins, str_del, split, merge, extend

Composite predicate child paths are decoded relative to the containing predicate path. Use path: "" on the containing predicate when children should be root-scoped.

Testing Contract

The codec has fixture coverage for field presence: missing required fields, present null, root path, zero numeric fields, and empty strings.

Documentation

Overview

Package json implements a codec for JSON Patch operations with full RFC 6902, JSON Predicate, and extended operation support.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOpMissingOpField   = errors.New("operation missing 'op' field")
	ErrOpMissingPathField = errors.New("operation missing 'path' field")
	ErrInvalidPointer     = errors.New("invalid pointer")
	ErrCodecOpUnknown     = errors.New("unknown operation")
	ErrUnsupportedOp      = errors.New("unsupported operation type")
)

Errors for base operation decoding.

View Source
var (
	ErrAddOpMissingValue     = errors.New("add operation missing 'value' field")
	ErrReplaceOpMissingValue = errors.New("replace operation missing 'value' field")
	ErrMissingValueField     = errors.New("missing value field")
	ErrMoveOpMissingFrom     = errors.New("move operation missing 'from' field")
	ErrCopyOpMissingFrom     = errors.New("copy operation missing 'from' field")
)

Errors for core operation (RFC 6902) decoding.

View Source
var (
	ErrIncOpMissingInc       = errors.New("inc operation missing 'inc' field")
	ErrIncOpInvalidType      = errors.New("inc operation 'inc' field must be a number")
	ErrStrInsOpMissingPos    = errors.New("str_ins operation missing 'pos' field")
	ErrStrInsOpMissingStr    = errors.New("str_ins operation missing 'str' field")
	ErrStrDelOpMissingPos    = errors.New("str_del operation missing 'pos' field")
	ErrStrDelOpMissingFields = errors.New("str_del operation missing 'str' or 'len' field")
	ErrSplitOpMissingPos     = errors.New("split operation missing 'pos' field")
	ErrMergeOpMissingPos     = errors.New("merge operation missing 'pos' field")
	ErrValueNotObject        = errors.New("value is not an object")
	ErrInvalidBooleanField   = errors.New("boolean field has invalid type")
)

Errors for extended operation decoding.

View Source
var (
	ErrTypeOpMissingValue        = errors.New("type operation missing string 'value' field")
	ErrTestTypeOpMissingType     = errors.New("test_type operation missing 'type' field")
	ErrTestStringOpMissingStr    = errors.New("test_string operation missing 'str' field")
	ErrTestStringLenOpMissingLen = errors.New("test_string_len operation missing 'len' field")
	ErrContainsOpMissingValue    = errors.New("contains operation missing 'value' field")
	ErrEndsOpMissingValue        = errors.New("ends operation missing 'value' field")
	ErrStartsOpMissingValue      = errors.New("starts operation missing 'value' field")
	ErrMatchesOpMissingValue     = errors.New("matches operation missing 'value' field")
	ErrInOpValueMustBeArray      = errors.New("in operation value must be an array")
	ErrLessOpMissingValue        = errors.New("less operation missing 'value' field")
	ErrMoreOpMissingValue        = errors.New("more operation missing 'value' field")
	ErrInvalidType               = errors.New("invalid type")
	ErrEmptyTypeList             = errors.New("empty type list")
	ErrTestStringOpMissingPos    = errors.New("test_string operation missing 'pos' field")
)

Errors for predicate operation decoding.

View Source
var (
	ErrAndOpMissingApply          = errors.New("and operation missing 'apply' field")
	ErrOrOpMissingApply           = errors.New("or operation missing 'apply' field")
	ErrNotOpMissingApply          = errors.New("not operation missing 'apply' field")
	ErrNotOpRequiresOperand       = errors.New("not operation requires at least one operand")
	ErrNotOpRequiresSingleOperand = errors.New("not operation requires exactly one operand")
	ErrNotOpRequiresValidOperand  = errors.New("not operation requires a valid predicate operand")
	ErrInvalidPredicateOperand    = errors.New("composite predicate requires predicate operands")
)

Errors for composite operation decoding.

Functions

func Decode

func Decode(operations []map[string]any, opts internal.JSONPatchOptions) ([]internal.Op, error)

Decode converts JSON operation maps to Op instances.

func DecodeJSON

func DecodeJSON(data []byte, opts internal.JSONPatchOptions) ([]internal.Op, error)

DecodeJSON converts JSON bytes to Op instances.

func DecodeOperations added in v0.5.0

func DecodeOperations(operations []internal.Operation, opts internal.JSONPatchOptions) ([]internal.Op, error)

DecodeOperations converts Operation structs to Op instances.

func Encode

func Encode(ops []internal.Op) ([]internal.Operation, error)

Encode converts Op instances to Operation structs.

func EncodeJSON

func EncodeJSON(ops []internal.Op) ([]byte, error)

EncodeJSON converts Op instances to JSON bytes.

Types

type Operation

type Operation = internal.Operation

Operation is a JSON Patch operation in JSON format.

type PatchOptions added in v0.4.3

type PatchOptions = internal.JSONPatchOptions

PatchOptions configures JSON Patch decoding.

Directories

Path Synopsis
Package tests provides test data and automated codec tests for the JSON codec.
Package tests provides test data and automated codec tests for the JSON codec.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL