gjson/object.go

141 lines
3.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package gjson
import (
"encoding/json"
)
// JSONObject JSON Object 继承自JSONBaseObject
type JSONObject struct {
data map[string]interface{}
}
// NewJSONObject 创建一个新的JSONObject
func NewJSONObject() *JSONObject {
return &JSONObject{
data: make(map[string]interface{}),
}
}
// FromJSON 从JSON字符串创建JSONObject
func (jo *JSONObject) FromJSON(jsonStr string) error {
var data map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
return err
}
jo.data = data
return nil
}
// ToJSON 将JSONObject转换为JSON字符串
func (jo *JSONObject) ToJSON() (string, error) {
bytes, err := json.Marshal(jo.data)
if err != nil {
return "", err
}
return string(bytes), nil
}
// FromString 从字符串创建JSONObject功能同FromJSON
func (jo *JSONObject) FromString(str string) error {
return jo.FromJSON(str)
}
// ToString 将JSONObject转换为字符串功能同ToJSON
func (jo *JSONObject) ToString() (string, error) {
return jo.ToJSON()
}
// Get 获取指定键的值
func (jo *JSONObject) Get(key string) (interface{}, bool) {
value, exists := jo.data[key]
return value, exists
}
// Put 设置指定键的值如果是JSONBaseObject则提取其值
func (jo *JSONObject) Put(key string, value interface{}) {
switch v := value.(type) {
case *JSONObject:
jo.data[key] = v.data
case *JSONArray:
jo.data[key] = v.data
case JSONBaseObject:
// 如果是JSONBaseObject转换为实际值
jo.data[key] = extractValueFromJSONBaseObject(v)
default:
jo.data[key] = v
}
}
// Remove 删除指定键
func (jo *JSONObject) Remove(key string) {
delete(jo.data, key)
}
// Keys 获取所有键
func (jo *JSONObject) Keys() []string {
keys := make([]string, 0, len(jo.data))
for k := range jo.data {
keys = append(keys, k)
}
return keys
}
// Size 获取对象大小
func (jo *JSONObject) Size() int {
return len(jo.data)
}
// IsEmpty 检查是否为空
func (jo *JSONObject) IsEmpty() bool {
return len(jo.data) == 0
}
// ContainsKey 检查是否包含指定键
func (jo *JSONObject) ContainsKey(key string) bool {
_, exists := jo.data[key]
return exists
}
// Entries 获取所有键值对
func (jo *JSONObject) Entries() map[string]interface{} {
result := make(map[string]interface{})
for k, v := range jo.data {
result[k] = v
}
return result
}
// Values 获取所有值
func (jo *JSONObject) Values() []interface{} {
values := make([]interface{}, 0, len(jo.data))
for _, v := range jo.data {
values = append(values, v)
}
return values
}
// GetData 获取内部数据
func (jo *JSONObject) GetData() map[string]interface{} {
return jo.data
}
// extractValueFromJSONBaseObject 从JSONBaseObject中提取原始值
func extractValueFromJSONBaseObject(obj JSONBaseObject) interface{} {
switch v := obj.(type) {
case *JSONString:
return v.Value()
case *JSONNumber:
return v.Value()
case *JSONBool:
return v.Value()
case *JSONNull:
return v.Value()
case *JSONObject:
return v.data
case *JSONArray:
return v.data
default:
return v
}
}