ClessLi / bifrost

Web服务器配置文件(Nginx config等)解析工具,提供配置文件展示和修改接口

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Config可以解析成json,逆向操作如何实现

safejob opened this issue · comments

json.Marshal可以直接将Config编码为json格式,如何将json解码成Config结构体呢?

1)Config结构体序列化成json的方法已调整,测试函数如下:
`func TestMarshalJSON(t *testing.T) {
conf, err := resolv.Load("config_test/nginx.conf")

if err != nil {
	t.Log(err)
}

j, jerr := json.MarshalIndent(conf, "", "    ")
//j, jerr := json.Marshal(conf)

if jerr != nil {
	t.Log(jerr)
}

t.Log(string(j))

}2)json解析成Config结构体的方法如下:func TestConfig_UnmarshalJSON(t *testing.T) {
// 读取本地json文件
jdata, rerr := ioutil.ReadFile("config_test/test.json")
if rerr != nil {
t.Log(rerr)
}

    // import ngJson "github.com/ClessLi/go-nginx-conf-parser/pkg/json"
conf, err := ngJson.Unmarshal(jdata, &ngJson.Config{})
if err != nil {
	t.Log(err)
}

t.Log(conf)
t.Log(conf.String())

}`