mirror of https://github.com/tidwall/gjson.git
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"
|
||||
"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
|
||||
|
|
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
|
||||
// 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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue