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
|
|
|
)
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// New creates a Json object with any variable type of `data`, but `data` should be a map
|
2020-03-21 13:32:43 +08:00
|
|
|
// or slice for data access reason, or it will make no sense.
|
2020-02-08 14:07:32 +08:00
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `safe` specifies whether using this Json object in concurrent-safe context,
|
2020-03-21 13:32:43 +08:00
|
|
|
// which is false in default.
|
2019-07-23 23:20:27 +08:00
|
|
|
func New(data interface{}, safe ...bool) *Json {
|
2022-09-26 22:11:13 +08:00
|
|
|
return NewWithTag(data, string(ContentTypeJson), safe...)
|
2020-02-08 14:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// NewWithTag creates a Json object with any variable type of `data`, but `data` should be a map
|
2020-03-21 13:32:43 +08:00
|
|
|
// or slice for data access reason, or it will make no sense.
|
2020-02-08 14:07:32 +08:00
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `tags` specifies priority tags for struct conversion to map, multiple tags joined
|
2020-03-21 13:32:43 +08:00
|
|
|
// with char ','.
|
2020-02-08 14:07:32 +08:00
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `safe` specifies whether using this Json object in concurrent-safe context, which
|
2020-03-21 13:32:43 +08:00
|
|
|
// is false in default.
|
2020-02-08 14:07:32 +08:00
|
|
|
func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
2021-05-15 22:38:07 +08:00
|
|
|
option := Options{
|
2021-01-19 14:26:17 +08:00
|
|
|
Tags: tags,
|
|
|
|
|
}
|
|
|
|
|
if len(safe) > 0 && safe[0] {
|
|
|
|
|
option.Safe = true
|
|
|
|
|
}
|
2021-05-15 22:38:07 +08:00
|
|
|
return NewWithOptions(data, option)
|
2021-01-19 14:26:17 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +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.
|
2021-05-15 22:38:07 +08:00
|
|
|
func NewWithOptions(data interface{}, options Options) *Json {
|
2021-01-19 14:26:17 +08:00
|
|
|
var j *Json
|
2024-09-28 18:10:00 +08:00
|
|
|
switch result := data.(type) {
|
|
|
|
|
case []byte:
|
|
|
|
|
if r, err := loadContentWithOptions(result, options); err == nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
j = r
|
2024-09-28 18:10:00 +08:00
|
|
|
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
|
|
|
)
|
2021-10-28 23:18:23 +08:00
|
|
|
switch reflectInfo.OriginKind {
|
2019-09-30 17:23:23 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2022-10-08 11:46:38 +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
|
|
|
}
|