francoispqt / gojay

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

Invalid JSON, wrong char ':' found at position xxxx #76

Closed marcopaganini closed 5 years ago

marcopaganini commented 5 years ago

Hello,

I'm trying to parse a simple JSON file and somehow it always gives me an Invalid JSON message. Other parsers work fine. I'm unsure if this is a bug in gojay of if I'm doing something wrong.

I'm attaching a simple program that shows the problem: gojay.go.gz

The source is also available here for immediate viewing: https://repl.it/@marcopaganini/GojayUnableToParse

(Unfortunately, repl.it does not allow loading external libraries, so it's impossible to run it there).

francoispqt commented 5 years ago

Within an UnmarshalJSONArray or UnmarshalJSONObject don't use dec.DecodeObject or dec.DecodeArray but use dec.Object or dec.Array.

Example in your code:

func (x *areas) UnmarshalJSONArray(dec *gojay.Decoder) error {
    err := dec.DecodeObject(x.areaRecord) // <----- replace by dec.Object(x.areaRecord)
    fmt.Printf("Back from arearecord with %v, err = %v\n", x.areaRecord, err)
    if err != nil {
        return err
    }
    return nil
}
marcopaganini commented 5 years ago

Excellent, that did the trick, thank you.

The documentation and examples section seem a bit light on the differences between dec.Add vs dec. vs dec.Decode. Maybe we should have a more detailed description or a more "complete" example? I could clean up this example and send a PR if you'd be interested.

francoispqt commented 5 years ago

dec.AddXXX and dec.XXX are basically the same (they parse keys/values inside objects/arrays) while dec.DecodeXXX are top level methods (they decode a whole JSON). Yes I agree that documentation can be slightly reworked in that regard. I'd be very happy if you submit a PR :)

Thanks

marcopaganini commented 5 years ago

Excellent, thanks. I'll certainly send you a few PRs. I'm closing this bug. Thanks again.