encoding

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 1 Imported by: 1

README

encoding

encoding is a small codec registry. Codec packages register themselves from init, so consumers normally blank import the formats they want and then look up a codec by content subtype.

Usage

import (
	"github.com/go-jimu/components/encoding"
	_ "github.com/go-jimu/components/encoding/json"
	_ "github.com/go-jimu/components/encoding/proto"
	_ "github.com/go-jimu/components/encoding/toml"
	_ "github.com/go-jimu/components/encoding/yaml"
)

codec := encoding.GetCodec("json")
data, err := codec.Marshal(value)

Built-in codec packages:

  • encoding/json
  • encoding/yaml
  • encoding/toml
  • encoding/proto

Documentation

Overview

Package encoding provides a registry for named codecs.

Codec packages such as encoding/json, encoding/yaml, encoding/toml, and encoding/proto register themselves from init. Consumers blank import the codec packages they need, then call GetCodec by content subtype.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterCodec

func RegisterCodec(codec Codec)

RegisterCodec registers the provided Codec for use with all Transport clients and servers.

Types

type Codec

type Codec interface {
	// Marshal returns the wire format of v.
	Marshal(v interface{}) ([]byte, error)
	// Unmarshal parses the wire format into v.
	Unmarshal(data []byte, v interface{}) error
	// Name returns the name of the Codec implementation. The returned string
	// will be used as part of content type in transmission.  The result must be
	// static; the result cannot change between calls.
	Name() string
}

Codec defines the interface Transport uses to encode and decode messages. Note that implementations of this interface must be thread safe; a Codec's methods can be called from concurrent goroutines.

func GetCodec

func GetCodec(contentSubtype string) Codec

GetCodec gets a registered Codec by content-subtype, or nil if no Codec is registered for the content-subtype.

The content-subtype is expected to be lowercase.

Example
package main

import (
	"fmt"

	"github.com/go-jimu/components/encoding"
	_ "github.com/go-jimu/components/encoding/json"
)

type examplePayload struct {
	Name string `json:"name"`
}

func main() {
	codec := encoding.GetCodec("json")
	if codec == nil {
		panic("json codec is not registered")
	}

	data, err := codec.Marshal(examplePayload{Name: "agent"})
	if err != nil {
		panic(err)
	}

	fmt.Println(codec.Name(), string(data))

}
Output:
json {"name":"agent"}

Directories

Path Synopsis
Package json registers the JSON codec with the encoding registry.
Package json registers the JSON codec with the encoding registry.
Package proto defines the protobuf codec.
Package proto defines the protobuf codec.
Package toml registers the TOML codec with the encoding registry.
Package toml registers the TOML codec with the encoding registry.
Package yaml registers the YAML codec with the encoding registry.
Package yaml registers the YAML codec with the encoding registry.

Jump to

Keyboard shortcuts

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