-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbagger.go
More file actions
40 lines (36 loc) · 800 Bytes
/
bagger.go
File metadata and controls
40 lines (36 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package jsonbagger
// ExtractJSON returns the first JSON object found in the input string.
func ExtractJSON(input string) (string, error) {
begin, end, err := extractJSONIndexes(input)
if err != nil {
return "", err
}
return input[begin:end], nil
}
// extractJSONIndexes returns the indexes of the first JSON object found in the input string.
func extractJSONIndexes(input string) (begin, end int, err error) {
var jsonFound bool
var count uint8
for i, character := range input {
if character == '{' {
if count == 255 {
err = ErrNestingOverflow
return
}
count++
if !jsonFound {
begin = i
}
jsonFound = true
}
if character == '}' && count > 0 {
count--
}
if count == 0 && jsonFound {
end = i + 1
return
}
}
err = ErrNotFound
return
}