Skip to content

Commit 2e52cf6

Browse files
committed
Merge branch 'master' of github.com:oliveagle/jsonpath
2 parents e5a552e + 1c146b9 commit 2e52cf6

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

jsonpath.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,15 @@ func parse_token(token string) (op string, key string, args interface{}, err err
256256
var frm interface{}
257257
var to interface{}
258258
if frm, err = strconv.Atoi(strings.Trim(tails[0], " ")); err != nil {
259+
if strings.Trim(tails[0], " ") == "" {
260+
err = nil
261+
}
259262
frm = nil
260263
}
261264
if to, err = strconv.Atoi(strings.Trim(tails[1], " ")); err != nil {
265+
if strings.Trim(tails[1], " ") == "" {
266+
err = nil
267+
}
262268
to = nil
263269
}
264270
args = [2]interface{}{frm, to}
@@ -391,6 +397,12 @@ func get_range(obj, frm, to interface{}) (interface{}, error) {
391397
length := reflect.ValueOf(obj).Len()
392398
_frm := 0
393399
_to := length
400+
if frm == nil {
401+
frm = 0
402+
}
403+
if to == nil {
404+
to = length - 1
405+
}
394406
if fv, ok := frm.(int); ok == true {
395407
if fv < 0 {
396408
_frm = length + fv

jsonpath_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ func Test_jsonpath_JsonPathLookup_1(t *testing.T) {
9090
}
9191
}
9292

93+
// full array
94+
res, err = JsonPathLookup(json_data, "$.store.book[0:].price")
95+
t.Log(err, res)
96+
if res_v, ok := res.([]interface{}); ok != true || res_v[0].(float64) != 8.95 || res_v[1].(float64) != 12.99 || res_v[2].(float64) != 8.99 || res_v[3].(float64) != 22.99 {
97+
t.Errorf("exp: [8.95, 12.99, 8.99, 22.99], got: %v", res)
98+
}
99+
93100
// range
94101
res, err = JsonPathLookup(json_data, "$.store.book[0:1].price")
95102
t.Log(err, res)

0 commit comments

Comments
 (0)