codec

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

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 6 Imported by: 1

README

codec-json

Go Reference GoReportCard codecov

codec-json provides a JSON-based codec for cmd-stream.

It maps concrete Command and Result types to internal identifiers, allowing type-safe serialization across network boundaries.

How To

import (
  "reflect"
  cdcjson "github.com/cmd-stream/codec-json-go"
)

var (
  // Note: The order of types matters — two codecs created with the same types
  // in a different order are not considered equal.
  cmdTypes = []reflect.Type{
    reflect.TypeFor[YourCmd](),
    // ...
  }
  resultTypes = []reflect.Type{
    reflect.TypeFor[YourResult](),
    // ...
  }
  serverCodec = cdcjson.NewServerCodec(cmdTypes, resultTypes)
  clientCodec = cdcjson.NewClientCodec(cmdTypes, resultTypes)
)

Example

See the calc_json example for a full demonstration of codec-json.

Fuzz Testing

To run fuzz tests:

./fuzz.sh 1m

Documentation

Overview

Package codec provides a JSON-based codec implementation for cmd-stream.

Index

Constants

View Source
const ErrorPrefix = "codecjson: "

Variables

This section is empty.

Functions

func WithCmd

func WithCmd[T any, C core.Cmd[T]]() func(*Registry[T])

WithCmd returns an option that registers a command type C in the Registry.

The order in which WithCmd options are passed to NewRegistry matters.

func WithResult

func WithResult[T any, R core.Result]() func(*Registry[T])

WithResult returns an option that registers a result type R in the Registry.

The order in which WithResult options are passed to NewRegistry matters.

Types

type ClientCodec

type ClientCodec[T any] = codec[core.Cmd[T], core.Result]

ClientCodec defines a JSON codec for the client side.

func NewClientCodec

func NewClientCodec[T any](cmdTypes []reflect.Type, resultTypes []reflect.Type,
	opts ...SetOption,
) (c ClientCodec[T])

NewClientCodec creates a JSON codec for the client side.

The cmdTypes slice lists Command types the client can send. The resultTypes slice lists Result types the client expects to receive.

Note: The order of types matters — two codecs created with the same types in a different order are not considered equal.

func NewClientCodecWith

func NewClientCodecWith[T any](registry *Registry[T], opts ...SetOption) ClientCodec[T]

NewClientCodecWith creates a JSON codec for the client side using the provided Registry.

type Registry

type Registry[T any] = cdc.Registry[T]

Registry is a collection of Command and Result types.

The order of registration matters — two registries created with the same types in a different order will produce codecs that are not compatible.

func NewRegistry

func NewRegistry[T any](opts ...func(*Registry[T])) *Registry[T]

NewRegistry creates a new Registry with the provided options.

type Serializer

type Serializer[T, V any] struct{}

Serializer implements the gnrc.Serializer interface using JSON.

func (Serializer[T, V]) Marshal

func (s Serializer[T, V]) Marshal(t T) (bs []byte, err error)

Marshal encodes the given value of type T into a JSON byte slice.

func (Serializer[T, V]) Unmarshal

func (s Serializer[T, V]) Unmarshal(bs []byte, v V) (err error)

Unmarshal decodes the JSON byte slice into the given value of type V.

type ServerCodec

type ServerCodec[T any] = codec[core.Result, core.Cmd[T]]

ServerCodec defines a JSON codec for the server side.

func NewServerCodec

func NewServerCodec[T any](cmdTypes []reflect.Type, resultTypes []reflect.Type,
	opts ...SetOption,
) (c ServerCodec[T])

NewServerCodec creates a JSON codec for the server side.

The cmdTypes slice lists Command types the server can handle. The resultTypes slice lists Result types that can be returned to the client.

Note: The order of types matters — two codecs created with the same types in a different order are not considered equal.

func NewServerCodecWith

func NewServerCodecWith[T any](registry *Registry[T], opts ...SetOption) ServerCodec[T]

NewServerCodecWith creates a JSON codec for the server side using the provided Registry.

type SetOption

type SetOption = cdc.SetOption

SetOption is a function that sets an option for the codec.

func WithMaxLen

func WithMaxLen(maxLen int) SetOption

WithMaxLen returns a SetOption that sets the maximum length of the encoded message byte slice.

Jump to

Keyboard shortcuts

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