add function LoadContentType for package gjson

This commit is contained in:
Jack 2020-08-12 20:38:48 +08:00
parent 7cc1b239d4
commit 607821ecbc
2 changed files with 15 additions and 5 deletions

View File

@ -189,14 +189,22 @@ func LoadContent(data interface{}, safe ...bool) (*Json, error) {
if len(content) == 0 {
return New(nil, safe...), nil
}
return LoadContentType(checkDataType(content), content, safe...)
}
// LoadContentType creates a Json object from given type and content,
// supporting data content type as follows:
// JSON, XML, INI, YAML and TOML.
func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, error) {
content := gconv.Bytes(data)
if len(content) == 0 {
return New(nil, safe...), nil
}
//ignore UTF8-BOM
if content[0] == 0xEF && content[1] == 0xBB && content[2] == 0xBF {
content = content[3:]
}
return doLoadContent(checkDataType(content), content, safe...)
return doLoadContent(dataType, content, safe...)
}
// checkDataType automatically checks and returns the data type for <content>.

View File

@ -113,8 +113,10 @@ func (c *Config) filePath(file ...string) (path string) {
// The parameter <path> can be absolute or relative path,
// but absolute path is strongly recommended.
func (c *Config) SetPath(path string) error {
isDir := false
realPath := ""
var (
isDir = false
realPath = ""
)
if file := gres.Get(path); file != nil {
realPath = path
isDir = file.FileInfo().IsDir()