Compare commits

...

3 Commits

Author SHA1 Message Date
Michel Couillard fbeb50b654
Merge b877ac2b1c into bbf40bb0e4 2024-02-15 03:09:42 -08:00
tidwall bbf40bb0e4 Fix backspace and form-feed for Go 1.22 2024-02-14 20:51:40 -07:00
Michel Couillard b877ac2b1c README: Array index as key for ForEach 2023-11-22 09:36:05 -05:00
2 changed files with 15 additions and 10 deletions

View File

@ -338,13 +338,14 @@ println(name.String()) // prints "Elliotte"
The `ForEach` function allows for quickly iterating through an object or array.
The key and value are passed to the iterator function for objects.
Only the value is passed for arrays.
With an array the key is an index and the value is passed.
Returning `false` from an iterator will stop iteration.
```go
result := gjson.Get(json, "programmers")
result.ForEach(func(key, value gjson.Result) bool {
println(value.String())
println(key.Int()) // index of the array
println(value.String())
return true // keep iterating
})
```

View File

@ -2578,15 +2578,19 @@ func TestJSONString(t *testing.T) {
testJSONString(t, s)
testJSONString(t, "R\xfd\xfc\a!\x82eO\x16?_\x0f\x9ab\x1dr")
testJSONString(t, "_\xb9\v\xad\xb3|X!\xb6\xd9U&\xa4\x1a\x95\x04")
testJSONString(t, "\b\f")
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
start := time.Now()
var buf [16]byte
for time.Since(start) < time.Second*2 {
if _, err := rng.Read(buf[:]); err != nil {
t.Fatal(err)
data, _ := json.Marshal("\b\f")
if (string(data) == "\"\\b\\f\"") {
// Go version 1.22+ encodes "\b" and "\f" correctly.
testJSONString(t, "\b\f")
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
start := time.Now()
var buf [16]byte
for time.Since(start) < time.Second*2 {
if _, err := rng.Read(buf[:]); err != nil {
t.Fatal(err)
}
testJSONString(t, string(buf[:]))
}
testJSONString(t, string(buf[:]))
}
}