-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add ArrayIterator to iterate over array values #254
base: master
Are you sure you want to change the base?
Conversation
d1498ac
to
2d47b9e
Compare
@buger any chance this can be merged in? i'm happy to pickup the PR and add any changes/tests/etc you want. imo the proposed API feels a lot more ergonomic than |
@prateek can you provide example of use-cases when using such iterator allows you to do smth more then current API? Overall if I get convinced, I do not mind to add iterators, but better to do it consitently, and also implement it for ObjectEach, and KeyEach. Also I wonder if there is room for refactoring here, e.g. make ArrayEach use ArrayIterator internally, will there be big performance penalty? |
@buger I don't think there's anything this API lets you do that the other doesn't. The iterator version would lead to simpler/easier to maintain code IMO. Maybe this should be a separate PR for discussion but while I have you here -- Alternative API
sample usage (using @80avin's data set from above)
Things I prefer about this:
As to your question about refactoring - i don't know how you handle offsets internally and/or if you want to maintain a b/w compat API. probably easiest to share the internals of the iterator + arrayeach code to start. other considerations |
I see, seems like you have a good sense about this. I trust your view here, and lets make iterators as alternative API. Thanks! |
@prateek Thanks for the suggestions. Looking at the range proposal, I'm finding that to be better as we can write like: Taking inspiration from scanner examplejsoniter := jsonparser.ArrayIterator(data, "menu", "items")
if err := jsoniter.Err(); err != nil {
fmt.Println("failed to read", err)
}
for _type, val := jsoniter.All {
// do something
}
if err := jsoniter.Err(); err != nil {
fmt.Println("error", err)
} Iterators are already in 1.23 release candidates and the stable release is also planned to for this month. Maybe @buger you can take the decision here. I'm way too nascent in golang to decide. |
Having played with this a bit the For example you can range over string IDs in your menu json like this
By defining an iterator
|
Description: Fixes #253
Benchmark before change:
Benchmark after change:
For running benchmarks use:
Sample usage