$ show mailru/easyjson
easyjson is a Go library and code generator that produces specialized, reflection-free JSON marshal/unmarshal code for Go structs. It achieves 4–5× faster serialization than the standard `encoding/json` by generating type-specific encoder/decoder functions at compile time. The architecture consists of a CLI tool (`easyjson/main.go`) that parses Go source files, extracts struct annotations (`//easyjson:json`), and generates `_easyjson.go` files implementing `easyjson.Marshaler`/`Unmarshaler` interfaces. The runtime library provides a fast JSON lexer (`jlexer`), writer (`jwriter`), buffer pool (`buffer`), optional types (`opt`), and helpers for unknown fields, string interning, and zero-copy parsing. The project includes extensive benchmarks comparing against ffjson, jsoniter, and go/codec, and a comprehensive test suite covering edge cases like omitempty, snake_case, embedded types, and custom key types.
The LLM identified 4 files worth highlighting for reuse:
Implements a configurable, thread-safe buffer pool using `sync.Pool` with chunk sizes from 128B to 32KB. Provides `Get()` and `Put()` methods for reusing byte slices, reducing allocation overhead in high-throughput JSON writing. The pool is self-contained and can be extracted for any Go project needing efficient byte buffer reuse.
A fast JSON tokenizer that parses JSON input without reflection. Supports non-copy string extraction via `unsafe` pointer casting (with a safe fallback), error collection, and multiple number formats. It is the core parsing engine for generated unmarshalers and can be reused independently for custom JSON processing pipelines.
A high-performance JSON writer that integrates with the buffer pool for efficient output. Provides methods for writing strings (with proper escaping), numbers, booleans, arrays, and objects. Supports optional human-readable formatting and is designed for generated marshalers. The writer is standalone and can be used in any Go project requiring fast JSON construction.
Defines a generic optional type (`Optional[T]`) that wraps a value and a `Defined` flag, implementing `easyjson.Marshaler`/`Unmarshaler`. This allows nullable fields without pointers, with automatic JSON omission when undefined. The pattern is reusable for any Go project needing explicit presence tracking in serialization.