Config可以解析成json,逆向操作如何实现
safejob opened this issue · comments
safejob commented
json.Marshal可以直接将Config编码为json格式,如何将json解码成Config结构体呢?
clessli commented
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())
}`