mirror of https://github.com/tidwall/gjson.git
add IsNull helper method
This commit is contained in:
parent
be96719f99
commit
38c3f5c110
9
gjson.go
9
gjson.go
|
@ -571,6 +571,15 @@ func (t Result) Exists() bool {
|
||||||
return t.Type != Null || len(t.Raw) != 0
|
return t.Type != Null || len(t.Raw) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsNull returns true if type is Null.
|
||||||
|
//
|
||||||
|
// if gjson.Get(json, "name.nullable").IsNull() {
|
||||||
|
// println("value is null")
|
||||||
|
// }
|
||||||
|
func (t Result) IsNull() bool {
|
||||||
|
return t.Type == Null
|
||||||
|
}
|
||||||
|
|
||||||
// Value returns one of these types:
|
// Value returns one of these types:
|
||||||
//
|
//
|
||||||
// bool, for JSON booleans
|
// bool, for JSON booleans
|
||||||
|
|
|
@ -136,7 +136,8 @@ var basicJSON = `{"age":100, "name":{"here":"B\\\"R"},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"lastly":{"yay":"final"}
|
"lastly":{"yay":"final"},
|
||||||
|
"nullable": null
|
||||||
}`
|
}`
|
||||||
var basicJSONB = []byte(basicJSON)
|
var basicJSONB = []byte(basicJSON)
|
||||||
|
|
||||||
|
@ -240,6 +241,12 @@ func TestIsArrayIsObject(t *testing.T) {
|
||||||
assert(t, !mtok.IsArray())
|
assert(t, !mtok.IsArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsNull(t *testing.T) {
|
||||||
|
value := get(basicJSON, "nullable")
|
||||||
|
assert(t, value.IsNull())
|
||||||
|
assert(t, value.Exists())
|
||||||
|
}
|
||||||
|
|
||||||
func TestPlus53BitInts(t *testing.T) {
|
func TestPlus53BitInts(t *testing.T) {
|
||||||
json := `{"IdentityData":{"GameInstanceId":634866135153775564}}`
|
json := `{"IdentityData":{"GameInstanceId":634866135153775564}}`
|
||||||
value := Get(json, "IdentityData.GameInstanceId")
|
value := Get(json, "IdentityData.GameInstanceId")
|
||||||
|
|
Loading…
Reference in New Issue