jsonptrerror

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

jsonptrerror - Extends encoding/json with errors reported as JSON Pointer (RFC 6901)

GoDoc Travis-CI Codecov Go Report Card

This package is a wrapper around the standard encoding/json package to improve the reporting of location for unmarshalling errors: UnmarshalTypeError are enhanced to also include a JSON Pointer (see RFC6901) indicating the location of the error: see jsonptrerror.UnmarshalTypeError.

Status

The aim is code coverage of 100%. Use go coverage tools and consider any code not covered by the testsuite as never tested and full of bugs.

See latest coverage report at codecov.io.

License

Copyright 2017-2018 Olivier Mengué

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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

func Unmarshal

func Unmarshal(document []byte, v interface{}) error

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.

json.UnmarshalTypeError is translated to the extended jsonptrerror.UnmarshalTypeError.

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

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.

The current implementation keeps a duplicate copy of the JSON document in memory.

func (*Decoder) Token

func (d *Decoder) Token() (json.Token, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL