francoispqt / gojay

high performance JSON encoder/decoder with stream API for Golang
MIT License
2.11k stars 112 forks source link

Valid JSON throws error "Invalid JSON, wrong char ':' found" #71

Closed karlgarske closed 6 years ago

karlgarske commented 6 years ago

Working with DecodeObject and UnmarshalerJSONObject interface, and I'm getting an error decoding JSON that I've validated with couple of different validators.

Using the attached issue.go and test.json, console reads, "Invalid JSON, wrong char ':' found at position 50". Using go version 1.10.3.

issue.zip

francoispqt commented 6 years ago

DecodeObject is for top level decoding, just like Decode. Example:

var dec = gojay.NewDecoder(io.Reader)
dec.DecodeObject(gojay.UnmarshalerJSONObject)

In the UnmarshalJSONObject method, use Object or AddObject.

Your code updated:

type Test struct {
    test string
    data NestedObject
}

func (t *Test) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
    switch key {
    case "test":
        return dec.String(&t.test)
    case "data":
        return dec.Object(&t.data)
    }
    return nil
}

Please close the issue if it works as expected, thanks!

karlgarske commented 6 years ago

That was it. Thank you very much -- library is great. Keep up the good work!