cbor

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 9 Imported by: 0

README

Bento CBOR Processor

A Bento processor plugin for working with CBOR (Concise Binary Object Representation) data. This processor allows you to convert between JSON and CBOR formats.

Overview

CBOR is a binary data format designed for small message size with the ability to support the seamless conversion of JSON data models. This processor is RFC 7049 and RFC 8949 compliant and provides encoding and decoding capabilities for CBOR data.

Installation

Using Pre-built Binary

Download a pre-built binary from the releases page.

Usage

The CBOR processor supports two operations:

  1. to_json: Converts CBOR data into JSON format
  2. from_json: Converts JSON data into CBOR format
Configuration Examples
Convert CBOR to JSON
pipeline:
  processors:
    - cbor:
        operator: to_json
Convert JSON to CBOR
pipeline:
  processors:
    - cbor:
        operator: from_json
Full Example

This example demonstrates a complete roundtrip conversion - JSON to CBOR and back to JSON:

input:
  generate:
    count: 1
    interval: 1ms
    mapping: |
      root = {
        "message": "Hello CBOR World",
        "numbers": [1, 2, 3, 4, 5],
        "nested": {
          "boolean": true,
          "null_value": null
        },
        "m": { "c": 3, "a": 1, "b": 2,}
      }

pipeline:
  processors:
    - cbor:
        operator: from_json  # Convert JSON to CBOR
    - cbor:
        operator: to_json    # Convert CBOR back to JSON

output:
  stdout: {}

logger:
  level: info

Docker

A pre built binary is also availale as a docker image:

 docker pull ghcr.io/akhenakh/bento-cbor:latest

Build

Clone the repository and build the custom Bento binary:

git clone https://github.com/akhenakh/bento-cbor.git
cd bento-cbor
go build ./cmd/bento-cbor

You can build your own binary, just load the plugin:

package main

import (
	"context"

	"github.com/warpstreamlabs/bento/public/service"

	// Import all standard Benthos components
	_ "github.com/warpstreamlabs/bento/public/components/all"

	_ "github.com/akhenakh/bento-cbor"
)

func main() {
	service.RunCLI(context.Background())
}

Technical Details

The processor uses the fxamacker/cbor/v2 library for CBOR encoding and decoding, which provides:

  • RFC 7049 and RFC 8949 compliant implementation.
  • High performance encoding and decoding (updated to v2.9.2 for hardened indefinite-length data encoding).
  • RFC3339 UTC time encoding with nanosecond precision natively within CBOR (TimeRFC3339NanoUTC).
  • Support for various CBOR data types.
Performance Optimizations
  • Lazy JSON Evaluation: The to_json operator sets the decoded CBOR payload natively as Bento's internal structured message (msg.SetStructured()). This enables lazy evaluation, skipping expensive JSON byte allocation and serialization steps entirely if the next processor handles mapped structured queries or structural modifications.
  • Precision Safety: The from_json processor guarantees that numbers are not incorrectly interpreted as strings inside CBOR payloads by circumventing json.Number fallback behaviors.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CBORProcessor

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

CBORProcessor processes messages by decoding CBOR data, displaying the decoded content, and re-encoding it.

func NewProcessor

func NewProcessor(operatorStr string) (*CBORProcessor, error)

func (*CBORProcessor) Close

func (cp *CBORProcessor) Close(ctx context.Context) error

Close implements the service.Processor interface.

func (*CBORProcessor) Process

Process implements the service.Processor interface.

Directories

Path Synopsis
cmd
bento-cbor command
Example build to include bento-cbor with all bento components
Example build to include bento-cbor with all bento components

Jump to

Keyboard shortcuts

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