mirror of https://github.com/tidwall/gjson.git
parent
7460ecfe69
commit
7405f21134
|
@ -128,7 +128,7 @@ result.Str // holds the string
|
||||||
result.Num // holds the float64 number
|
result.Num // holds the float64 number
|
||||||
result.Raw // holds the raw json
|
result.Raw // holds the raw json
|
||||||
result.Index // index of raw value in original json, zero means index unknown
|
result.Index // index of raw value in original json, zero means index unknown
|
||||||
result.HashtagIndexes // hashtagIndexes contains the indexes of the elements returned by a query containing the '#' character
|
result.Indexes // Indexes contains the indexes of the elements returned by a query containing the '#' character
|
||||||
```
|
```
|
||||||
|
|
||||||
There are a variety of handy functions that work on a result:
|
There are a variety of handy functions that work on a result:
|
||||||
|
|
21
gjson.go
21
gjson.go
|
@ -64,8 +64,8 @@ type Result struct {
|
||||||
Num float64
|
Num float64
|
||||||
// Index of raw value in original json, zero means index unknown
|
// Index of raw value in original json, zero means index unknown
|
||||||
Index int
|
Index int
|
||||||
// HashtagIndexes contains the Indexes of the elements returned by a query containing the '#' character
|
// Indexes contains the Indexes of the elements returned by a query containing the '#' character
|
||||||
HashtagIndexes []int
|
Indexes []int
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a string representation of the value.
|
// String returns a string representation of the value.
|
||||||
|
@ -1263,7 +1263,7 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
|
||||||
var alog []int
|
var alog []int
|
||||||
var partidx int
|
var partidx int
|
||||||
var multires []byte
|
var multires []byte
|
||||||
var hashtagQueryIndex []int
|
var queryIndexes []int
|
||||||
rp := parseArrayPath(path)
|
rp := parseArrayPath(path)
|
||||||
if !rp.arrch {
|
if !rp.arrch {
|
||||||
n, ok := parseUint(rp.part)
|
n, ok := parseUint(rp.part)
|
||||||
|
@ -1319,7 +1319,7 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
|
||||||
multires = append(multires, ',')
|
multires = append(multires, ',')
|
||||||
}
|
}
|
||||||
multires = append(multires, raw...)
|
multires = append(multires, raw...)
|
||||||
hashtagQueryIndex = append(hashtagQueryIndex, res.Index+parentIndex)
|
queryIndexes = append(queryIndexes, res.Index+parentIndex)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.value = res
|
c.value = res
|
||||||
|
@ -1484,7 +1484,7 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
|
||||||
c.pipe = right
|
c.pipe = right
|
||||||
c.piped = true
|
c.piped = true
|
||||||
}
|
}
|
||||||
var hashtagIndexes = make([]int, 0, 64)
|
var indexes = make([]int, 0, 64)
|
||||||
var jsons = make([]byte, 0, 64)
|
var jsons = make([]byte, 0, 64)
|
||||||
jsons = append(jsons, '[')
|
jsons = append(jsons, '[')
|
||||||
for j, k := 0, 0; j < len(alog); j++ {
|
for j, k := 0, 0; j < len(alog); j++ {
|
||||||
|
@ -1511,7 +1511,7 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
|
||||||
raw = res.String()
|
raw = res.String()
|
||||||
}
|
}
|
||||||
jsons = append(jsons, []byte(raw)...)
|
jsons = append(jsons, []byte(raw)...)
|
||||||
hashtagIndexes = append(hashtagIndexes, res.Index+parentIndex)
|
indexes = append(indexes, res.Index+parentIndex)
|
||||||
k++
|
k++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1520,7 +1520,7 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
|
||||||
jsons = append(jsons, ']')
|
jsons = append(jsons, ']')
|
||||||
c.value.Type = JSON
|
c.value.Type = JSON
|
||||||
c.value.Raw = string(jsons)
|
c.value.Raw = string(jsons)
|
||||||
c.value.HashtagIndexes = hashtagIndexes
|
c.value.Indexes = indexes
|
||||||
return i + 1, true
|
return i + 1, true
|
||||||
}
|
}
|
||||||
if rp.alogok {
|
if rp.alogok {
|
||||||
|
@ -1536,9 +1536,9 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
|
||||||
if !c.value.Exists() {
|
if !c.value.Exists() {
|
||||||
if len(multires) > 0 {
|
if len(multires) > 0 {
|
||||||
c.value = Result{
|
c.value = Result{
|
||||||
Raw: string(append(multires, ']')),
|
Raw: string(append(multires, ']')),
|
||||||
Type: JSON,
|
Type: JSON,
|
||||||
HashtagIndexes: hashtagQueryIndex,
|
Indexes: queryIndexes,
|
||||||
}
|
}
|
||||||
} else if rp.query.all {
|
} else if rp.query.all {
|
||||||
c.value = Result{
|
c.value = Result{
|
||||||
|
@ -1819,6 +1819,7 @@ func Get(json, path string) Result {
|
||||||
if len(path) > 0 && (path[0] == '|' || path[0] == '.') {
|
if len(path) > 0 && (path[0] == '|' || path[0] == '.') {
|
||||||
res := Get(rjson, path[1:])
|
res := Get(rjson, path[1:])
|
||||||
res.Index = 0
|
res.Index = 0
|
||||||
|
res.Indexes = nil
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
return Parse(rjson)
|
return Parse(rjson)
|
||||||
|
|
|
@ -859,9 +859,9 @@ func TestIssue20(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIssue21(t *testing.T) {
|
func TestIssue21(t *testing.T) {
|
||||||
json := `{ "Level1Field1":3,
|
json := `{ "Level1Field1":3,
|
||||||
"Level1Field4":4,
|
"Level1Field4":4,
|
||||||
"Level1Field2":{ "Level2Field1":[ "value1", "value2" ],
|
"Level1Field2":{ "Level2Field1":[ "value1", "value2" ],
|
||||||
"Level2Field2":{ "Level3Field1":[ { "key1":"value1" } ] } } }`
|
"Level2Field2":{ "Level3Field1":[ { "key1":"value1" } ] } } }`
|
||||||
paths := []string{"Level1Field1", "Level1Field2.Level2Field1",
|
paths := []string{"Level1Field1", "Level1Field2.Level2Field1",
|
||||||
"Level1Field2.Level2Field2.Level3Field1", "Level1Field4"}
|
"Level1Field2.Level2Field2.Level3Field1", "Level1Field4"}
|
||||||
|
@ -922,7 +922,7 @@ var complicatedJSON = `
|
||||||
"nestedTagged": {
|
"nestedTagged": {
|
||||||
"Green": "Green",
|
"Green": "Green",
|
||||||
"Map": {
|
"Map": {
|
||||||
"this": "that",
|
"this": "that",
|
||||||
"and": "the other thing"
|
"and": "the other thing"
|
||||||
},
|
},
|
||||||
"Ints": {
|
"Ints": {
|
||||||
|
@ -1291,10 +1291,10 @@ func TestArrayValues(t *testing.T) {
|
||||||
}
|
}
|
||||||
expect := strings.Join([]string{
|
expect := strings.Join([]string{
|
||||||
`gjson.Result{Type:3, Raw:"\"PERSON1\"", Str:"PERSON1", Num:0, ` +
|
`gjson.Result{Type:3, Raw:"\"PERSON1\"", Str:"PERSON1", Num:0, ` +
|
||||||
`Index:0, HashtagIndexes:[]int(nil)}`,
|
`Index:0, Indexes:[]int(nil)}`,
|
||||||
`gjson.Result{Type:3, Raw:"\"PERSON2\"", Str:"PERSON2", Num:0, ` +
|
`gjson.Result{Type:3, Raw:"\"PERSON2\"", Str:"PERSON2", Num:0, ` +
|
||||||
`Index:0, HashtagIndexes:[]int(nil)}`,
|
`Index:0, Indexes:[]int(nil)}`,
|
||||||
`gjson.Result{Type:2, Raw:"0", Str:"", Num:0, Index:0, HashtagIndexes:[]int(nil)}`,
|
`gjson.Result{Type:2, Raw:"0", Str:"", Num:0, Index:0, Indexes:[]int(nil)}`,
|
||||||
}, "\n")
|
}, "\n")
|
||||||
if output != expect {
|
if output != expect {
|
||||||
t.Fatalf("expected '%v', got '%v'", expect, output)
|
t.Fatalf("expected '%v', got '%v'", expect, output)
|
||||||
|
@ -1492,7 +1492,7 @@ func TestDeepSelectors(t *testing.T) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"first": "Roger", "last": "Craig",
|
"first": "Roger", "last": "Craig",
|
||||||
"extra": [40,50,60],
|
"extra": [40,50,60],
|
||||||
"details": {
|
"details": {
|
||||||
"city": "Phoenix",
|
"city": "Phoenix",
|
||||||
|
@ -2122,7 +2122,7 @@ func TestModifierDoubleQuotes(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHashtagIndexes(t *testing.T) {
|
func TestIndexes(t *testing.T) {
|
||||||
var exampleJSON = `{
|
var exampleJSON = `{
|
||||||
"vals": [
|
"vals": [
|
||||||
[1,66,{test: 3}],
|
[1,66,{test: 3}],
|
||||||
|
@ -2150,14 +2150,18 @@ func TestHashtagIndexes(t *testing.T) {
|
||||||
`objectArray.#(age>43)#.first`,
|
`objectArray.#(age>43)#.first`,
|
||||||
[]string{`"`, `"`},
|
[]string{`"`, `"`},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
`objectArray.@reverse.#.first`,
|
||||||
|
nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
r := Get(exampleJSON, tc.path)
|
r := Get(exampleJSON, tc.path)
|
||||||
|
|
||||||
assert(t, len(r.HashtagIndexes) == len(tc.expected))
|
assert(t, len(r.Indexes) == len(tc.expected))
|
||||||
|
|
||||||
for i, a := range r.HashtagIndexes {
|
for i, a := range r.Indexes {
|
||||||
assert(t, string(exampleJSON[a]) == tc.expected[i])
|
assert(t, string(exampleJSON[a]) == tc.expected[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2178,10 +2182,10 @@ func TestHashtagIndexesMatchesRaw(t *testing.T) {
|
||||||
if v.IsArray() {
|
if v.IsArray() {
|
||||||
v.ForEach(func(_, sv Result) bool {
|
v.ForEach(func(_, sv Result) bool {
|
||||||
if sv.IsObject() {
|
if sv.IsObject() {
|
||||||
assert(t, string(exampleJSON[r.HashtagIndexes[0]:r.HashtagIndexes[0]+len(sv.Raw)]) == sv.Raw)
|
assert(t, string(exampleJSON[r.Indexes[0]:r.Indexes[0]+len(sv.Raw)]) == sv.Raw)
|
||||||
}
|
}
|
||||||
if sv.IsArray() {
|
if sv.IsArray() {
|
||||||
assert(t, string(exampleJSON[r.HashtagIndexes[1]:r.HashtagIndexes[1]+len(sv.Raw)]) == sv.Raw)
|
assert(t, string(exampleJSON[r.Indexes[1]:r.Indexes[1]+len(sv.Raw)]) == sv.Raw)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue