Skip to content

Extensions Overview ​

Extension points come in two kinds: hooks insert custom logic into the operation flow, and custom encoding decides how a type shapes its JSON output. They work independently or together — for example, audit writes with a hook while customizing output format with MarshalJSON.

Hook System ​

Hooks intercept the full Get/Set/Delete flow (package-level wrappers included; Encode/Marshal paths do not trigger them), firing before and after each operation — the place for logging, timing, input validation, and audit:

go
p, err := json.New()
if err != nil {
    panic(err)
}
defer p.Close()

// Runtime registration: attach a logging hook
p.AddHook(json.LoggingHook(slog.Default()))

Register hooks at assembly time via Config.Hooks (loaded at construction), or at runtime via Processor.AddHook. Beyond the built-in LoggingHook/TimingHook/ValidationHook/ErrorHook factories, implement the Hook interface for full control.

For trigger points, ordering, and panic protection, see the Hook System page.

Custom Encoding ​

Three channels actually take effect in the current version for changing "what JSON a type encodes to":

ChannelUse case
Implement json.Marshaler (MarshalJSON)A type fully controls its own JSON shape
Implement encoding.TextMarshaler (MarshalText)Textual representation, encoded as a JSON string
Config.CustomEscapesGlobally rewrite the escaping of certain characters

Config.CustomEncoder is deprecated

Config.CustomEncoder and Config.CustomTypeEncoders are deprecated: declared but never wired into the encoding pipeline, setting them does not change output today (kept within v1). Use the three channels above instead.

For complete examples and selection guidance, see Custom Encoder.

See Also ​