Standard library / encoding/json

encoding/json.MarshalerError

Wraps a failure from custom JSON or text marshaling. Type identifies the marshaled type; Err contains the underlying error. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
type MarshalerError struct {  Type reflect.Type  Err  error  // Has unexported fields.}

Working Example

Example Snippetgo
package main
import (  "encoding/json"  "errors"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
var broken = errors.New("broken")
type Bad struct{}
func (Bad) MarshalJSON() ([]byte, error) { return nil, broken }func main() {  _, e := json.Marshal(Bad{})  var x *json.MarshalerError  fmt.Println(errors.As(e, &x), errors.Is(e, broken))}
Go Reference