buger / jsonparser

One of the fastest alternative JSON parser for Go that does not require schema

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot get Error from callback

yb3616 opened this issue · comments

if e != nil {

+1, for now error passed to callback will be always nil and break will never occur. I think the right api would be:

func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int) error, keys ...string) (offset int, err error)

@PiotrKozimor you are right that callback at the moment never receive the error. But I guess proper way to handle it will be smth like:

Instead of

jsonparser/parser.go

Lines 1028 to 1030 in 09bcf22

if e != nil {
return offset, e
}

Do

if err != nil {
  cb(nil, t, offset+o-len(v), e)
  return offset, e
}

Well it does not make TOO much sense from API point of view, since it always returns on the first error. But at least it does not break API (I really care about backward compatibility).

I've set v2 milestone, so when we do next major release, we can "remove" err object from callback or fix it in another way.

@buger It would make sense as a temporary, compatible fix.

Going back to my breaking suggestion - when error is returned from callback, iterating over array would be stopped and this error would be returned from ArrayEach. That's how I see it. Fail fast could bring benefit in some use cases.

Agree with @PiotrKozimor recommendation, I would like that behavior and that would bring it inline with ObjectEach, I think.