added Index field

This commit is contained in:
Josh Baker 2016-10-17 17:39:27 -07:00
parent 2dec1c4e7b
commit 800ce5e927
2 changed files with 11 additions and 0 deletions

View File

@ -106,6 +106,7 @@ result.Str // holds the string
result.Num // holds the float64 number
result.Raw // holds the raw json
result.Multi // holds nested array values
result.Index // index of raw value in original json, zero means index unknown
```
There are a variety of handy functions that work on a result:

View File

@ -37,6 +37,8 @@ type Result struct {
Str string
// Num is the json number
Num float64
// Index of raw value in original json, zero means index unknown
Index int
}
// String returns a string representation of the value.
@ -1131,6 +1133,14 @@ func Get(json, path string) Result {
break
}
}
if len(c.value.Raw) > 0 {
jhdr := *(*reflect.StringHeader)(unsafe.Pointer(&json))
rhdr := *(*reflect.StringHeader)(unsafe.Pointer(&(c.value.Raw)))
c.value.Index = int(rhdr.Data - jhdr.Data)
if c.value.Index < 0 || c.value.Index >= len(json) {
c.value.Index = 0
}
}
return c.value
}