Documentation ¶
Overview ¶
Package jsonptrerror extends encoding/json.Decoder to return unmarshal errors located with JSON Pointer (RFC 6901).
Requires Go 1.5 because it adds an 'Offset' field to type UnmarshalTypeError.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is the same as encoding/json.Decoder, except Decode returns our UnmarshalTypeError (providing a JSON Pointer) instead of encoding/json.UnmarshalTypeError.
Example ¶
package main import ( "fmt" "strings" "github.com/dolmen-go/jsonptrerror" ) func main() { decoder := jsonptrerror.NewDecoder(strings.NewReader( `{"key": "x", "value": 5}`, )) var out struct { Key string `json:"key"` Value bool `json:"value"` } err := decoder.Decode(&out) fmt.Println(err) if err, ok := err.(*jsonptrerror.UnmarshalTypeError); ok { fmt.Println("Original error:", err.UnmarshalTypeError.Error()) fmt.Println("Error location:", err.Pointer) } }
Output: /value: cannot unmarshal number into Go value of type bool Original error: json: cannot unmarshal number into Go struct field .value of type bool Error location: /value
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
type UnmarshalTypeError ¶
type UnmarshalTypeError struct { json.UnmarshalTypeError Pointer jsonptr.Pointer }
UnmarshalTypeError is an extension of encoding/json.UnmarshalTypeError that also includes the error location as a JSON Pointer (RFC 6901).
func (UnmarshalTypeError) Error ¶
func (e UnmarshalTypeError) Error() string
Click to show internal directories.
Click to hide internal directories.