mirror of https://github.com/q191201771/naza
new package: nazajson
parent
09421c2b98
commit
e01dda7218
@ -0,0 +1,44 @@
|
||||
package nzjson
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type JSON struct {
|
||||
//raw []byte
|
||||
m map[string]interface{}
|
||||
}
|
||||
|
||||
func New(raw []byte) (JSON, error) {
|
||||
var j JSON
|
||||
err := j.Init(raw)
|
||||
return j, err
|
||||
}
|
||||
|
||||
func (j *JSON) Init(raw []byte) error {
|
||||
return json.Unmarshal(raw, &j.m)
|
||||
}
|
||||
|
||||
func (j *JSON) Exist(path string) bool {
|
||||
return exist(j.m, path)
|
||||
}
|
||||
|
||||
func exist(m map[string]interface{}, path string) bool {
|
||||
ps := strings.Split(path, ".")
|
||||
|
||||
if len(ps) > 1 {
|
||||
v, ok := m[ps[0]]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
mm, ok := v.(map[string]interface{})
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return exist(mm, ps[1])
|
||||
}
|
||||
|
||||
_, ok := m[ps[0]]
|
||||
return ok
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package nzjson
|
||||
|
||||
import (
|
||||
"github.com/q191201771/nezha/pkg/assert"
|
||||
"github.com/q191201771/nezha/pkg/log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var raw = []byte(`
|
||||
{
|
||||
"num": 1,
|
||||
"num2": 0,
|
||||
"flag": true,
|
||||
"flag2": false,
|
||||
"str": "aaa",
|
||||
"str2": "",
|
||||
"obj": {
|
||||
"onum": 2,
|
||||
"onum3": 0,
|
||||
"oflag": true,
|
||||
"oflag3": false,
|
||||
"ostr": "bbb",
|
||||
"ostr3": ""
|
||||
},
|
||||
"arr": [],
|
||||
"arr2": [1, 2]
|
||||
}
|
||||
`)
|
||||
|
||||
func TestExist(t *testing.T) {
|
||||
var exist bool
|
||||
|
||||
j, err := New(raw)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
exist = j.Exist("num")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("flag")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("str")
|
||||
assert.Equal(t, true, exist)
|
||||
|
||||
exist = j.Exist("num2")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("flag2")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("str2")
|
||||
assert.Equal(t, true, exist)
|
||||
|
||||
exist = j.Exist("arr")
|
||||
assert.Equal(t, true, exist)
|
||||
|
||||
exist = j.Exist("arr2")
|
||||
assert.Equal(t, true, exist)
|
||||
|
||||
exist = j.Exist("obj")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("obj.onum")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("obj.oflag")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("obj.ostr")
|
||||
assert.Equal(t, true, exist)
|
||||
|
||||
exist = j.Exist("obj.onum3")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("obj.oflag3")
|
||||
assert.Equal(t, true, exist)
|
||||
exist = j.Exist("obj.ostr3")
|
||||
assert.Equal(t, true, exist)
|
||||
|
||||
exist = j.Exist("notexist")
|
||||
assert.Equal(t, false, exist)
|
||||
|
||||
exist = j.Exist("notexist.notexist")
|
||||
assert.Equal(t, false, exist)
|
||||
|
||||
exist = j.Exist("obj.notexist")
|
||||
assert.Equal(t, false, exist)
|
||||
|
||||
exist = j.Exist("obj.notexist.notexist")
|
||||
assert.Equal(t, false, exist)
|
||||
|
||||
exist = j.Exist("num.notexist")
|
||||
assert.Equal(t, false, exist)
|
||||
|
||||
exist = j.Exist(".")
|
||||
assert.Equal(t, false, exist)
|
||||
exist = j.Exist("..")
|
||||
assert.Equal(t, false, exist)
|
||||
}
|
||||
|
||||
func BenchmarkExist(b *testing.B) {
|
||||
var exist bool
|
||||
|
||||
j, _ := New(raw)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
exist = j.Exist("num")
|
||||
exist = j.Exist("flag")
|
||||
exist = j.Exist("str")
|
||||
|
||||
exist = j.Exist("obj")
|
||||
exist = j.Exist("obj.onum")
|
||||
exist = j.Exist("obj.oflag")
|
||||
exist = j.Exist("obj.ostr")
|
||||
|
||||
exist = j.Exist("notexist")
|
||||
|
||||
exist = j.Exist("obj.notexist")
|
||||
|
||||
exist = j.Exist("obj.notexist.notexist")
|
||||
|
||||
exist = j.Exist(".")
|
||||
exist = j.Exist("..")
|
||||
}
|
||||
log.Debug(exist)
|
||||
}
|
Loading…
Reference in New Issue