2023-03-10 14:42:15 +08:00
|
|
|
|
//----------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
|
//
|
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
2023-03-12 22:52:37 +08:00
|
|
|
|
// 依赖于 json-iterator 或 encoding/json 的JSON字节数组序列化
|
2023-03-10 14:42:15 +08:00
|
|
|
|
package json
|
|
|
|
|
|
|
|
|
|
import (
|
2023-03-10 15:46:07 +08:00
|
|
|
|
"encoding/json"
|
2023-03-10 14:42:15 +08:00
|
|
|
|
"github.com/energye/energy/common"
|
|
|
|
|
. "github.com/energye/energy/consts"
|
2023-03-24 09:24:03 +08:00
|
|
|
|
jsoniter "github.com/json-iterator/go" //json-iterator or encoding/json
|
2023-03-10 14:42:15 +08:00
|
|
|
|
"reflect"
|
|
|
|
|
"strconv"
|
2023-03-10 15:46:07 +08:00
|
|
|
|
"strings"
|
2023-03-10 14:42:15 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-03-12 22:52:37 +08:00
|
|
|
|
// BaseJSON JSON基础对象
|
|
|
|
|
//
|
|
|
|
|
// 当前对象直接操作
|
2023-03-10 15:10:40 +08:00
|
|
|
|
type BaseJSON interface {
|
2023-03-12 22:52:37 +08:00
|
|
|
|
Size() int //返回数据数量
|
|
|
|
|
Type() GO_VALUE_TYPE //当前对象数量类型
|
|
|
|
|
Data() any //返回原始数据
|
2023-03-25 22:35:06 +08:00
|
|
|
|
JsonData() *JsonData //返回原始JsonData数据结构
|
2023-03-24 22:23:23 +08:00
|
|
|
|
SetValue(value any) //设置值
|
2023-03-12 22:52:37 +08:00
|
|
|
|
String() string //返回 string 类型值
|
2023-03-14 23:07:51 +08:00
|
|
|
|
Int() int //返回 int 类型值, 把所有数字类型都转换 int 返回
|
2023-03-24 22:23:23 +08:00
|
|
|
|
Int64() int64 //返回 uint 类型值, 把所有数字类型都转换 int64 返回
|
2023-03-14 23:07:51 +08:00
|
|
|
|
UInt() uint //返回 uint 类型值, 把所有数字类型都转换 uint 返回
|
2023-03-24 22:23:23 +08:00
|
|
|
|
UInt64() uint64 //返回 uint 类型值, 把所有数字类型都转换 uint64 返回
|
2023-03-14 23:07:51 +08:00
|
|
|
|
Bytes() []byte //转换为 '[]byte' 并返回, 任意类型都将转换
|
|
|
|
|
Float() float64 //返回 float64 类型值 把所有数字类型都转换 float64 返回
|
2023-03-12 22:52:37 +08:00
|
|
|
|
Bool() bool //返回 bool 类型值
|
|
|
|
|
JSONObject() JSONObject //返回 JSONObject 对象类型
|
|
|
|
|
JSONArray() JSONArray //返回 JSONArray 对象类型
|
|
|
|
|
ToJSONString() string //转换为JSON字符串并返回
|
|
|
|
|
IsString() bool //当前对象是否为 string
|
|
|
|
|
IsInt() bool //当前对象是否为 int
|
|
|
|
|
IsUInt() bool //当前对象是否为 uint
|
|
|
|
|
IsBytes() bool //当前对象是否为 []byte
|
|
|
|
|
IsFloat() bool //当前对象是否为 float64
|
|
|
|
|
IsBool() bool //当前对象是否为 bool
|
|
|
|
|
IsObject() bool //当前对象是否为 JSONObject
|
|
|
|
|
IsArray() bool //当前对象是否为 JSONArray
|
|
|
|
|
Clear() //清空所有数据,保留原始数据类型
|
|
|
|
|
Free() //释放数据空间,且类型失效,当前对象不可用
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:52:37 +08:00
|
|
|
|
// JSONArray 数组类型 index
|
|
|
|
|
//
|
2023-03-14 23:07:51 +08:00
|
|
|
|
// 根据下标操作数据,失败返回数据类型的默认值
|
2023-03-10 15:10:40 +08:00
|
|
|
|
type JSONArray interface {
|
|
|
|
|
BaseJSON
|
2023-03-12 22:52:37 +08:00
|
|
|
|
Add(value ...any) //添加任意类型数据
|
|
|
|
|
SetByIndex(index int, value any) //设置指定下标位置任意类型数据
|
|
|
|
|
RemoveByIndex(index int) //删除指定下标数据
|
|
|
|
|
GetStringByIndex(index int) string //根据下标返回 string 类型值
|
|
|
|
|
GetIntByIndex(index int) int //根据下标返回 int 类型值
|
2023-03-23 17:33:33 +08:00
|
|
|
|
GetInt64ByIndex(index int) int64 //根据下标返回 int64 类型值
|
2023-03-12 22:52:37 +08:00
|
|
|
|
GetUIntByIndex(index int) uint //根据下标返回 uint 类型值
|
2023-03-23 17:33:33 +08:00
|
|
|
|
GetUInt64ByIndex(index int) uint64 //根据下标返回 uint64 类型值
|
2023-03-12 22:52:37 +08:00
|
|
|
|
GetBytesByIndex(index int) []byte //根据下标返回 []byte 类型值
|
|
|
|
|
GetFloatByIndex(index int) float64 //根据下标返回 float64 类型值
|
|
|
|
|
GetBoolByIndex(index int) bool //根据下标返回 bool 类型值
|
|
|
|
|
GetArrayByIndex(index int) JSONArray //根据下标返回 JSONArray 类型值
|
|
|
|
|
GetObjectByIndex(index int) JSONObject //根据下标返回 JSONObject 类型值
|
|
|
|
|
GetByIndex(index int) JSON //根据下标返回 JSON 类型值
|
2023-03-10 15:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:52:37 +08:00
|
|
|
|
// JSONObject 对象类型 key=value
|
|
|
|
|
//
|
2023-03-14 23:07:51 +08:00
|
|
|
|
// 根据key操作数据,失败返回数据类型的默认值
|
2023-03-10 15:10:40 +08:00
|
|
|
|
type JSONObject interface {
|
|
|
|
|
BaseJSON
|
2023-03-28 13:42:02 +08:00
|
|
|
|
HasKey(key string) bool //key是否存在
|
2023-03-12 22:52:37 +08:00
|
|
|
|
Set(key string, value any) //设置或覆盖指定key,并设置新任意类型值
|
|
|
|
|
RemoveByKey(key string) //删除指定key数据
|
|
|
|
|
GetStringByKey(key string) string //根据 key 返回 string 类型值
|
|
|
|
|
GetIntByKey(key string) int //根据 key 返回 int 类型值
|
2023-03-23 17:33:33 +08:00
|
|
|
|
GetInt64ByKey(key string) int64 //根据 key 返回 int64 类型值
|
2023-03-12 22:52:37 +08:00
|
|
|
|
GetUIntByKey(key string) uint //根据 key 返回 uint 类型值
|
2023-03-23 17:33:33 +08:00
|
|
|
|
GetUInt64ByKey(key string) uint64 //根据 key 返回 uint64 类型值
|
2023-03-12 22:52:37 +08:00
|
|
|
|
GetBytesByKey(key string) []byte //根据 key 返回 []byte 类型值
|
|
|
|
|
GetFloatByKey(key string) float64 //根据 key 返回 float64 类型值
|
|
|
|
|
GetBoolByKey(key string) bool //根据 key 返回 bool 类型值
|
|
|
|
|
GetArrayByKey(key string) JSONArray //根据 key 返回 JSONArray 类型值
|
|
|
|
|
GetObjectByKey(key string) JSONObject //根据 key 返回 JSONObject 类型值
|
|
|
|
|
GetByKey(key string) JSON //根据 key 返回 JSON 类型值
|
|
|
|
|
Keys() []string //返回当前对象所有 key
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 15:10:40 +08:00
|
|
|
|
// JSON Object
|
|
|
|
|
type JSON interface {
|
|
|
|
|
JSONArray
|
|
|
|
|
JSONObject
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
// JsonData JSON 数据结构
|
|
|
|
|
type JsonData struct {
|
2023-03-24 23:08:44 +08:00
|
|
|
|
T GO_VALUE_TYPE // type
|
|
|
|
|
S int // size
|
|
|
|
|
V any // value
|
2023-03-27 17:14:44 +08:00
|
|
|
|
pKey string // object parent key
|
|
|
|
|
pIndex int // array parent index
|
2023-03-25 22:35:06 +08:00
|
|
|
|
p *JsonData // parent
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
// NewJSON 返回 JsonData 对象,JSONArray or JSONObject
|
2023-03-24 09:33:36 +08:00
|
|
|
|
//
|
|
|
|
|
// []byte("{...}")
|
|
|
|
|
// []byte("[...]")
|
2023-03-10 15:10:40 +08:00
|
|
|
|
func NewJSON(data []byte) JSON {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if data == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var v any
|
|
|
|
|
if err := jsoniter.Unmarshal(data, &v); err == nil {
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
|
switch rv.Kind() {
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
if v, ok := v.([]any); ok {
|
2023-03-24 22:23:23 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE, V: v, S: len(v)}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case reflect.Map:
|
|
|
|
|
if v, ok := v.(map[string]any); ok {
|
2023-03-24 22:23:23 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-20 21:35:21 +08:00
|
|
|
|
} else {
|
|
|
|
|
println("NewJSON error:", err.Error())
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 15:10:40 +08:00
|
|
|
|
// NewJSONArray 字节JSONArray / 数组 / 切片 转换
|
2023-03-24 09:33:36 +08:00
|
|
|
|
//
|
|
|
|
|
// []byte("[...]")
|
|
|
|
|
// []slice
|
2023-03-10 15:10:40 +08:00
|
|
|
|
func NewJSONArray(value any) JSONArray {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if value != nil {
|
|
|
|
|
// 如果 []byte 就必须是 字节JSONArray
|
|
|
|
|
switch value.(type) {
|
|
|
|
|
case []byte:
|
|
|
|
|
if v := NewJSON(value.([]byte)); v != nil {
|
2023-03-11 15:30:10 +08:00
|
|
|
|
return v.JSONArray()
|
2023-03-10 14:42:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-20 21:35:21 +08:00
|
|
|
|
case string:
|
|
|
|
|
if v := NewJSON([]byte(value.(string))); v != nil {
|
|
|
|
|
return v.JSONArray()
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
rv := reflect.ValueOf(value)
|
2023-03-20 15:00:49 +08:00
|
|
|
|
kind := rv.Kind()
|
|
|
|
|
if kind == reflect.Ptr {
|
|
|
|
|
kind = rv.Elem().Kind()
|
|
|
|
|
}
|
|
|
|
|
if kind != reflect.Slice && kind != reflect.Array {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-20 21:35:21 +08:00
|
|
|
|
//转为[]any类型
|
|
|
|
|
if byt, err := jsoniter.Marshal(value); err == nil {
|
|
|
|
|
var v []any
|
|
|
|
|
if err = jsoniter.Unmarshal(byt, &v); err == nil {
|
2023-03-24 22:23:23 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE, V: v, S: len(v)}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-24 22:23:23 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE, V: make([]any, 0), S: 0}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-11 15:30:10 +08:00
|
|
|
|
// NewJSONObject 字节JSONObject / 结构 / JSONObject 转换
|
2023-03-24 09:33:36 +08:00
|
|
|
|
//
|
|
|
|
|
// []byte("{...}")
|
|
|
|
|
// struct
|
|
|
|
|
// map[string][type]
|
2023-03-10 15:10:40 +08:00
|
|
|
|
func NewJSONObject(value any) JSONObject {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if value != nil {
|
|
|
|
|
switch value.(type) {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
// 如果 []byte 就必须是 字节JSONObject
|
2023-03-10 14:42:15 +08:00
|
|
|
|
case []byte:
|
|
|
|
|
if v := NewJSON(value.([]byte)); v != nil {
|
2023-03-11 15:30:10 +08:00
|
|
|
|
return v.JSONObject()
|
2023-03-10 14:42:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-25 22:35:06 +08:00
|
|
|
|
// 如果 string 就必须是 字符串JSONObject
|
2023-03-20 21:35:21 +08:00
|
|
|
|
case string:
|
|
|
|
|
if v := NewJSON([]byte(value.(string))); v != nil {
|
|
|
|
|
return v.JSONObject()
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
rv := reflect.ValueOf(value)
|
|
|
|
|
kind := rv.Kind()
|
2023-03-20 15:00:49 +08:00
|
|
|
|
if kind == reflect.Ptr {
|
|
|
|
|
kind = rv.Elem().Kind()
|
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if kind != reflect.Map && kind != reflect.Struct {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-20 21:35:21 +08:00
|
|
|
|
//转为map[string]any类型
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if byt, err := jsoniter.Marshal(value); err == nil {
|
|
|
|
|
var v map[string]any
|
|
|
|
|
if err = jsoniter.Unmarshal(byt, &v); err == nil {
|
2023-03-24 22:23:23 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-24 22:23:23 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_MAP, V: make(map[string]any), S: 0}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Size() int {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.S
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Type() GO_VALUE_TYPE {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Data() any {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.V
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-25 22:35:06 +08:00
|
|
|
|
func (m *JsonData) JsonData() *JsonData {
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Add(value ...any) {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() {
|
2023-03-11 15:36:44 +08:00
|
|
|
|
tmp := make([]any, len(value))
|
|
|
|
|
for i, v := range value {
|
|
|
|
|
switch v.(type) {
|
2023-03-17 14:43:31 +08:00
|
|
|
|
case []byte:
|
|
|
|
|
if vv := NewJSON(v.([]byte)); v != nil {
|
|
|
|
|
tmp[i] = vv.Data()
|
|
|
|
|
} else {
|
|
|
|
|
tmp[i] = value
|
|
|
|
|
}
|
2023-03-11 15:36:44 +08:00
|
|
|
|
case JSON:
|
|
|
|
|
tmp[i] = v.(JSON).Data()
|
|
|
|
|
case JSONObject:
|
|
|
|
|
tmp[i] = v.(JSONObject).Data()
|
|
|
|
|
case JSONArray:
|
|
|
|
|
tmp[i] = v.(JSONArray).Data()
|
|
|
|
|
default:
|
|
|
|
|
tmp[i] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m.V = append(m.V.([]any), tmp...)
|
2023-03-11 15:30:10 +08:00
|
|
|
|
m.S += len(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) SetByIndex(index int, value any) {
|
2023-03-11 15:30:10 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
switch value.(type) {
|
2023-03-17 14:43:31 +08:00
|
|
|
|
case []byte:
|
|
|
|
|
if vv := NewJSON(value.([]byte)); vv != nil {
|
|
|
|
|
m.V.([]any)[index] = vv.Data()
|
|
|
|
|
} else {
|
|
|
|
|
m.V.([]any)[index] = value
|
|
|
|
|
}
|
2023-03-11 15:30:10 +08:00
|
|
|
|
case JSON:
|
|
|
|
|
m.V.([]any)[index] = value.(JSON).Data()
|
|
|
|
|
case JSONObject:
|
|
|
|
|
m.V.([]any)[index] = value.(JSONObject).Data()
|
|
|
|
|
case JSONArray:
|
|
|
|
|
m.V.([]any)[index] = value.(JSONArray).Data()
|
|
|
|
|
default:
|
|
|
|
|
m.V.([]any)[index] = value
|
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) RemoveByIndex(index int) {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index >= 0 && index < m.S {
|
|
|
|
|
v := m.V.([]any)
|
|
|
|
|
m.V = append(v[:index], v[index+1:]...)
|
|
|
|
|
m.S--
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetStringByIndex(index int) string {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
if r, ok := m.V.([]any)[index].(string); ok {
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetIntByIndex(index int) int {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
return m.toInt(m.V.([]any)[index])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetInt64ByIndex(index int) int64 {
|
2023-03-23 17:33:33 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
return m.toInt64(m.V.([]any)[index])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetUIntByIndex(index int) uint {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
return m.toUInt(m.V.([]any)[index])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetUInt64ByIndex(index int) uint64 {
|
2023-03-23 17:33:33 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
return m.toUInt64(m.V.([]any)[index])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetBytesByIndex(index int) []byte {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
return m.toBytes(m.V.([]any)[index])
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetFloatByIndex(index int) float64 {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
return m.toFloat64(m.V.([]any)[index])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetBoolByIndex(index int) bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
s := m.V.([]any)[index]
|
|
|
|
|
switch s.(type) {
|
|
|
|
|
case bool:
|
|
|
|
|
return s.(bool)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetArrayByIndex(index int) JSONArray {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
if v, ok := m.V.([]any)[index].([]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE, V: v, S: len(v), p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetObjectByIndex(index int) JSONObject {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
|
|
|
|
if v, ok := m.V.([]any)[index].(map[string]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_MAP, V: v, S: len(v), p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetByIndex(index int) JSON {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() && index < m.S {
|
2023-03-24 22:23:23 +08:00
|
|
|
|
value := m.V.([]any)[index]
|
|
|
|
|
switch value.(type) {
|
2023-03-10 15:46:07 +08:00
|
|
|
|
case json.Number:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v, err := value.(json.Number).Int64(); err == nil {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_INT, V: v, S: strconv.IntSize, p: m, pIndex: index}
|
2023-03-10 15:46:07 +08:00
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
case string:
|
2023-03-24 23:08:44 +08:00
|
|
|
|
v := value.(string)
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_STRING, V: v, S: len(v), p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
case int, int8, int16, int32, int64:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v := m.GetIntByIndex(index); v != 0 {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_INT, V: v, S: strconv.IntSize, p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case uint, uint8, uint16, uint32, uint64:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v := m.GetUIntByIndex(index); v != 0 {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_UINT, V: v, S: strconv.IntSize, p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case []byte:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v := m.GetBytesByIndex(index); v != nil {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE_BYTE, V: v, S: len(v), p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case float32, float64:
|
2023-03-10 15:46:07 +08:00
|
|
|
|
//不带有 . 转为 int 类型
|
2023-03-24 22:23:23 +08:00
|
|
|
|
v := m.toFloat64(value)
|
|
|
|
|
if strings.Index(strconv.FormatFloat(v, 'G', -1, 64), ".") != -1 {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_FLOAT64, V: v, S: 8, p: m, pIndex: index}
|
2023-03-10 15:46:07 +08:00
|
|
|
|
} else {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_INT, V: int(v), S: strconv.IntSize, p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case bool:
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_BOOL, V: m.GetBoolByIndex(index), S: 1, p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
case []any:
|
|
|
|
|
if index < m.S {
|
|
|
|
|
if v, ok := m.V.([]any)[index].([]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE, V: v, S: len(v), p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case map[string]any:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v, ok := value.(map[string]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_MAP, V: v, S: len(v), p: m, pIndex: index}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 13:42:02 +08:00
|
|
|
|
func (m *JsonData) HasKey(key string) bool {
|
|
|
|
|
if m.IsObject() {
|
|
|
|
|
_, ok := m.V.(map[string]any)[key]
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Set(key string, value any) {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-11 15:36:44 +08:00
|
|
|
|
switch value.(type) {
|
2023-03-17 14:43:31 +08:00
|
|
|
|
case []byte:
|
|
|
|
|
if vv := NewJSON(value.([]byte)); vv != nil {
|
|
|
|
|
value = vv.Data()
|
|
|
|
|
}
|
2023-03-11 15:36:44 +08:00
|
|
|
|
case JSON:
|
|
|
|
|
value = value.(JSON).Data()
|
|
|
|
|
case JSONObject:
|
|
|
|
|
value = value.(JSONObject).Data()
|
|
|
|
|
case JSONArray:
|
|
|
|
|
value = value.(JSONArray).Data()
|
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
m.V.(map[string]any)[key] = value
|
|
|
|
|
m.S++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 23:08:44 +08:00
|
|
|
|
func (m *JsonData) modifyParentValue() {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
if m.p != nil {
|
|
|
|
|
if m.p.IsArray() {
|
|
|
|
|
m.p.SetByIndex(m.pIndex, m.V)
|
|
|
|
|
} else if m.p.IsObject() {
|
|
|
|
|
m.p.Set(m.pKey, m.V)
|
2023-03-24 23:08:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) SetValue(value any) {
|
|
|
|
|
switch value.(type) {
|
|
|
|
|
case json.Number:
|
|
|
|
|
if v, err := value.(json.Number).Int64(); err == nil {
|
|
|
|
|
m.T = GO_VALUE_INT
|
|
|
|
|
m.V = v
|
|
|
|
|
m.S = 8
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
}
|
|
|
|
|
case string:
|
2023-03-24 23:08:44 +08:00
|
|
|
|
v := value.(string)
|
|
|
|
|
m.T = GO_VALUE_STRING
|
|
|
|
|
m.V = v
|
|
|
|
|
m.S = len(v)
|
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
case int, int8, int16, int32, int64:
|
2023-03-24 23:08:44 +08:00
|
|
|
|
v := m.toInt(value)
|
|
|
|
|
m.T = GO_VALUE_INT
|
|
|
|
|
m.V = v
|
|
|
|
|
m.S = strconv.IntSize
|
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
case uint, uint8, uint16, uint32, uint64:
|
2023-03-24 23:08:44 +08:00
|
|
|
|
v := m.toUInt(value)
|
|
|
|
|
m.T = GO_VALUE_UINT
|
|
|
|
|
m.V = v
|
|
|
|
|
m.S = strconv.IntSize
|
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
case []byte:
|
|
|
|
|
m.T = GO_VALUE_SLICE_BYTE
|
|
|
|
|
m.V = value.([]byte)
|
|
|
|
|
m.S = len(value.([]byte))
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
case float32, float64:
|
|
|
|
|
sv := m.toFloat64(value)
|
|
|
|
|
m.T = GO_VALUE_FLOAT64
|
|
|
|
|
m.V = sv
|
|
|
|
|
m.S = 8
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
case bool:
|
|
|
|
|
m.T = GO_VALUE_BOOL
|
|
|
|
|
m.V = value
|
|
|
|
|
m.S = 1
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
case []any:
|
|
|
|
|
m.T = GO_VALUE_SLICE
|
|
|
|
|
m.V = value
|
|
|
|
|
m.S = len(value.([]any))
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
case map[string]any:
|
|
|
|
|
m.T = GO_VALUE_MAP
|
|
|
|
|
m.V = value
|
|
|
|
|
m.S = len(value.(map[string]any))
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
default:
|
|
|
|
|
if v := NewJSONArray(value); v != nil {
|
|
|
|
|
m.T = v.Type()
|
|
|
|
|
m.V = v.Data()
|
|
|
|
|
m.S = v.Size()
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
} else if v := NewJSONObject(value); v != nil {
|
|
|
|
|
m.T = v.Type()
|
|
|
|
|
m.V = v.Data()
|
|
|
|
|
m.S = v.Size()
|
2023-03-24 23:08:44 +08:00
|
|
|
|
m.modifyParentValue()
|
2023-03-24 22:23:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *JsonData) RemoveByKey(key string) {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if _, ok := m.V.(map[string]any)[key]; ok {
|
|
|
|
|
delete(m.V.(map[string]any), key)
|
|
|
|
|
m.S--
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetStringByKey(key string) string {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if r, ok := m.V.(map[string]any)[key].(string); ok {
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetIntByKey(key string) int {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.toInt(m.V.(map[string]any)[key])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetInt64ByKey(key string) int64 {
|
2023-03-23 17:33:33 +08:00
|
|
|
|
if m.IsObject() {
|
|
|
|
|
return m.toInt64(m.V.(map[string]any)[key])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetUIntByKey(key string) uint {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.toUInt(m.V.(map[string]any)[key])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetUInt64ByKey(key string) uint64 {
|
2023-03-23 17:33:33 +08:00
|
|
|
|
if m.IsObject() {
|
|
|
|
|
return m.toUInt64(m.V.(map[string]any)[key])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetBytesByKey(key string) []byte {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.toBytes(m.V.(map[string]any)[key])
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetFloatByKey(key string) float64 {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.toFloat64(m.V.(map[string]any)[key])
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetBoolByKey(key string) bool {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if s, ok := m.V.(map[string]any)[key].(bool); ok {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetArrayByKey(key string) JSONArray {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if v, ok := m.V.(map[string]any)[key].([]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE, V: v, S: len(v), p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetObjectByKey(key string) JSONObject {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if v, ok := m.V.(map[string]any)[key].(map[string]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_MAP, V: v, S: len(v), p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) GetByKey(key string) JSON {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-28 13:42:02 +08:00
|
|
|
|
value, ok := m.V.(map[string]any)[key]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-24 22:23:23 +08:00
|
|
|
|
switch value.(type) {
|
2023-03-10 15:46:07 +08:00
|
|
|
|
case json.Number:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v, err := value.(json.Number).Int64(); err == nil {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_INT, V: v, S: 8, p: m, pKey: key}
|
2023-03-10 15:46:07 +08:00
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
case string:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v, ok := value.(string); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_STRING, V: v, S: len(v), p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case int, int8, int16, int32, int64:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v := m.GetIntByKey(key); v != 0 {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_INT, V: v, S: strconv.IntSize, p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case uint, uint8, uint16, uint32, uint64:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v := m.GetUIntByKey(key); v != 0 {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_UINT, V: v, S: strconv.IntSize, p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case []byte:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v := m.GetBytesByKey(key); v != nil {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE_BYTE, V: v, S: len(v), p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case float32, float64:
|
2023-03-10 15:46:07 +08:00
|
|
|
|
//不带有 . 转为 int 类型
|
2023-03-24 22:23:23 +08:00
|
|
|
|
sv := m.toFloat64(value)
|
2023-03-10 15:46:07 +08:00
|
|
|
|
if strings.Index(strconv.FormatFloat(sv, 'G', -1, 64), ".") != -1 {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_FLOAT64, V: sv, S: 8, p: m, pKey: key}
|
2023-03-10 15:46:07 +08:00
|
|
|
|
} else {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_INT, V: int(sv), S: strconv.IntSize, p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case bool:
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_BOOL, V: m.GetBytesByKey(key), S: 1, p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
case []any:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v, ok := value.([]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_SLICE, V: v, S: len(v), p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
case map[string]any:
|
2023-03-24 22:23:23 +08:00
|
|
|
|
if v, ok := value.(map[string]any); ok {
|
2023-03-25 22:35:06 +08:00
|
|
|
|
return &JsonData{T: GO_VALUE_MAP, V: v, S: len(v), p: m, pKey: key}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) String() string {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsString() {
|
|
|
|
|
return m.V.(string)
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Int() int {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.toInt(m.V)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Int64() int64 {
|
|
|
|
|
return m.toInt64(m.V)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *JsonData) UInt() uint {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.toUInt(m.V)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) UInt64() uint64 {
|
|
|
|
|
return m.toUInt64(m.V)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *JsonData) Bytes() []byte {
|
2023-03-11 00:02:03 +08:00
|
|
|
|
return m.toBytes(m.V)
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Float() float64 {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.toFloat64(m.V)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Bool() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsBool() {
|
2023-03-13 10:50:45 +08:00
|
|
|
|
switch m.V.(type) {
|
|
|
|
|
case bool:
|
|
|
|
|
return m.V.(bool)
|
|
|
|
|
case []uint8:
|
|
|
|
|
if v := m.V.([]uint8); len(v) > 0 {
|
|
|
|
|
return v[0] != 0
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-10 14:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) JSONObject() JSONObject {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) JSONArray() JSONArray {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
if m.IsArray() {
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Keys() []string {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
var result []string
|
|
|
|
|
for key, _ := range m.V.(map[string]any) {
|
|
|
|
|
result = append(result, key)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) ToJSONString() string {
|
2023-03-11 15:30:10 +08:00
|
|
|
|
return string(m.Bytes())
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsString() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_STRING
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsInt() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_INT
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsUInt() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_UINT
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsBytes() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_SLICE_BYTE
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsFloat() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_FLOAT64
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsBool() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_BOOL
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsObject() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_MAP
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) IsArray() bool {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
return m.T == GO_VALUE_SLICE
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Clear() {
|
2023-03-10 15:10:40 +08:00
|
|
|
|
if m.IsObject() {
|
2023-03-12 21:02:35 +08:00
|
|
|
|
m.V = make(map[string]any, 0)
|
2023-03-10 15:10:40 +08:00
|
|
|
|
} else if m.IsArray() {
|
|
|
|
|
m.V = make([]any, 0)
|
|
|
|
|
} else {
|
|
|
|
|
m.V = nil
|
|
|
|
|
}
|
|
|
|
|
m.S = 0
|
2023-03-12 21:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) Free() {
|
2023-03-14 17:30:45 +08:00
|
|
|
|
if m == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-12 21:02:35 +08:00
|
|
|
|
m.V = nil
|
|
|
|
|
m.S = 0
|
2023-03-10 15:10:40 +08:00
|
|
|
|
m.T = GO_VALUE_INVALID
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) toBytes(s any) []byte {
|
2023-03-14 23:07:51 +08:00
|
|
|
|
switch s.(type) {
|
|
|
|
|
case []byte:
|
|
|
|
|
return s.([]byte)
|
|
|
|
|
case string:
|
|
|
|
|
return []byte(s.(string))
|
|
|
|
|
case bool:
|
|
|
|
|
return []byte{common.BoolToByte(s.(bool))}
|
|
|
|
|
case float32:
|
|
|
|
|
return common.Float32ToBytes(s.(float32))
|
|
|
|
|
case float64:
|
|
|
|
|
return common.Float64ToBytes(s.(float64))
|
|
|
|
|
case int:
|
|
|
|
|
return common.IntToBytes(s.(int))
|
|
|
|
|
case int8:
|
|
|
|
|
return common.Int8ToBytes(s.(int8))
|
|
|
|
|
case int16:
|
|
|
|
|
return common.Int16ToBytes(s.(int16))
|
|
|
|
|
case int32:
|
|
|
|
|
return common.Int32ToBytes(s.(int32))
|
|
|
|
|
case int64:
|
|
|
|
|
return common.Int64ToBytes(s.(int64))
|
|
|
|
|
case uint:
|
|
|
|
|
return common.UIntToBytes(s.(uint))
|
|
|
|
|
case uint8:
|
|
|
|
|
return common.UInt8ToBytes(s.(uint8))
|
|
|
|
|
case uint16:
|
|
|
|
|
return common.UInt16ToBytes(s.(uint16))
|
|
|
|
|
case uint32:
|
|
|
|
|
return common.UInt32ToBytes(s.(uint32))
|
|
|
|
|
case uint64:
|
|
|
|
|
return common.UInt64ToBytes(s.(uint64))
|
|
|
|
|
default: // slice or map or other
|
|
|
|
|
if r, err := jsoniter.Marshal(s); err == nil {
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) toFloat64(s any) float64 {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
switch s.(type) {
|
|
|
|
|
case float32:
|
|
|
|
|
return float64(s.(float32))
|
|
|
|
|
case float64:
|
|
|
|
|
return s.(float64)
|
|
|
|
|
case int:
|
|
|
|
|
return float64(s.(int))
|
|
|
|
|
case int8:
|
|
|
|
|
return float64(s.(int8))
|
|
|
|
|
case int16:
|
|
|
|
|
return float64(s.(int16))
|
|
|
|
|
case int32:
|
|
|
|
|
return float64(s.(int32))
|
|
|
|
|
case int64:
|
|
|
|
|
return float64(s.(int64))
|
|
|
|
|
case uint:
|
|
|
|
|
return float64(s.(uint))
|
|
|
|
|
case uint8:
|
|
|
|
|
return float64(s.(uint8))
|
|
|
|
|
case uint16:
|
|
|
|
|
return float64(s.(uint16))
|
|
|
|
|
case uint32:
|
|
|
|
|
return float64(s.(uint32))
|
|
|
|
|
case uint64:
|
|
|
|
|
return float64(s.(uint64))
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) toInt(s any) int {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
switch s.(type) {
|
|
|
|
|
case float32:
|
|
|
|
|
return int(s.(float32))
|
|
|
|
|
case float64:
|
|
|
|
|
return int(s.(float64))
|
|
|
|
|
case int:
|
|
|
|
|
return s.(int)
|
|
|
|
|
case int8:
|
|
|
|
|
return int(s.(int8))
|
|
|
|
|
case int16:
|
|
|
|
|
return int(s.(int16))
|
|
|
|
|
case int32:
|
|
|
|
|
return int(s.(int32))
|
|
|
|
|
case int64:
|
|
|
|
|
return int(s.(int64))
|
|
|
|
|
case uint:
|
|
|
|
|
return int(s.(uint))
|
|
|
|
|
case uint8:
|
|
|
|
|
return int(s.(uint8))
|
|
|
|
|
case uint16:
|
|
|
|
|
return int(s.(uint16))
|
|
|
|
|
case uint32:
|
|
|
|
|
return int(s.(uint32))
|
|
|
|
|
case uint64:
|
|
|
|
|
return int(s.(uint64))
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) toUInt(s any) uint {
|
2023-03-10 14:42:15 +08:00
|
|
|
|
switch s.(type) {
|
|
|
|
|
case float32:
|
|
|
|
|
return uint(s.(float32))
|
|
|
|
|
case float64:
|
|
|
|
|
return uint(s.(float64))
|
|
|
|
|
case int:
|
|
|
|
|
return uint(s.(int))
|
|
|
|
|
case int8:
|
|
|
|
|
return uint(s.(int8))
|
|
|
|
|
case int16:
|
|
|
|
|
return uint(s.(int16))
|
|
|
|
|
case int32:
|
|
|
|
|
return uint(s.(int32))
|
|
|
|
|
case int64:
|
|
|
|
|
return uint(s.(int64))
|
|
|
|
|
case uint:
|
|
|
|
|
return s.(uint)
|
|
|
|
|
case uint8:
|
|
|
|
|
return uint(s.(uint8))
|
|
|
|
|
case uint16:
|
|
|
|
|
return uint(s.(uint16))
|
|
|
|
|
case uint32:
|
|
|
|
|
return uint(s.(uint32))
|
|
|
|
|
case uint64:
|
|
|
|
|
return uint(s.(uint64))
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2023-03-23 17:33:33 +08:00
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) toUInt64(s any) uint64 {
|
2023-03-23 17:33:33 +08:00
|
|
|
|
switch s.(type) {
|
|
|
|
|
case float32:
|
|
|
|
|
return uint64(s.(float32))
|
|
|
|
|
case float64:
|
|
|
|
|
return uint64(s.(float64))
|
|
|
|
|
case int:
|
|
|
|
|
return uint64(s.(int))
|
|
|
|
|
case int8:
|
|
|
|
|
return uint64(s.(int8))
|
|
|
|
|
case int16:
|
|
|
|
|
return uint64(s.(int16))
|
|
|
|
|
case int32:
|
|
|
|
|
return uint64(s.(int32))
|
|
|
|
|
case int64:
|
|
|
|
|
return uint64(s.(int64))
|
|
|
|
|
case uint:
|
|
|
|
|
return uint64(s.(uint))
|
|
|
|
|
case uint8:
|
|
|
|
|
return uint64(s.(uint8))
|
|
|
|
|
case uint16:
|
|
|
|
|
return uint64(s.(uint16))
|
|
|
|
|
case uint32:
|
|
|
|
|
return uint64(s.(uint32))
|
|
|
|
|
case uint64:
|
|
|
|
|
return s.(uint64)
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 22:23:23 +08:00
|
|
|
|
func (m *JsonData) toInt64(s any) int64 {
|
2023-03-23 17:33:33 +08:00
|
|
|
|
switch s.(type) {
|
|
|
|
|
case float32:
|
|
|
|
|
return int64(s.(float32))
|
|
|
|
|
case float64:
|
|
|
|
|
return int64(s.(float64))
|
|
|
|
|
case int:
|
|
|
|
|
return int64(s.(int))
|
|
|
|
|
case int8:
|
|
|
|
|
return int64(s.(int8))
|
|
|
|
|
case int16:
|
|
|
|
|
return int64(s.(int16))
|
|
|
|
|
case int32:
|
|
|
|
|
return int64(s.(int32))
|
|
|
|
|
case int64:
|
|
|
|
|
return s.(int64)
|
|
|
|
|
case uint:
|
|
|
|
|
return int64(s.(uint))
|
|
|
|
|
case uint8:
|
|
|
|
|
return int64(s.(uint8))
|
|
|
|
|
case uint16:
|
|
|
|
|
return int64(s.(uint16))
|
|
|
|
|
case uint32:
|
|
|
|
|
return int64(s.(uint32))
|
|
|
|
|
case uint64:
|
|
|
|
|
return int64(s.(uint64))
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|