diff --git a/edb_test.go b/edb_test.go new file mode 100644 index 0000000..8764486 --- /dev/null +++ b/edb_test.go @@ -0,0 +1,24 @@ +package gjson + +import ( + "fmt" + "testing" +) + +func TestMethodCase(t *testing.T) { + content := `{"name":{"first":"int ","last":"Prichard"}, "age":(47)}` + r := Parse(content) + + t.Error(fmt.Sprintf("%#v", r)) + + content = `(12+23)` + r = Parse(content) + + t.Error(fmt.Sprintf("%#v", r)) + + content = `[(12+2), 213, {"name": "das", "key": (23+4)}]` + r = Parse(content) + + t.Error(fmt.Sprintf("%#v", r)) + +} diff --git a/gjson.go b/gjson.go index 0b6dcb0..ffd1a74 100644 --- a/gjson.go +++ b/gjson.go @@ -31,6 +31,8 @@ const ( True // JSON is a raw block of JSON JSON + // Method is for query fucntion + Method ) // String returns a string representation of the type. @@ -50,6 +52,8 @@ func (t Type) String() string { return "True" case JSON: return "JSON" + case Method: + return "Method" } } @@ -95,6 +99,8 @@ func (t Result) String() string { return t.Raw case True: return "true" + case Method: + return t.Raw } } @@ -448,6 +454,10 @@ func Parse(json string) Result { case '"': value.Type = String value.Raw, value.Str = tostr(json[i:]) + case '(': + value.Type = Method + value.Raw = json[i:] + value.Str = json[i+1 : len(json)-1] } break } @@ -646,6 +656,9 @@ func parseString(json string, i int) (int, string, bool, bool) { if json[i] == '"' { return i + 1, json[s-1 : i+1], false, true } + if json[i] == ')' { + return i + 1, json[s-1 : i+1], false, true + } if json[i] == '\\' { i++ for ; i < len(json); i++ { @@ -2125,6 +2138,24 @@ func parseAny(json string, i int, hit bool) (int, Result, bool) { continue } switch json[i] { + case '(': + i++ + var vesc bool + var ok bool + i, val, vesc, ok = parseString(json, i) + if !ok { + return i, res, false + } + if hit { + res.Type = Method + res.Raw = val + if vesc { + res.Str = unescape(val[1 : len(val)-1]) + } else { + res.Str = val[1 : len(val)-1] + } + } + return i, res, true case '"': i++ var vesc bool