2019-05-08 22:04:36 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://gitee.com/johng/gp.
|
|
|
|
|
|
|
|
package gparser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gogf/gf/g/encoding/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a Parser object with any variable type of <data>,
|
|
|
|
// but <data> should be a map or slice for data access reason,
|
|
|
|
// or it will make no sense.
|
|
|
|
// The <unsafe> param specifies whether using this Parser object
|
|
|
|
// in un-concurrent-safe context, which is false in default.
|
2019-07-23 23:20:27 +08:00
|
|
|
func New(value interface{}, safe ...bool) *Parser {
|
|
|
|
return &Parser{gjson.New(value, safe...)}
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads content from specified file <path>,
|
|
|
|
// and creates a Parser object from its content.
|
2019-07-23 23:20:27 +08:00
|
|
|
func Load(path string, safe ...bool) (*Parser, error) {
|
|
|
|
if j, e := gjson.Load(path, safe...); e == nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
return &Parser{j}, nil
|
|
|
|
} else {
|
|
|
|
return nil, e
|
|
|
|
}
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadContent creates a Parser object from given content,
|
|
|
|
// it checks the data type of <content> automatically,
|
|
|
|
// supporting JSON, XML, YAML and TOML types of data.
|
2019-07-23 23:20:27 +08:00
|
|
|
func LoadContent(data interface{}, safe ...bool) (*Parser, error) {
|
|
|
|
if j, e := gjson.LoadContent(data, safe...); e == nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
return &Parser{j}, nil
|
|
|
|
} else {
|
|
|
|
return nil, e
|
|
|
|
}
|
2019-05-08 22:04:36 +08:00
|
|
|
}
|