forked from mirror/gjson
empty arrays for non-existent values #11
This commit is contained in:
parent
1584ec68ff
commit
ac4cd1ab55
|
@ -76,12 +76,13 @@ The dot and wildcard characters can be escaped with '\'.
|
||||||
```
|
```
|
||||||
"name.last" >> "Anderson"
|
"name.last" >> "Anderson"
|
||||||
"age" >> 37
|
"age" >> 37
|
||||||
|
"children" >> ["Sara","Alex","Jack"]
|
||||||
"children.#" >> 3
|
"children.#" >> 3
|
||||||
"children.1" >> "Alex"
|
"children.1" >> "Alex"
|
||||||
"child*.2" >> "Jack"
|
"child*.2" >> "Jack"
|
||||||
"c?ildren.0" >> "Sara"
|
"c?ildren.0" >> "Sara"
|
||||||
"fav\.movie" >> "Deer Hunter"
|
"fav\.movie" >> "Deer Hunter"
|
||||||
"friends.#.first" >> [ "James", "Roger" ]
|
"friends.#.first" >> ["James","Roger"]
|
||||||
"friends.1.last" >> "Craig"
|
"friends.1.last" >> "Craig"
|
||||||
```
|
```
|
||||||
To query an array:
|
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.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
|
```go
|
||||||
boolean >> bool
|
boolean >> bool
|
||||||
number >> float64
|
number >> float64
|
||||||
|
|
13
gjson.go
13
gjson.go
|
@ -138,9 +138,13 @@ func (t Result) Float() float64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array returns back an array of children. If the result is not
|
// Array returns back an array of values.
|
||||||
// a JSON array, the return will be an array containing one result.
|
// 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 {
|
func (t Result) Array() []Result {
|
||||||
|
if !t.Exists() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if t.Type != JSON {
|
if t.Type != JSON {
|
||||||
return []Result{t}
|
return []Result{t}
|
||||||
}
|
}
|
||||||
|
@ -148,7 +152,7 @@ func (t Result) Array() []Result {
|
||||||
return r.a
|
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 {
|
func (t Result) Map() map[string]Result {
|
||||||
if t.Type != JSON {
|
if t.Type != JSON {
|
||||||
return map[string]Result{}
|
return map[string]Result{}
|
||||||
|
@ -1154,11 +1158,12 @@ type parseContext struct {
|
||||||
// }
|
// }
|
||||||
// "name.last" >> "Anderson"
|
// "name.last" >> "Anderson"
|
||||||
// "age" >> 37
|
// "age" >> 37
|
||||||
|
// "children" >> ["Sara","Alex","Jack"]
|
||||||
// "children.#" >> 3
|
// "children.#" >> 3
|
||||||
// "children.1" >> "Alex"
|
// "children.1" >> "Alex"
|
||||||
// "child*.2" >> "Jack"
|
// "child*.2" >> "Jack"
|
||||||
// "c?ildren.0" >> "Sara"
|
// "c?ildren.0" >> "Sara"
|
||||||
// "friends.#.first" >> [ "James", "Roger" ]
|
// "friends.#.first" >> ["James","Roger"]
|
||||||
//
|
//
|
||||||
func Get(json, path string) Result {
|
func Get(json, path string) Result {
|
||||||
var i int
|
var i int
|
||||||
|
|
|
@ -446,7 +446,7 @@ func TestUnmarshalMap(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSingleArrayValue(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 result = Get(json, "key")
|
||||||
var array = result.Array()
|
var array = result.Array()
|
||||||
if len(array) != 1 {
|
if len(array) != 1 {
|
||||||
|
@ -455,6 +455,17 @@ func TestSingleArrayValue(t *testing.T) {
|
||||||
if array[0].String() != "value" {
|
if array[0].String() != "value" {
|
||||||
t.Fatal("got %s, should be %s", 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 {
|
type BenchStruct struct {
|
||||||
|
|
Loading…
Reference in New Issue