binary

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 5 Imported by: 0

README

Binary Codec for JSON Patch+

The binary codec serializes JSON Patch+ operations directly to MessagePack using the same array shape as the compact codec. It skips any intermediate JSON/compact conversion, resulting in smaller payloads and higher performance.

Features

  • Direct MessagePack output – no temporary compact representation
  • Full support for first-order JSON Patch, extended and predicate operations (composite predicates not yet implemented)
  • Familiar API: Encode / Decode, aligning with the other codecs

Quick Example

import (
    "github.com/kaptinlin/jsonpatch/codec/binary"
    "github.com/kaptinlin/jsonpatch/op"
    "github.com/kaptinlin/jsonpatch/internal"
)

func main() {
    // Build operations
    ops := []internal.Op{
        op.NewOpAddOperation([]string{"/", "foo"}, "bar"),
    }

    codec := binary.Codec{}

    // Encode to MessagePack
    data, err := codec.Encode(ops)
    if err != nil {
        panic(err)
    }

    // Decode back to operations
    decoded, err := codec.Decode(data)
    if err != nil {
        panic(err)
    }

    _ = decoded // use decoded operations
}

TIP: Fully round-trip compatible with the json and compact codecs – mix & match freely.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedOp indicates an unknown or unsupported operation code.
	ErrUnsupportedOp = errors.New("binary: unsupported operation code")
	// ErrInvalidTestTypeFormat indicates invalid types array for test_type predicate.
	ErrInvalidTestTypeFormat = errors.New("binary: invalid test_type types format")
)

Functions

This section is empty.

Types

type Codec

type Codec struct{}

Codec implements the Codec interface for the binary format.

func NewCodec

func NewCodec() *Codec

NewCodec creates a new Codec.

func (*Codec) Decode

func (c *Codec) Decode(data []byte) ([]internal.Op, error)

Decode decodes a slice of operations from binary format.

func (*Codec) Encode

func (c *Codec) Encode(ops []internal.Op) ([]byte, error)

Encode encodes a slice of operations into binary format.

Jump to

Keyboard shortcuts

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