jsonpatch

package module
v0.0.0-...-eec5423 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: BSD-3-Clause Imports: 5 Imported by: 6

README

jsonpatch

Test

As per http://jsonpatch.com/ JSON Patch is specified in RFC 6902 from the IETF.

an attempt to join the efforts made by mattbaird and evanphx

using jsondiff on the tests

Create Patch

package main

import (
	"fmt"
	"github.com/benitogf/jsonpatch"
)

var simpleA = `{"a":100, "b":200, "c":"hello"}`
var simpleB = `{"a":100, "b":200, "c":"goodbye"}`

func main() {
	patch, e := jsonpatch.CreatePatch([]byte(simpleA), []byte(simpleA))
	if e != nil {
		fmt.Printf("Error creating JSON patch:%v", e)
		return
	}
	for _, operation := range patch {
		fmt.Printf("%s\n", operation.Json())
	}
}

Apply Patch

package main

import (
	"fmt"
	"github.com/benitogf/jsonpatch"
)

func main() {
	original := []byte(`{"name": "John", "age": 24, "height": 3.21}`)
	patchJSON := []byte(`[
		{"op": "replace", "path": "/name", "value": "Jane"},
		{"op": "remove", "path": "/height"}
	]`)

	patch, err := jsonpatch.DecodePatch(patchJSON)
	if err != nil {
		panic(err)
	}

	modified, err := patch.Apply(original)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Original document: %s\n", original)
	fmt.Printf("Modified document: %s\n", modified)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SupportNegativeIndices decides whether to support non-standard practice of
	// allowing negative indices to mean indices starting at the end of an array.
	// Default to true.
	SupportNegativeIndices = true
	// AccumulatedCopySizeLimit limits the total size increase in bytes caused by
	// "copy" operations in a patch.
	AccumulatedCopySizeLimit = int64(0)
)

Functions

func Equal

func Equal(a, b []byte) bool

Equal indicates if 2 JSON documents have the same structural equality.

Types

type AccumulatedCopySizeError

type AccumulatedCopySizeError struct {
	// contains filtered or unexported fields
}

AccumulatedCopySizeError is an error type returned when the accumulated size increase caused by copy operations in a patch operation has exceeded the limit.

func NewAccumulatedCopySizeError

func NewAccumulatedCopySizeError(l, a int64) *AccumulatedCopySizeError

NewAccumulatedCopySizeError returns an AccumulatedCopySizeError.

func (*AccumulatedCopySizeError) Error

func (a *AccumulatedCopySizeError) Error() string

Error implements the error interface.

type ArraySizeError

type ArraySizeError struct {
	// contains filtered or unexported fields
}

ArraySizeError is an error type returned when the array size has exceeded the limit.

func NewArraySizeError

func NewArraySizeError(l, s int) *ArraySizeError

NewArraySizeError returns an ArraySizeError.

func (*ArraySizeError) Error

func (a *ArraySizeError) Error() string

Error implements the error interface.

type ByPath

type ByPath []Operation

ByPath array of patch operation structs

func (ByPath) Len

func (a ByPath) Len() int

func (ByPath) Less

func (a ByPath) Less(i, j int) bool

func (ByPath) Swap

func (a ByPath) Swap(i, j int)

type Operation

type Operation struct {
	Operation string      `json:"op"`
	Path      string      `json:"path"`
	Value     interface{} `json:"value,omitempty"`
}

Operation operation struct

func CreatePatch

func CreatePatch(a, b []byte) ([]Operation, error)

CreatePatch creates a patch as specified in http://jsonpatch.com/

'a' is original, 'b' is the modified document. Both are to be given as json encoded content. The function will return an array of Operations

An error will be returned if any of the two documents are invalid.

func NewPatch

func NewPatch(operation, path string, value interface{}) Operation

NewPatch creates a patch operation struct

func (*Operation) JSON

func (j *Operation) JSON() string

JSON returns a patch operation Json representation

func (*Operation) MarshalJSON

func (j *Operation) MarshalJSON() ([]byte, error)

MarshalJSON for patch operations

type Patch

type Patch []operation

Patch is an ordered collection of operations.

func DecodePatch

func DecodePatch(buf []byte) (Patch, error)

DecodePatch decodes the passed JSON document as an RFC 6902 patch.

func (Patch) Apply

func (p Patch) Apply(doc []byte) ([]byte, error)

Apply mutates a JSON document according to the patch, and returns the new document.

func (Patch) ApplyIndent

func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error)

ApplyIndent mutates a JSON document according to the patch, and returns the new document indented.

Jump to

Keyboard shortcuts

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