From 800ce5e92777d1d865edd8b4f12082584869c1d1 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 17 Oct 2016 17:39:27 -0700 Subject: [PATCH] added Index field --- README.md | 1 + gjson.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 1ee5ae3..1aa0765 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/gjson.go b/gjson.go index 6ea5ebe..0945497 100644 --- a/gjson.go +++ b/gjson.go @@ -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 }