Compare commits

...

2 Commits

Author SHA1 Message Date
huangsimin
a7eac74d36 json to table 2020-03-06 18:33:24 +08:00
huangsimin
63a8fb51d3 add Method 2020-03-06 18:04:32 +08:00
2 changed files with 57 additions and 0 deletions

26
edb_test.go Normal file
View File

@ -0,0 +1,26 @@
package gjson
import (
"fmt"
"testing"
)
func TestMethodCase(t *testing.T) {
content := `{"name":{"first":"int ","last":"Prichard"}, "age":(47)}`
r := Parse(content)
t.Error(r.Get("age"))
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))
}

View File

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