From ac4cd1ab55e6f7741d9f1440f23e10eca52a5342 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Wed, 2 Nov 2016 13:45:25 -0700 Subject: [PATCH] empty arrays for non-existent values #11 --- README.md | 9 ++++++++- gjson.go | 13 +++++++++---- gjson_test.go | 13 ++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index aad39de..8d682a0 100644 --- a/README.md +++ b/README.md @@ -76,12 +76,13 @@ The dot and wildcard characters can be escaped with '\'. ``` "name.last" >> "Anderson" "age" >> 37 +"children" >> ["Sara","Alex","Jack"] "children.#" >> 3 "children.1" >> "Alex" "child*.2" >> "Jack" "c?ildren.0" >> "Sara" "fav\.movie" >> "Deer Hunter" -"friends.#.first" >> [ "James", "Roger" ] +"friends.#.first" >> ["James","Roger"] "friends.1.last" >> "Craig" ``` To query an array: @@ -129,6 +130,12 @@ result.Get(path string) Result The `result.Value()` function returns an `interface{}` which requires type assertion and is one of the following Go types: + + +The `result.Array()` funtion returns back an array of values. +If the result represents a non-existent value, then an empty array will be returned. +If the result is not a JSON array, the return value will be an array containing one result. + ```go boolean >> bool number >> float64 diff --git a/gjson.go b/gjson.go index 9849311..9624eab 100644 --- a/gjson.go +++ b/gjson.go @@ -138,9 +138,13 @@ func (t Result) Float() float64 { } } -// Array returns back an array of children. If the result is not -// a JSON array, the return will be an array containing one result. +// Array returns back an array of values. +// If the result represents a non-existent value, then an empty array will be returned. +// If the result is not a JSON array, the return value will be an array containing one result. func (t Result) Array() []Result { + if !t.Exists() { + return nil + } if t.Type != JSON { return []Result{t} } @@ -148,7 +152,7 @@ func (t Result) Array() []Result { return r.a } -// Map returns back an map of children. The result should be a JSON array. +// Map returns back an map of values. The result should be a JSON array. func (t Result) Map() map[string]Result { if t.Type != JSON { return map[string]Result{} @@ -1154,11 +1158,12 @@ type parseContext struct { // } // "name.last" >> "Anderson" // "age" >> 37 +// "children" >> ["Sara","Alex","Jack"] // "children.#" >> 3 // "children.1" >> "Alex" // "child*.2" >> "Jack" // "c?ildren.0" >> "Sara" -// "friends.#.first" >> [ "James", "Roger" ] +// "friends.#.first" >> ["James","Roger"] // func Get(json, path string) Result { var i int diff --git a/gjson_test.go b/gjson_test.go index d91b2cd..976588a 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -446,7 +446,7 @@ func TestUnmarshalMap(t *testing.T) { } func TestSingleArrayValue(t *testing.T) { - var json = `{"key": "value"}` + var json = `{"key": "value","key2":[1,2,3,4,"A"]}` var result = Get(json, "key") var array = result.Array() if len(array) != 1 { @@ -455,6 +455,17 @@ func TestSingleArrayValue(t *testing.T) { if array[0].String() != "value" { t.Fatal("got %s, should be %s", array[0].String(), "value") } + + array = Get(json, "key2.#").Array() + if len(array) != 1 { + t.Fatal("got '%v', expected '%v'", len(array), 1) + } + + array = Get(json, "key3").Array() + if len(array) != 0 { + t.Fatal("got '%v', expected '%v'", len(array), 0) + } + } type BenchStruct struct {