Documentation
¶
Overview ¶
Package bogo provides fast, compact binary serialization with JSON-compatible API.
Bogo is a high-performance binary serialization format designed for efficient encoding and decoding of complex data types. It offers significant performance improvements over JSON while maintaining API compatibility, making it a drop-in replacement for encoding/json in many use cases.
Key Features ¶
- JSON-Compatible API: Familiar Marshal/Unmarshal functions
- High Performance: Up to 99x faster deserialization than JSON
- Compact Binary Format: Efficient variable-length encoding
- Selective Field Decoding: Revolutionary optimization for large objects
- Zero vs Nil Distinction: Robust handling of zero and null values
- Streaming Support: Memory-efficient streaming encoding/decoding
Basic Usage ¶
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
user := User{ID: 123, Name: "John", Email: "john@example.com"}
// Marshal to binary format
data, err := bogo.Marshal(user)
if err != nil {
log.Fatal(err)
}
// Unmarshal from binary format
var decoded User
err = bogo.Unmarshal(data, &decoded)
if err != nil {
log.Fatal(err)
}
Streaming API ¶
// Encoding
var buf bytes.Buffer
encoder := bogo.NewEncoder(&buf)
err := encoder.Encode(data)
// Decoding
decoder := bogo.NewDecoder(&buf)
var result interface{}
err = decoder.Decode(&result)
Advanced Configuration ¶
// Configurable decoder with selective field optimization
decoder := bogo.NewConfigurableDecoder(
bogo.WithSelectiveFields([]string{"id", "name"}), // Only decode these fields
bogo.WithDecoderMaxDepth(50), // Limit nesting depth
bogo.WithDecoderStrictMode(true), // Enable strict validation
)
result, err := decoder.Decode(largeObjectData)
// Up to 334x faster than decoding the entire object!
Zero Values vs Nil Values ¶
Bogo maintains a clear distinction between zero values and nil values:
- Zero values (e.g., "", 0, false) are preserved with type information
- Nil values are encoded as TypeNull and decode back to nil
- Enables tri-state logic: true/false/unknown, value/zero/unset
For complete technical specifications, see: https://github.com/bubunyo/bogo/blob/main/spec.md
Example ¶
package main
import (
"fmt"
"log"
"github.com/bubunyo/bogo"
)
func main() {
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
// Create a user
user := User{
ID: 12345,
Name: "John Doe",
Email: "john@example.com",
Age: 30,
}
// Marshal to Bogo binary format
data, err := bogo.Marshal(user)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Encoded %d bytes\n", len(data))
// Unmarshal back to struct
var decoded User
err = bogo.Unmarshal(data, &decoded)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Name: %s, Age: %d\n", decoded.Name, decoded.Age)
}
Output: Encoded 68 bytes Name: John Doe, Age: 30
Index ¶
- Constants
- func Decode(data []byte) (any, error)
- func Encode(v any) ([]byte, error)
- func Marshal(v any) ([]byte, error)
- func SetDefaultDecoder(decoder *Decoder)
- func SetDefaultEncoder(encoder *Encoder)
- func Unmarshal(data []byte, v any) error
- type Decoder
- type DecoderOption
- func WithDecoderMaxDepth(depth int) DecoderOption
- func WithDecoderStrictMode(strict bool) DecoderOption
- func WithDecoderStructTag(tagName string) DecoderOption
- func WithMaxObjectSize(size int64) DecoderOption
- func WithSelectiveFields(fields []string) DecoderOption
- func WithUTF8Validation(validate bool) DecoderOption
- func WithUnknownTypes(allow bool) DecoderOption
- type DecoderStatsCollector
- type DecodingStats
- type Encoder
- type EncoderOption
- type EncodingStats
- type FieldInfo
- type Parser
- type StatsCollector
- type StreamDecoder
- type StreamEncoder
- type Type
- type TypeNumber
- type UnknownType
Examples ¶
Constants ¶
const ( TypeNull = iota TypeBoolTrue TypeBoolFalse TypeString TypeByte TypeInt TypeUint TypeFloat TypeBlob TypeTimestamp TypeArray TypeTypedArray TypeObject )
const ( // Version helps determine which encoders/decoders to use Version byte = 0x00 // version 0 )
Type constants
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode deserializes Bogo binary data back into a Go value.
This is the primary decoding function that converts Bogo binary format back to Go values with automatic type reconstruction. It supports all types that can be encoded with Encode().
Example:
decoded, err := bogo.Decode(binaryData)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Decoded value: %v\n", decoded)
Type mapping:
- TypeNull → nil
- TypeString → string
- TypeInt → int64
- TypeUint → uint64
- TypeFloat → float64
- TypeArray → []any
- TypeObject → map[string]any
- And more...
Returns the decoded value and any decoding error.
func Encode ¶
Encode serializes a value to the Bogo binary format.
This is the primary encoding function that converts Go values to compact binary representation. It handles all supported types including primitives, arrays, objects, and structs with automatic type detection.
Example:
data, err := bogo.Encode("hello world")
if err != nil {
log.Fatal(err)
}
Supported types:
- nil, bool, string, byte
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- []byte, time.Time
- []any, []string, []int, []float64, etc.
- map[string]any, structs
Returns the binary representation and any encoding error.
func Marshal ¶
Marshal encodes a value to Bogo binary format.
Marshal is compatible with json.Marshal and can be used as a drop-in replacement in most cases. It traverses the value v recursively and encodes it using the Bogo binary format.
Example:
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
p := Person{Name: "Alice", Age: 30}
data, err := bogo.Marshal(p)
if err != nil {
log.Fatal(err)
}
Struct fields are encoded based on json struct tags, following the same conventions as encoding/json (omitempty, field renaming, etc.).
Returns the Bogo binary representation of v and any encoding error.
func SetDefaultDecoder ¶
func SetDefaultDecoder(decoder *Decoder)
SetDefaultDecoder sets the default decoder used by Unmarshal
func SetDefaultEncoder ¶
func SetDefaultEncoder(encoder *Encoder)
SetDefaultEncoder sets the default encoder used by Marshal
func Unmarshal ¶
Unmarshal parses Bogo binary data and stores the result in the value pointed to by v.
Unmarshal is compatible with json.Unmarshal and can be used as a drop-in replacement. It decodes the Bogo binary data and assigns the result to the value pointed to by v.
Example:
var person Person
err := bogo.Unmarshal(data, &person)
if err != nil {
log.Fatal(err)
}
var result map[string]any
err = bogo.Unmarshal(data, &result)
if err != nil {
log.Fatal(err)
}
The destination v must be a pointer to a value where the decoded result will be stored. Unmarshal handles type conversions automatically (e.g., int to int64, float32 to float64).
Returns an error if the data cannot be decoded or assigned to v.
Types ¶
type Decoder ¶
type Decoder struct {
// Configuration options
MaxDepth int // Maximum nesting depth for objects/arrays (0 = unlimited)
StrictMode bool // Strict type checking and validation
AllowUnknownTypes bool // Allow unknown type IDs for forward compatibility
MaxObjectSize int64 // Maximum size for objects/arrays (0 = unlimited)
ValidateUTF8 bool // Validate UTF-8 encoding in strings
TagName string // Struct tag name to use (default: "json" for compatibility)
SelectiveFields []string // List of specific fields to decode (optimization)
// contains filtered or unexported fields
}
Decoder provides structured decoding with configurable options
func GetDefaultDecoder ¶
func GetDefaultDecoder() *Decoder
GetDefaultDecoder returns the current default decoder
func NewConfigurableDecoder ¶
func NewConfigurableDecoder(options ...DecoderOption) *Decoder
NewConfigurableDecoder creates a new Decoder with optional configuration
type DecoderOption ¶
type DecoderOption func(*Decoder)
DecoderOption is a function type for configuring a Decoder
func WithDecoderMaxDepth ¶
func WithDecoderMaxDepth(depth int) DecoderOption
Decoder option functions
func WithDecoderStrictMode ¶
func WithDecoderStrictMode(strict bool) DecoderOption
func WithDecoderStructTag ¶
func WithDecoderStructTag(tagName string) DecoderOption
func WithMaxObjectSize ¶
func WithMaxObjectSize(size int64) DecoderOption
func WithSelectiveFields ¶
func WithSelectiveFields(fields []string) DecoderOption
WithSelectiveFields enables field-specific decoding optimization. When set, the decoder will only decode the specified fields from objects, dramatically improving performance for large objects where only specific fields are needed. This provides up to 334x performance improvement and 113x reduction in memory allocations compared to full object decoding.
func WithUTF8Validation ¶
func WithUTF8Validation(validate bool) DecoderOption
func WithUnknownTypes ¶
func WithUnknownTypes(allow bool) DecoderOption
type DecoderStatsCollector ¶
type DecoderStatsCollector struct {
*Decoder
Stats DecodingStats
}
DecoderStatsCollector is a decoder that collects statistics
func NewDecoderStatsCollector ¶
func NewDecoderStatsCollector(options ...DecoderOption) *DecoderStatsCollector
NewDecoderStatsCollector creates a decoder that collects decoding statistics
func (*DecoderStatsCollector) Decode ¶
func (dsc *DecoderStatsCollector) Decode(data []byte) (any, error)
Decode wraps the parent Decode with statistics collection
func (*DecoderStatsCollector) GetStats ¶
func (dsc *DecoderStatsCollector) GetStats() DecodingStats
GetStats returns a copy of the current statistics
func (*DecoderStatsCollector) ResetStats ¶
func (dsc *DecoderStatsCollector) ResetStats()
ResetStats resets all statistics
type DecodingStats ¶
type DecodingStats struct {
BytesDecoded int64
MaxDepthUsed int
TypesDecoded map[Type]int
ErrorsCount int64
UnknownTypes int64
}
DecodingStats provides statistics about decoding operations
type Encoder ¶
type Encoder struct {
// Configuration options
MaxDepth int // Maximum nesting depth for objects/arrays (0 = unlimited)
StrictMode bool // Strict type checking and validation
CompactArrays bool // Use typed arrays when beneficial
ValidateStrings bool // Validate UTF-8 encoding in strings
TagName string // Struct tag name to use (default: "json" for compatibility)
// contains filtered or unexported fields
}
Encoder provides structured encoding with configurable options
Example ¶
package main
import (
"fmt"
"log"
"github.com/bubunyo/bogo"
)
func main() {
// Advanced configuration for high-performance scenarios
decoder := bogo.NewConfigurableDecoder(
bogo.WithSelectiveFields([]string{"id", "name"}), // Only decode these fields
bogo.WithDecoderMaxDepth(10), // Limit nesting
)
// Create some test data
testData := map[string]any{
"id": int64(123),
"name": "Alice",
"email": "alice@example.com", // This will be skipped
"profile": map[string]any{ // This will be skipped
"bio": "Long biography...",
"settings": map[string]any{"theme": "dark"},
},
}
// Encode normally
encoded, err := bogo.Marshal(testData)
if err != nil {
log.Fatal(err)
}
// Decode selectively (much faster for large objects)
result, err := decoder.Decode(encoded)
if err != nil {
log.Fatal(err)
}
resultMap := result.(map[string]any)
fmt.Printf("Selective decode - ID: %v, Name: %v\n", resultMap["id"], resultMap["name"])
}
Output: Selective decode - ID: 123, Name: Alice
func GetDefaultEncoder ¶
func GetDefaultEncoder() *Encoder
GetDefaultEncoder returns the current default encoder
func NewConfigurableEncoder ¶
func NewConfigurableEncoder(options ...EncoderOption) *Encoder
NewConfigurableEncoder creates a new Encoder with optional configuration
type EncoderOption ¶
type EncoderOption func(*Encoder)
EncoderOption is a function type for configuring an Encoder
func WithCompactArrays ¶
func WithCompactArrays(compact bool) EncoderOption
func WithStrictMode ¶
func WithStrictMode(strict bool) EncoderOption
func WithStringValidation ¶
func WithStringValidation(validate bool) EncoderOption
func WithStructTag ¶
func WithStructTag(tagName string) EncoderOption
type EncodingStats ¶
type EncodingStats struct {
BytesEncoded int64
MaxDepthUsed int
TypesEncoded map[Type]int
ErrorsCount int64
}
EncodingStats provides statistics about encoding operations
type Parser ¶
type Parser struct {
TagName string // Configurable tag name (e.g., "json", "bogo")
}
Parser extracts struct field information for encoding/decoding
func (*Parser) ParseFields ¶
ParseFields returns a map of field names to their corresponding field info
type StatsCollector ¶
type StatsCollector struct {
*Encoder
Stats EncodingStats
}
StatsCollector is an encoder that collects statistics
func NewStatsCollector ¶
func NewStatsCollector(options ...EncoderOption) *StatsCollector
NewStatsCollector creates an encoder that collects encoding statistics
func (*StatsCollector) Encode ¶
func (sc *StatsCollector) Encode(v any) ([]byte, error)
Encode wraps the parent Encode with statistics collection
func (*StatsCollector) GetStats ¶
func (sc *StatsCollector) GetStats() EncodingStats
GetStats returns a copy of the current statistics
func (*StatsCollector) ResetStats ¶
func (sc *StatsCollector) ResetStats()
ResetStats resets all statistics
type StreamDecoder ¶
type StreamDecoder struct {
// contains filtered or unexported fields
}
StreamDecoder reads and decodes bogo values from an input stream, similar to json.Decoder
func NewDecoder ¶
func NewDecoder(r io.Reader) *StreamDecoder
NewDecoder creates a new StreamDecoder that reads from r, similar to json.NewDecoder
func NewDecoderWithOptions ¶
func NewDecoderWithOptions(r io.Reader, options ...DecoderOption) *StreamDecoder
NewDecoderWithOptions creates a StreamDecoder with custom configuration options
func (*StreamDecoder) Decode ¶
func (dec *StreamDecoder) Decode(v any) error
Decode reads the next bogo value from the stream and stores it in v, similar to json.Decoder.Decode
func (*StreamDecoder) SetDecoder ¶
func (dec *StreamDecoder) SetDecoder(d *Decoder)
SetDecoder allows setting a custom decoder instance
type StreamEncoder ¶
type StreamEncoder struct {
// contains filtered or unexported fields
}
StreamEncoder writes bogo values to an output stream, similar to json.Encoder
func NewEncoder ¶
func NewEncoder(w io.Writer) *StreamEncoder
NewEncoder creates a new StreamEncoder that writes to w, similar to json.NewEncoder
func NewEncoderWithOptions ¶
func NewEncoderWithOptions(w io.Writer, options ...EncoderOption) *StreamEncoder
NewEncoderWithOptions creates a StreamEncoder with custom configuration options
func (*StreamEncoder) Encode ¶
func (enc *StreamEncoder) Encode(v any) error
Encode encodes v and writes it to the stream, similar to json.Encoder.Encode
func (*StreamEncoder) SetEncoder ¶
func (enc *StreamEncoder) SetEncoder(e *Encoder)
SetEncoder allows setting a custom encoder instance
type TypeNumber ¶
type UnknownType ¶
UnknownType represents a type that couldn't be decoded but was allowed
func (UnknownType) String ¶
func (ut UnknownType) String() string