add IsNull helper method

This commit is contained in:
Wei Fu 2017-09-18 17:32:03 +08:00
parent be96719f99
commit 38c3f5c110
2 changed files with 17 additions and 1 deletions

View File

@ -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

View File

@ -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")