mirror of
https://gitee.com/johng/gf
synced 2026-07-07 22:27:06 +08:00
improve context feature for ghttp.Request; improve comment for package gjson
This commit is contained in:
@ -1,38 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
)
|
||||
|
||||
type ModifyFieldInfoType struct {
|
||||
Id int64 `json:"id"`
|
||||
New string `json:"new"`
|
||||
}
|
||||
type ModifyFieldInfosType struct {
|
||||
Duration ModifyFieldInfoType `json:"duration"`
|
||||
OMLevel ModifyFieldInfoType `json:"om_level"`
|
||||
}
|
||||
|
||||
type MediaRequestModifyInfo struct {
|
||||
Modify ModifyFieldInfosType `json:"modifyFieldInfos"`
|
||||
Field ModifyFieldInfosType `json:"fieldInfos"`
|
||||
FeedID string `json:"feed_id"`
|
||||
Vid string `json:"id"`
|
||||
}
|
||||
|
||||
var processQueue chan MediaRequestModifyInfo
|
||||
|
||||
func main() {
|
||||
|
||||
jsonContent := `{"dataSetId":2001,"fieldInfos":{"duration":{"id":80079,"value":"59"},"om_level":{"id":2409,"value":"4"}},"id":"g0936lt1u0f","modifyFieldInfos":{"om_level":{"id":2409,"new":"4","old":""}},"timeStamp":1584599734}`
|
||||
var t MediaRequestModifyInfo
|
||||
err := gjson.DecodeTo(jsonContent, &t)
|
||||
fmt.Println(err)
|
||||
fmt.Printf("%+v\n", t)
|
||||
fmt.Println(gjson.New(t).MustToJsonString())
|
||||
type Sender struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
type To struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
b, _ := json.Marshal(t)
|
||||
fmt.Println(string(b))
|
||||
type SendReq struct {
|
||||
Sender *Sender `json:"sender"`
|
||||
//Name string `json:"name"`
|
||||
HtmlContent string `json:"htmlContent"`
|
||||
Subject string `json:"subject"`
|
||||
To []*To `json:"to"`
|
||||
}
|
||||
|
||||
//url := "emailCampaigns"
|
||||
sendreq := SendReq{
|
||||
Sender: &Sender{
|
||||
Name: "123",
|
||||
Email: "globalclienthelp@gmail.com",
|
||||
},
|
||||
To: []*To{{
|
||||
Name: "456",
|
||||
//Email: order.Email,
|
||||
Email: "jinmao88@hotmail.com",
|
||||
}},
|
||||
}
|
||||
subject := ""
|
||||
htmlcontent := ""
|
||||
|
||||
subject = " Your order:" + gstr.Split("11111", "-")[1] + " , Shipment is on its way."
|
||||
htmlcontent = "test"
|
||||
sendreq.Subject = subject
|
||||
sendreq.HtmlContent = htmlcontent
|
||||
j, err := gjson.DecodeToJson(sendreq)
|
||||
fmt.Println(err)
|
||||
fmt.Println(j)
|
||||
}
|
||||
|
||||
@ -76,7 +76,6 @@ func Test_TreeMap_Batch(t *testing.T) {
|
||||
func Test_TreeMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
|
||||
m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
@ -96,6 +95,25 @@ func Test_TreeMap_Iterator(t *testing.T) {
|
||||
t.Assert(i, 2)
|
||||
t.Assert(j, 1)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
|
||||
m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
|
||||
for i := 0; i < 10; i++ {
|
||||
m.IteratorAsc(func(k interface{}, v interface{}) bool {
|
||||
t.Assert(expect[k], v)
|
||||
return true
|
||||
})
|
||||
}
|
||||
j := 0
|
||||
for i := 0; i < 10; i++ {
|
||||
m.IteratorAsc(func(k interface{}, v interface{}) bool {
|
||||
j++
|
||||
return false
|
||||
})
|
||||
}
|
||||
t.Assert(j, 10)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TreeMap_Clone(t *testing.T) {
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gjson provides convenient API for JSON/XML/YAML/TOML data handling.
|
||||
// Package gjson provides convenient API for JSON/XML/INI/YAML/TOML data handling.
|
||||
package gjson
|
||||
|
||||
import (
|
||||
|
||||
@ -17,7 +17,7 @@ import (
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// Val returns the json value.
|
||||
// Value returns the json value.
|
||||
func (j *Json) Value() interface{} {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
@ -31,8 +31,8 @@ func (j *Json) IsNil() bool {
|
||||
return j.p == nil || *(j.p) == nil
|
||||
}
|
||||
|
||||
// Get returns value by specified <pattern>.
|
||||
// It returns all values of current Json object, if <pattern> is empty or not specified.
|
||||
// Get retrieves and returns value by specified <pattern>.
|
||||
// It returns all values of current Json object if <pattern> is given empty or string ".".
|
||||
// It returns nil if no value found by <pattern>.
|
||||
//
|
||||
// We can also access slice item by its index number in <pattern>,
|
||||
@ -132,18 +132,18 @@ func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArray gets the value by specified <pattern>,
|
||||
// GetArray retrieves the value by specified <pattern>,
|
||||
// and converts it to a slice of []interface{}.
|
||||
func (j *Json) GetArray(pattern string, def ...interface{}) []interface{} {
|
||||
return gconv.Interfaces(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetString gets the value by specified <pattern>,
|
||||
// and converts it to string.
|
||||
// GetString retrieves the value by specified <pattern> and converts it to string.
|
||||
func (j *Json) GetString(pattern string, def ...interface{}) string {
|
||||
return gconv.String(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetBytes retrieves the value by specified <pattern> and converts it to byte.
|
||||
func (j *Json) GetBytes(pattern string, def ...interface{}) []byte {
|
||||
return gconv.Bytes(j.Get(pattern, def...))
|
||||
}
|
||||
@ -156,81 +156,98 @@ func (j *Json) GetBool(pattern string, def ...interface{}) bool {
|
||||
return gconv.Bool(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetInt retrieves the value by specified <pattern> and converts it to int.
|
||||
func (j *Json) GetInt(pattern string, def ...interface{}) int {
|
||||
return gconv.Int(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetInt8 retrieves the value by specified <pattern> and converts it to int8.
|
||||
func (j *Json) GetInt8(pattern string, def ...interface{}) int8 {
|
||||
return gconv.Int8(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetInt16 retrieves the value by specified <pattern> and converts it to int16.
|
||||
func (j *Json) GetInt16(pattern string, def ...interface{}) int16 {
|
||||
return gconv.Int16(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetInt32 retrieves the value by specified <pattern> and converts it to int32.
|
||||
func (j *Json) GetInt32(pattern string, def ...interface{}) int32 {
|
||||
return gconv.Int32(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetInt64 retrieves the value by specified <pattern> and converts it to int64.
|
||||
func (j *Json) GetInt64(pattern string, def ...interface{}) int64 {
|
||||
return gconv.Int64(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetUint retrieves the value by specified <pattern> and converts it to uint.
|
||||
func (j *Json) GetUint(pattern string, def ...interface{}) uint {
|
||||
return gconv.Uint(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetUint8 retrieves the value by specified <pattern> and converts it to uint8.
|
||||
func (j *Json) GetUint8(pattern string, def ...interface{}) uint8 {
|
||||
return gconv.Uint8(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetUint16 retrieves the value by specified <pattern> and converts it to uint16.
|
||||
func (j *Json) GetUint16(pattern string, def ...interface{}) uint16 {
|
||||
return gconv.Uint16(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetUint32 retrieves the value by specified <pattern> and converts it to uint32.
|
||||
func (j *Json) GetUint32(pattern string, def ...interface{}) uint32 {
|
||||
return gconv.Uint32(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetUint64 retrieves the value by specified <pattern> and converts it to uint64.
|
||||
func (j *Json) GetUint64(pattern string, def ...interface{}) uint64 {
|
||||
return gconv.Uint64(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetFloat32 retrieves the value by specified <pattern> and converts it to float32.
|
||||
func (j *Json) GetFloat32(pattern string, def ...interface{}) float32 {
|
||||
return gconv.Float32(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetFloat64 retrieves the value by specified <pattern> and converts it to float64.
|
||||
func (j *Json) GetFloat64(pattern string, def ...interface{}) float64 {
|
||||
return gconv.Float64(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetFloats retrieves the value by specified <pattern> and converts it to []float64.
|
||||
func (j *Json) GetFloats(pattern string, def ...interface{}) []float64 {
|
||||
return gconv.Floats(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetInts retrieves the value by specified <pattern> and converts it to []int.
|
||||
func (j *Json) GetInts(pattern string, def ...interface{}) []int {
|
||||
return gconv.Ints(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetStrings gets the value by specified <pattern>,
|
||||
// and converts it to a slice of []string.
|
||||
// GetStrings retrieves the value by specified <pattern> and converts it to []string.
|
||||
func (j *Json) GetStrings(pattern string, def ...interface{}) []string {
|
||||
return gconv.Strings(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetInterfaces is alias of GetArray.
|
||||
// See GetArray.
|
||||
func (j *Json) GetInterfaces(pattern string, def ...interface{}) []interface{} {
|
||||
return gconv.Interfaces(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetTime retrieves the value by specified <pattern> and converts it to time.Time.
|
||||
func (j *Json) GetTime(pattern string, format ...string) time.Time {
|
||||
return gconv.Time(j.Get(pattern), format...)
|
||||
}
|
||||
|
||||
// GetDuration retrieves the value by specified <pattern> and converts it to time.Duration.
|
||||
func (j *Json) GetDuration(pattern string, def ...interface{}) time.Duration {
|
||||
return gconv.Duration(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetGTime retrieves the value by specified <pattern> and converts it to *gtime.Time.
|
||||
func (j *Json) GetGTime(pattern string, format ...string) *gtime.Time {
|
||||
return gconv.GTime(j.Get(pattern), format...)
|
||||
}
|
||||
@ -284,9 +301,8 @@ func (j *Json) Append(pattern string, value interface{}) error {
|
||||
return fmt.Errorf("invalid variable type of %s", pattern)
|
||||
}
|
||||
|
||||
// GetStruct gets the value by specified <pattern>,
|
||||
// and converts it to specified object <pointer>.
|
||||
// The <pointer> should be the pointer to an object.
|
||||
// GetStruct retrieves the value by specified <pattern> and converts it to specified object
|
||||
// <pointer>. The <pointer> should be the pointer to an object.
|
||||
func (j *Json) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.Struct(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
@ -306,18 +322,26 @@ func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...ma
|
||||
return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
// GetMapToMap retrieves the value by specified <pattern> and converts it specified map variable.
|
||||
// The parameter of <pointer> should be type of *map.
|
||||
func (j *Json) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMap(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
// GetMapToMapDeep retrieves the value by specified <pattern> and converts it specified map
|
||||
// variable recursively. The parameter of <pointer> should be type of *map.
|
||||
func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMapDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
// GetMapToMaps retrieves the value by specified <pattern> and converts it specified map slice
|
||||
// variable. The parameter of <pointer> should be type of []map/*map.
|
||||
func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMaps(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
// GetMapToMapsDeep retrieves the value by specified <pattern> and converts it specified map slice
|
||||
// variable recursively. The parameter of <pointer> should be type of []map/*map.
|
||||
func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMapsDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
@ -339,49 +363,63 @@ func (j *Json) ToArray() []interface{} {
|
||||
}
|
||||
|
||||
// ToStruct converts current Json object to specified object.
|
||||
// The <pointer> should be a pointer type.
|
||||
// The <pointer> should be a pointer type of *struct.
|
||||
func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.Struct(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToStructDeep converts current Json object to specified object recursively.
|
||||
// The <pointer> should be a pointer type of *struct.
|
||||
func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.StructDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToStructs converts current Json object to specified object slice.
|
||||
// The <pointer> should be a pointer type of []struct/*struct.
|
||||
func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.Structs(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToStructsDeep converts current Json object to specified object slice recursively.
|
||||
// The <pointer> should be a pointer type of []struct/*struct.
|
||||
func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.StructsDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToMapToMap converts current Json object to specified map variable.
|
||||
// The parameter of <pointer> should be type of *map.
|
||||
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapToMap(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToMapToMapDeep converts current Json object to specified map variable recursively.
|
||||
// The parameter of <pointer> should be type of *map.
|
||||
func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapToMapDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToMapToMaps converts current Json object to specified map variable slice.
|
||||
// The parameter of <pointer> should be type of []map/*map.
|
||||
func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapToMaps(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
|
||||
// The parameter of <pointer> should be type of []map/*map.
|
||||
func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
|
||||
@ -25,22 +25,23 @@ import (
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// New creates a Json 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.
|
||||
// New creates a Json 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 parameter <safe> specifies whether using this Json object in concurrent-safe context, which is
|
||||
// false in default.
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context,
|
||||
// which is false in default.
|
||||
func New(data interface{}, safe ...bool) *Json {
|
||||
return NewWithTag(data, "json", safe...)
|
||||
}
|
||||
|
||||
// NewWithTag creates a Json 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.
|
||||
// NewWithTag creates a Json 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 parameter <tags> specifies priority tags for struct conversion to map, multiple tags joined with char ','.
|
||||
// The parameter <tags> specifies priority tags for struct conversion to map, multiple tags joined
|
||||
// with char ','.
|
||||
//
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context, which is
|
||||
// false in default.
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context, which
|
||||
// is false in default.
|
||||
func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
||||
j := (*Json)(nil)
|
||||
switch data.(type) {
|
||||
@ -92,48 +93,7 @@ func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
||||
return j
|
||||
}
|
||||
|
||||
// Valid checks whether <data> is a valid JSON data type.
|
||||
func Valid(data interface{}) bool {
|
||||
return json.Valid(gconv.Bytes(data))
|
||||
}
|
||||
|
||||
// Encode encodes <value> to JSON data type of bytes.
|
||||
func Encode(value interface{}) ([]byte, error) {
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
// Decode decodes <data>(string/[]byte) to golang variable.
|
||||
func Decode(data interface{}) (interface{}, error) {
|
||||
var value interface{}
|
||||
if err := DecodeTo(gconv.Bytes(data), &value); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Decode decodes <data>(string/[]byte) to specified golang variable <v>.
|
||||
// The <v> should be a pointer type.
|
||||
func DecodeTo(data interface{}, v interface{}) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
|
||||
// Do not use number, it converts float64 to json.Number type,
|
||||
// which actually a string type. It causes converting issue for other data formats,
|
||||
// for example: yaml.
|
||||
//decoder.UseNumber()
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
// DecodeToJson codes <data>(string/[]byte) to a Json object.
|
||||
func DecodeToJson(data interface{}, safe ...bool) (*Json, error) {
|
||||
if v, err := Decode(gconv.Bytes(data)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return New(v, safe...), nil
|
||||
}
|
||||
}
|
||||
|
||||
// Load loads content from specified file <path>,
|
||||
// and creates a Json object from its content.
|
||||
// Load loads content from specified file <path>, and creates a Json object from its content.
|
||||
func Load(path string, safe ...bool) (*Json, error) {
|
||||
if p, err := gfile.Search(path); err != nil {
|
||||
return nil, err
|
||||
@ -143,26 +103,34 @@ func Load(path string, safe ...bool) (*Json, error) {
|
||||
return doLoadContent(gfile.Ext(path), gfcache.GetBinContents(path), safe...)
|
||||
}
|
||||
|
||||
// LoadJson creates a Json object from given JSON format content.
|
||||
func LoadJson(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("json", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
// LoadXml creates a Json object from given XML format content.
|
||||
func LoadXml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("xml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
func LoadYaml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("yaml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
func LoadToml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("toml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
// LoadIni creates a Json object from given INI format content.
|
||||
func LoadIni(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("ini", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
// LoadYaml creates a Json object from given YAML format content.
|
||||
func LoadYaml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("yaml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
// LoadToml creates a Json object from given TOML format content.
|
||||
func LoadToml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("toml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
// doLoadContent creates a Json object from given content.
|
||||
// It supports data content type as follows:
|
||||
// JSON, XML, INI, YAML and TOML.
|
||||
func doLoadContent(dataType string, data []byte, safe ...bool) (*Json, error) {
|
||||
var err error
|
||||
var result interface{}
|
||||
@ -214,6 +182,9 @@ func doLoadContent(dataType string, data []byte, safe ...bool) (*Json, error) {
|
||||
return New(result, safe...), nil
|
||||
}
|
||||
|
||||
// LoadContent creates a Json object from given content, it checks the data type of <content>
|
||||
// automatically, supporting data content type as follows:
|
||||
// JSON, XML, INI, YAML and TOML.
|
||||
func LoadContent(data interface{}, safe ...bool) (*Json, error) {
|
||||
content := gconv.Bytes(data)
|
||||
if len(content) == 0 {
|
||||
|
||||
58
encoding/gjson/gjson_stdlib_json_util.go
Normal file
58
encoding/gjson/gjson_stdlib_json_util.go
Normal file
@ -0,0 +1,58 @@
|
||||
// Copyright 2019 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://github.com/gogf/gf.
|
||||
|
||||
package gjson
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// Valid checks whether <data> is a valid JSON data type.
|
||||
// The parameter <data> specifies the json format data, which can be either
|
||||
// bytes or string type.
|
||||
func Valid(data interface{}) bool {
|
||||
return json.Valid(gconv.Bytes(data))
|
||||
}
|
||||
|
||||
// Encode encodes any golang variable <value> to JSON bytes.
|
||||
func Encode(value interface{}) ([]byte, error) {
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
// Decode decodes json format <data> to golang variable.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
func Decode(data interface{}) (interface{}, error) {
|
||||
var value interface{}
|
||||
if err := DecodeTo(gconv.Bytes(data), &value); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Decode decodes json format <data> to specified golang variable <v>.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
// The parameter <v> should be a pointer type.
|
||||
func DecodeTo(data interface{}, v interface{}) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
|
||||
// Do not use number, it converts float64 to json.Number type,
|
||||
// which actually a string type. It causes converting issue for other data formats,
|
||||
// for example: yaml.
|
||||
//decoder.UseNumber()
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
// DecodeToJson codes json format <data> to a Json object.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
func DecodeToJson(data interface{}, safe ...bool) (*Json, error) {
|
||||
if v, err := Decode(gconv.Bytes(data)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return New(v, safe...), nil
|
||||
}
|
||||
}
|
||||
@ -10,22 +10,23 @@ import (
|
||||
"github.com/gogf/gf/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.
|
||||
// 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 parameter <safe> specifies whether using this Json object in concurrent-safe context, which is
|
||||
// false in default.
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context, which
|
||||
// is false in default.
|
||||
func New(data interface{}, safe ...bool) *Parser {
|
||||
return gjson.New(data, safe...)
|
||||
}
|
||||
|
||||
// NewWithTag 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.
|
||||
// NewWithTag 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 parameter <tags> specifies priority tags for struct conversion to map, multiple tags joined with char ','.
|
||||
// The parameter <tags> specifies priority tags for struct conversion to map, multiple tags joined
|
||||
// with char ','.
|
||||
//
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context, which is
|
||||
// false in default.
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context, which
|
||||
// is false in default.
|
||||
func NewWithTag(data interface{}, tags string, safe ...bool) *Parser {
|
||||
return gjson.NewWithTag(data, tags, safe...)
|
||||
}
|
||||
@ -38,7 +39,7 @@ func Load(path string, safe ...bool) (*Parser, error) {
|
||||
|
||||
// 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.
|
||||
// supporting JSON, XML, INI, YAML and TOML types of data.
|
||||
func LoadContent(data interface{}, safe ...bool) (*Parser, error) {
|
||||
return gjson.LoadContent(data, safe...)
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
// Context retrieves and returns the request's context.
|
||||
// This function overwrites the http.Request.Context function.
|
||||
func (r *Request) Context() context.Context {
|
||||
if r.context == nil {
|
||||
r.context = r.Request.Context()
|
||||
|
||||
@ -268,7 +268,7 @@ func MapStrStrDeep(value interface{}, tags ...string) map[string]string {
|
||||
}
|
||||
|
||||
// MapToMap converts map type variable <params> to another map type variable <pointer> using reflect.
|
||||
// The elements of <pointer> should be type of *map.
|
||||
// The parameter of <pointer> should be type of *map.
|
||||
func MapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMap(params, pointer, false, mapping...)
|
||||
}
|
||||
@ -327,19 +327,19 @@ func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...m
|
||||
}
|
||||
|
||||
// MapToMaps converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []map/*map.
|
||||
// The parameter of <pointer> should be type of []map/*map.
|
||||
func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMaps(params, pointer, false, mapping...)
|
||||
}
|
||||
|
||||
// MapToMapsDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []map/*map.
|
||||
// The parameter of <pointer> should be type of []map/*map.
|
||||
func MapToMapsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMaps(params, pointer, true, mapping...)
|
||||
}
|
||||
|
||||
// doMapToMaps converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []map/*map.
|
||||
// The parameter of <pointer> should be type of []map/*map.
|
||||
func doMapToMaps(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) error {
|
||||
paramsRv := reflect.ValueOf(params)
|
||||
paramsKind := paramsRv.Kind()
|
||||
|
||||
Reference in New Issue
Block a user