Files
gf/encoding/gjson/gjson_api_new_load.go

99 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-05-08 22:04:36 +08:00
//
// 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 (
2019-08-30 20:29:12 +08:00
"reflect"
2022-03-11 10:24:42 +08:00
"github.com/gogf/gf/v2/internal/reflection"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/internal/rwmutex"
"github.com/gogf/gf/v2/util/gconv"
2019-05-08 22:04:36 +08:00
)
// 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.
func New(data interface{}, safe ...bool) *Json {
2022-09-26 22:11:13 +08:00
return NewWithTag(data, string(ContentTypeJson), 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.
//
// 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.
func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
option := Options{
2021-01-19 14:26:17 +08:00
Tags: tags,
}
if len(safe) > 0 && safe[0] {
option.Safe = true
}
return NewWithOptions(data, option)
2021-01-19 14:26:17 +08:00
}
// NewWithOptions creates a Json object with any variable type of `data`, but `data` should be a map
2021-01-19 14:26:17 +08:00
// or slice for data access reason, or it will make no sense.
func NewWithOptions(data interface{}, options Options) *Json {
2021-01-19 14:26:17 +08:00
var j *Json
switch result := data.(type) {
case []byte:
if r, err := loadContentWithOptions(result, options); err == nil {
2019-06-19 09:06:52 +08:00
j = r
break
}
j = &Json{
p: &data,
c: byte(defaultSplitChar),
vc: false,
}
case string:
if r, err := loadContentWithOptions([]byte(result), options); err == nil {
j = r
break
}
j = &Json{
p: &data,
c: byte(defaultSplitChar),
vc: false,
2019-06-19 09:06:52 +08:00
}
default:
2022-02-23 16:54:15 +08:00
var (
pointedData interface{}
2022-03-11 10:24:42 +08:00
reflectInfo = reflection.OriginValueAndKind(data)
2022-02-23 16:54:15 +08:00
)
switch reflectInfo.OriginKind {
case reflect.Slice, reflect.Array:
2022-02-23 16:54:15 +08:00
pointedData = gconv.Interfaces(data)
2022-01-15 22:15:10 +08:00
case reflect.Map:
2022-02-23 16:54:15 +08:00
pointedData = gconv.MapDeep(data, options.Tags)
2022-01-15 22:15:10 +08:00
case reflect.Struct:
if v, ok := data.(iVal); ok {
return NewWithOptions(v.Val(), options)
}
2022-02-23 16:54:15 +08:00
pointedData = gconv.MapDeep(data, options.Tags)
2019-06-19 09:06:52 +08:00
default:
2022-02-23 16:54:15 +08:00
pointedData = data
}
j = &Json{
p: &pointedData,
c: byte(defaultSplitChar),
vc: false,
2019-06-19 09:06:52 +08:00
}
}
j.mu = rwmutex.Create(options.Safe)
2019-06-19 09:06:52 +08:00
return j
2019-05-08 22:04:36 +08:00
}