You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/nazajson/json_file.go

42 lines
869 B
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Copyright 2022, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package nazajson
import (
"encoding/json"
"io/ioutil"
)
func MarshalJsonFile(in interface{}, filename string) error {
b, err := json.Marshal(in)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, b, 0755)
return err
}
// UnmarshalJsonFile
//
// @param filenames json文件名支持传入多个文件名按先后顺序读取第一个存在的文件
//
func UnmarshalJsonFile(out interface{}, filenames ...string) (err error) {
var b []byte
for i := range filenames {
b, err = ioutil.ReadFile(filenames[i])
if err == nil {
break
}
}
if err != nil {
return err
}
return json.Unmarshal(b, out)
}