How to get JSON when the decoder fails to mark?

advertisements

I am using a json.Decoder to decode JSON delivered over a network stream. It works fine, but whenever someone sends data that doesn't fit the schema (e.g. sending a negative integer when the struct's field type is unsigned) it returns an error with a vague message that doesn't pinpoint the offending property. This makes debugging more difficult.

Is there any way to extract the JSON that the decoder was trying to unmarshal when it errored? Here's a small reproducable snippet:

package main

import (
    "bytes"
    "fmt"
    "encoding/json"
)

func main() {
    buff := bytes.NewBufferString("{\"bar\": -123}")
    decoder := json.NewDecoder(buff)

    var foo struct{
        Bar uint32
    }
    if err := decoder.Decode(&foo); err != nil {
        fmt.Println(err)
        fmt.Println("TODO: how to get JSON that caused this error?")
    } else {
        fmt.Println(foo.Bar)
    }
}

Or on playground: https://play.golang.org/p/-WdYBkYEzJ


Some information is in the error, which is of type *json.UnamrshalTypeError

type UnmarshalTypeError struct {
        Value  string       // description of JSON value - "bool", "array", "number -5"
        Type   reflect.Type // type of Go value it could not be assigned to
        Offset int64        // error occurred after reading Offset bytes
}

You can get the offset in the json string, the reflect.Type of the field being unmarshaled into, and the json description of the Value. This can still pose a problem for types that implement their own unmarshaling logic, which is referenced by issue 11858