Documentation
¶
Overview ¶
Package jsonmerge provides RFC 7386 JSON Merge Patch implementation. It offers a simple, type-safe API for applying merge patches to JSON documents.
Basic usage:
result, err := jsonmerge.Merge(target, patch)
if err != nil {
return err
}
// result.Doc contains the merged document
The library supports multiple document types:
- Structs (with full type safety)
- map[string]any (dynamic documents)
- []byte (JSON bytes)
- string (JSON strings)
All operations are immutable by default. Use WithMutate(true) for performance-critical scenarios where in-place modification is acceptable.
types.go defines the core types and functional options for JSON Merge Patch operations.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate creates a JSON Merge Patch between source and target documents. The generated patch can be applied to source to produce target.
Possible errors (checkable with errors.Is):
- ErrMarshal: JSON marshaling failed during type conversion
- ErrUnmarshal: JSON unmarshaling failed during type conversion
- ErrConversion: type conversion between document types failed
Example ¶
package main
import (
"fmt"
"log"
"github.com/kaptinlin/jsonmerge"
)
func main() {
source := map[string]any{"name": "John", "age": 30, "city": "NYC"}
target := map[string]any{"name": "Jane", "age": 30}
patch, err := jsonmerge.Generate(source, target)
if err != nil {
log.Fatal(err)
}
fmt.Println(patch["name"])
fmt.Println(patch["city"])
}
Output: Jane <nil>
func Valid ¶
Valid checks if a patch is a valid JSON Merge Patch. According to RFC 7386, any valid JSON value is a valid merge patch.
Example ¶
package main
import (
"fmt"
"github.com/kaptinlin/jsonmerge"
)
func main() {
fmt.Println(jsonmerge.Valid(map[string]any{"name": "Jane"}))
fmt.Println(jsonmerge.Valid([]byte(`{"name": "Jane"}`)))
fmt.Println(jsonmerge.Valid([]byte(`{invalid}`)))
}
Output: true true false
Types ¶
type Document ¶
Document represents the supported document types for JSON Merge Patch operations. This constraint allows for type-safe operations across different JSON representations.
Supported types and their behavior:
- []byte: must contain valid JSON; returns ErrUnmarshal if invalid
- string: first attempts JSON parsing; if invalid JSON, treated as raw string value
- map[string]any: native format with zero conversion overhead (most efficient)
- struct types (via any): converted through JSON marshal/unmarshal cycle; respects json struct tags (json:"name,omitempty", json:"-")
- primitive types (bool, int*, uint*, float*): passed through directly
type Error ¶ added in v0.2.4
type Error string
Error represents a sentinel error type for the jsonmerge package.
const ( // ErrMarshal indicates JSON marshaling failed. ErrMarshal Error = "marshal failed" // ErrUnmarshal indicates JSON unmarshaling failed. ErrUnmarshal Error = "unmarshal failed" // ErrConversion indicates type conversion between document types failed. ErrConversion Error = "type conversion failed" )
Sentinel errors for error checking with errors.Is.
type Option ¶
type Option func(*Options)
Option is a functional option type for configuring merge operations.
func WithMutate ¶
WithMutate configures whether to modify the target document in place. By default, merge operations are immutable and create a new document. Setting mutate to true can improve performance but may affect thread safety.
Default: false
Example:
result, err := Merge(target, patch, WithMutate(true))
type Options ¶
type Options struct {
// Mutate controls whether to modify the target document in place for performance.
Mutate bool
}
Options contains configuration for merge operations.
type Result ¶
type Result[T Document] struct { // Doc is the merged document, preserving the same type as the input. Doc T }
Result wraps the merged document with type safety. The generic parameter T preserves the original document type through the merge operation.
func Merge ¶
Merge applies a JSON Merge Patch (RFC 7386) to a target document. It returns a new Result containing the merged document and metadata. The operation is immutable by default unless WithMutate(true) is specified.
Possible errors (checkable with errors.Is):
- ErrMarshal: JSON marshaling failed during type conversion
- ErrUnmarshal: JSON unmarshaling failed during type conversion
- ErrConversion: type conversion between document types failed
Example ¶
package main
import (
"fmt"
"log"
"github.com/kaptinlin/jsonmerge"
)
func main() {
target := map[string]any{"name": "John", "age": 30}
patch := map[string]any{"age": 31}
result, err := jsonmerge.Merge(target, patch)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Doc["name"])
fmt.Println(result.Doc["age"])
}
Output: John 31
Example (WithMutate) ¶
package main
import (
"fmt"
"log"
"github.com/kaptinlin/jsonmerge"
)
func main() {
target := map[string]any{"name": "John", "age": 30}
patch := map[string]any{"age": 31}
result, err := jsonmerge.Merge(target, patch, jsonmerge.WithMutate(true))
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Doc["age"])
}
Output: 31
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
json-bytes-merge
command
|
|
|
json-string-merge
command
|
|
|
map-merge
command
|
|
|
struct-merge
command
|