Ai
5 Star 6 Fork 4

zstackio/zstack-vyos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
json.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
pengchao.liu 提交于 2022-06-15 16:17 +08:00 . [vyos]: replace vyos cli
package utils
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/pkg/errors"
)
func JsonDecodeHttpRequest(req *http.Request, val interface{}) (err error) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return errors.Wrap(err, "unable to read the request, %s")
}
if err = json.Unmarshal(body, val); err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to parse string '%s' to JSON object", string(body)))
}
return nil
}
func JsonLoadConfig(filepath string, v interface{}) error {
if filepath == "" {
return errors.New("filepath can not be empty")
}
if ok, err := PathExists(filepath); err != nil || !ok {
return nil
}
f, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
if len(f) == 0 {
return nil
}
return json.Unmarshal(f, v)
}
func JsonStoreConfig(filepath string, v interface{}) error {
var out bytes.Buffer
if filepath == "" {
return errors.New("filepath can not be empty")
}
data, err := json.Marshal(v)
if err != nil {
return err
}
err = json.Indent(&out, data, "", "\t")
if err != nil {
return err
}
if ok, err := PathExists(filepath); err != nil || !ok {
MkdirForFile(filepath, 0755)
}
content := string(out.Bytes())
content = strings.ReplaceAll(content, "\\u003c", "<")
content = strings.ReplaceAll(content, "\\u003e", ">")
return ioutil.WriteFile(filepath, []byte(content), 0664)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zstackio/zstack-vyos.git
git@gitee.com:zstackio/zstack-vyos.git
zstackio
zstack-vyos
zstack-vyos
master

搜索帮助