exists function

This commit is contained in:
Josh Baker 2016-08-12 08:39:08 -07:00
parent 09a331fffa
commit 725f3caa55
3 changed files with 26 additions and 3 deletions

View File

@ -48,7 +48,10 @@ This will print:
Prichard Prichard
``` ```
A path is a series of keys separated by a dot. A key may contain special wildcard characters '*' and '?'. To access an array value use the index as the key. To get the number of elements in an array use the '#' character. A path is a series of keys separated by a dot.
A key may contain special wildcard characters '\*' and '?'.
To access an array value use the index as the key.
To get the number of elements in an array use the '#' character.
The dot and wildcard character can be escaped with '\'. The dot and wildcard character can be escaped with '\'.
``` ```
{ {
@ -69,14 +72,14 @@ The dot and wildcard character can be escaped with '\'.
## Result Type ## Result Type
GJSON supports the json types `string`, `number`, `bool`, and `null`. Arrays and Objects are returned as their raw json types. GJSON supports the json types `string`, `number`, `bool`, and `null`.
Arrays and Objects are returned as their raw json types.
The `Result` type holds one of these types: The `Result` type holds one of these types:
``` ```
bool, for JSON booleans bool, for JSON booleans
float64, for JSON numbers float64, for JSON numbers
Number, for JSON numbers
string, for JSON string literals string, for JSON string literals
nil, for JSON null nil, for JSON null
``` ```
@ -89,6 +92,12 @@ result.Value() // interface{} which may be nil, string, float64, or bool
// Or just get the value in one step. // Or just get the value in one step.
gjson.Get(json, "name.last").Value() gjson.Get(json, "name.last").Value()
// Check for the existence of a value.
if gjson.Get(json, "name.last").Exists(){
println("value exists")
}
``` ```
To directly access the value from its original type: To directly access the value from its original type:

View File

@ -51,6 +51,15 @@ func (t Result) String() string {
} }
} }
// Exists returns true if value exists.
//
// if gjson.Get(json, "name.last").Exists(){
// println("value exists")
// }
func (t Result) Exists() bool {
return t.Type != Null && len(t.Raw) != 0
}
// Value returns one of these types: // Value returns one of these types:
// //
// bool, for JSON booleans // bool, for JSON booleans
@ -97,6 +106,8 @@ type frame struct {
// A key may contain special wildcard characters '*' and '?'. // A key may contain special wildcard characters '*' and '?'.
// To access an array value use the index as the key. // To access an array value use the index as the key.
// To get the number of elements in an array use the '#' character. // To get the number of elements in an array use the '#' character.
// The dot and wildcard character can be escaped with '\'.
//
// { // {
// "name": {"first": "Tom", "last": "Anderson"}, // "name": {"first": "Tom", "last": "Anderson"},
// "age":37, // "age":37,

View File

@ -103,6 +103,9 @@ var basicJSON = `{"age":100, "name":{"here":"B\\\"R"},
func TestBasic(t *testing.T) { func TestBasic(t *testing.T) {
var token Result var token Result
if !Get(basicJSON, "name.last").Exists() {
t.Fatal("expected true, got false")
}
token = Get(basicJSON, "name.here") token = Get(basicJSON, "name.here")
if token.String() != "B\\\"R" { if token.String() != "B\\\"R" {
t.Fatal("expecting 'B\\\"R'", "got", token.String()) t.Fatal("expecting 'B\\\"R'", "got", token.String())