forked from mirror/gjson
parent
67e2a63ac7
commit
182ad76050
6
gjson.go
6
gjson.go
|
@ -177,8 +177,8 @@ func (t Result) Time() time.Time {
|
|||
// 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 == Null {
|
||||
return []Result{}
|
||||
}
|
||||
if t.Type != JSON {
|
||||
return []Result{t}
|
||||
|
@ -192,7 +192,7 @@ func (t Result) IsObject() bool {
|
|||
return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '{'
|
||||
}
|
||||
|
||||
// IsObject returns true if the result value is a JSON array.
|
||||
// IsArray returns true if the result value is a JSON array.
|
||||
func (t Result) IsArray() bool {
|
||||
return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '['
|
||||
}
|
||||
|
|
|
@ -1110,3 +1110,22 @@ func TestResultRawForLiteral(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNullArray(t *testing.T) {
|
||||
n := len(Get(`{"data":null}`, "data").Array())
|
||||
if n != 0 {
|
||||
t.Fatalf("expected '%v', got '%v'", 0, n)
|
||||
}
|
||||
n = len(Get(`{}`, "data").Array())
|
||||
if n != 0 {
|
||||
t.Fatalf("expected '%v', got '%v'", 0, n)
|
||||
}
|
||||
n = len(Get(`{"data":[]}`, "data").Array())
|
||||
if n != 0 {
|
||||
t.Fatalf("expected '%v', got '%v'", 0, n)
|
||||
}
|
||||
n = len(Get(`{"data":[null]}`, "data").Array())
|
||||
if n != 1 {
|
||||
t.Fatalf("expected '%v', got '%v'", 1, n)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue