Output Methods
The Processor provides a variety of JSON encoding output methods.
Basic Output
Encode
Signature: func (p *Processor) Encode(value any, cfg ...Config) (string, error)
Encodes any value into a JSON string. This is the canonical string-returning encoder (Marshal is the []byte-returning encoding/json drop-in); the optional trailing Config selects the encoding behavior for this call, and the processor's default configuration applies when omitted.
result, err := p.Encode(map[string]any{"name": "CyberGo"})
if err != nil {
panic(err)
}
fmt.Println(result)EncodePretty
Signature: func (p *Processor) EncodePretty(value any, cfg ...Config) (string, error)
Encodes any value into a pretty-printed JSON string.
result, err := p.EncodePretty(user)
if err != nil {
panic(err)
}Advanced Encoding
EncodeWithConfig
DeprecatedSignature: func (p *Processor) EncodeWithConfig(value any, cfg ...Config) (string, error)
Encodes a value into a JSON string with the given configuration.
Deprecated
Processor.EncodeWithConfig is functionally identical to Encode (both delegate to the same implementation) — the name predates Encode accepting an optional trailing Config. Use p.Encode(value, cfg) in new code, or Marshal when a []byte output is acceptable. EncodeWithConfig will not be removed within v1.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
value | any | Yes | Value to encode |
cfg | Config | No | Encoding configuration (optional) |
// Fully equivalent to p.Encode(data, json.PrettyConfig())
result, err := p.EncodeWithConfig(data, json.PrettyConfig())EncodeBatch
Signature: func (p *Processor) EncodeBatch(pairs map[string]any, cfg ...Config) (string, error)
Batch-encodes key-value pairs into a JSON object.
result, err := p.EncodeBatch(map[string]any{
"name": "CyberGo",
"version": "1.0.0",
})EncodeFields
Signature: func (p *Processor) EncodeFields(value any, fields []string, cfg ...Config) (string, error)
Encodes only the specified fields — commonly used for partial serialization.
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Private string `json:"private"`
}
user := User{Name: "CyberGo", Email: "[email protected]", Private: "secret"}
// Encode only the name and email fields
result, err := p.EncodeFields(user, []string{"name", "email"})EncodeStream
Signature: func (p *Processor) EncodeStream(values any, cfg ...Config) (string, error)
Encodes multiple values into a JSON array stream. values is usually a slice or enumerable collection, and the output is a JSON array string like [v1,v2,...].
values := []any{"item1", "item2", "item3"}
result, err := p.EncodeStream(values)Encoding/Decoding
Marshal
Signature: func (p *Processor) Marshal(value any, cfg ...Config) ([]byte, error)
Encodes a Go value into a JSON byte slice. 100% compatible with encoding/json.Marshal.
Output is always HTML-escaped
Consistent with encoding/json.Marshal, this method's output is always HTML-escaped — even a passed cfg setting EscapeHTML=false is overridden on this path. When the caller must control escaping, use Encode.
data, err := p.Marshal(map[string]any{"name": "CyberGo"})
if err != nil {
panic(err)
}
fmt.Println(string(data)) // {"name":"CyberGo"}MarshalIndent
Signature: func (p *Processor) MarshalIndent(value any, prefix, indent string, cfg ...Config) ([]byte, error)
Encodes a Go value into a pretty-printed JSON byte slice. 100% compatible with encoding/json.MarshalIndent.
data, err := p.MarshalIndent(user, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))Unmarshal
Signature: func (p *Processor) Unmarshal(data []byte, value any, cfg ...Config) error
Parses a JSON byte slice into a target variable. 100% compatible with encoding/json.Unmarshal.
var user User
err := p.Unmarshal([]byte(`{"name":"Alice","age":30}`), &user)
if err != nil {
panic(err)
}Formatting
Prettify
Signature: func (p *Processor) Prettify(jsonStr string, cfg ...Config) (string, error)
Formats a JSON string with indentation. Defaults to 2-space indentation; customizable via the Indent / Prefix fields of cfg.
pretty, err := p.Prettify(`{"name":"Alice","age":30}`)
// Output:
// {
// "name": "Alice",
// "age": 30
// }
// 4-space indentation
cfg := json.DefaultConfig()
cfg.Indent = " "
pretty, err = p.Prettify(`{"name":"Alice","age":30}`, cfg)Print (removed)
API change notice
Print, PrintE, PrintPretty, and PrintPrettyE have been removed from the library and are no longer provided. Use these replacements:
// Compact output
s, err := p.Encode(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(s)
// Pretty output
pretty, err := p.EncodePretty(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(pretty)ValidateSchema
Signature: func (p *Processor) ValidateSchema(jsonStr string, schema *Schema, cfg ...Config) ([]ValidationError, error)
Validates JSON data against the given Schema. Schema violations are reported via the returned []ValidationError; error is non-empty only when parsing or a precondition fails (e.g. invalid JSON, schema is nil) — validation passing returns (nil, nil), and validation failing with a normal flow returns (non-empty slice, nil).
schema := &json.Schema{
Type: "object",
Required: []string{"name", "email"},
Properties: map[string]*json.Schema{
"name": {Type: "string", MinLength: 1},
"email": {Type: "string", Format: "email"},
},
}
errors, err := p.ValidateSchema(jsonStr, schema)
if err != nil {
panic(err)
}
for _, ve := range errors {
fmt.Printf("Path %s: %s\n", ve.Path, ve.Message)
}Formatting Operations
Compact
Signature: func (p *Processor) Compact(jsonStr string, cfg ...Config) (string, error)
Compacts a JSON string, removing all whitespace.
Naming difference between the method and the package-level function
The "string in, string out" compaction has different names at the two entry points: package-level json.CompactString(s) versus the method's preferred name p.Compact(s) (an equivalent CompactString method is also provided). The package-level json.Compact(dst, src) is the encoding/json.Compact-compatible Buffer form; the corresponding method is CompactBuffer, not this one.
compact, err := p.Compact(`{"name": "CyberGo"}`)
// Output: {"name":"CyberGo"}CompactString
Signature: func (p *Processor) CompactString(jsonStr string, cfg ...Config) (string, error)
Compacts JSON in a string-in, string-out form; behaves identically to Compact and is the Processor mirror of the package-level json.CompactString — use it when you want the same name as the package-level function.
compact, err := p.CompactString(`{"name": "CyberGo"}`)
// Output: {"name":"CyberGo"}CompactBuffer
Signature: func (p *Processor) CompactBuffer(dst *bytes.Buffer, src []byte, cfg ...Config) error
Compacts JSON and writes it to a Buffer. Signature-compatible with encoding/json.Compact; the Buffer form of Compact (package-level counterpart: json.Compact).
var buf bytes.Buffer
err := p.CompactBuffer(&buf, []byte(`{"name": "test"}`))Indent
Signature: func (p *Processor) Indent(dst *bytes.Buffer, src []byte, prefix, indent string, cfg ...Config) error
Formats JSON and writes it to a Buffer.
var buf bytes.Buffer
err := p.Indent(&buf, []byte(`{"name":"test"}`), "", " ")HTMLEscape
Signature: func (p *Processor) HTMLEscape(dst *bytes.Buffer, src []byte, cfg ...Config)
HTML-escapes JSON and writes it to a Buffer.
var buf bytes.Buffer
p.HTMLEscape(&buf, []byte(`{"html":"<script>alert(1)</script>"}`))See Also
- Config - Configuration options
- Parse & Load - Parse/Load methods