MCPcopy
hub / github.com/buger/jsonparser / ArrayEach

Function ArrayEach

parser.go:1027–1109  ·  view source on GitHub ↗

SYS-REQ-006, SYS-REQ-028, SYS-REQ-029, SYS-REQ-052, SYS-REQ-053, SYS-REQ-055, SYS-REQ-083 ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.

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

Source from the content-addressed store, hash-verified

1025// SYS-REQ-006, SYS-REQ-028, SYS-REQ-029, SYS-REQ-052, SYS-REQ-053, SYS-REQ-055, SYS-REQ-083
1026// ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
1027func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (offset int, err error) {
1028 if len(data) == 0 {
1029 return -1, MalformedObjectError
1030 }
1031
1032 nT := nextToken(data)
1033 if nT == -1 {
1034 return -1, MalformedJsonError
1035 }
1036
1037 offset = nT + 1
1038
1039 if len(keys) > 0 {
1040 if offset = searchKeys(data, keys...); offset == -1 {
1041 return offset, KeyPathNotFoundError
1042 }
1043
1044 // Go to closest value
1045 nO := nextToken(data[offset:])
1046 if nO == -1 {
1047 return offset, MalformedJsonError
1048 }
1049
1050 offset += nO
1051
1052 if data[offset] != '[' {
1053 return offset, MalformedArrayError
1054 }
1055
1056 offset++
1057 }
1058
1059 nO := nextToken(data[offset:])
1060 if nO == -1 {
1061 return offset, MalformedJsonError
1062 }
1063
1064 offset += nO
1065
1066 if data[offset] == ']' {
1067 return offset, nil
1068 }
1069
1070 for {
1071 v, t, o, e := Get(data[offset:])
1072
1073 if o == 0 {
1074 // When Get returns endOffset==0, it always means a parse error
1075 // (no valid value found at the current position). The former
1076 // e==nil/break branch was structurally unreachable because Get
1077 // never returns endOffset==0 without an error.
1078 return offset, e
1079 }
1080
1081 // Pass the error to the callback — the callback signature declares
1082 // an err parameter, so callers who check it should see real errors.
1083 cb(v, t, offset+o-len(v), e)
1084

Callers 15

BenchmarkJsonParserLargeFunction · 0.92
TestTraversalDeterminismFunction · 0.85
TestTraversalNilSafetyFunction · 0.85
TestTraversalDeepNestingFunction · 0.85
toArrayFunction · 0.85
toStringArrayFunction · 0.85
TestArrayEachFunction · 0.85

Calls 3

nextTokenFunction · 0.85
searchKeysFunction · 0.85
GetFunction · 0.85

Tested by 15

BenchmarkJsonParserLargeFunction · 0.74
TestTraversalDeterminismFunction · 0.68
TestTraversalNilSafetyFunction · 0.68
TestTraversalDeepNestingFunction · 0.68
toArrayFunction · 0.68
toStringArrayFunction · 0.68
TestArrayEachFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…