gjson/array.go

141 lines
3.3 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"
"strconv"
)
// JSONArray JSON Array 继承自JSONBaseObject代表JSON中的数组类型
type JSONArray struct {
data []interface{}
}
// NewJSONArray 创建一个新的JSONArray
func NewJSONArray() *JSONArray {
return &JSONArray{
data: make([]interface{}, 0),
}
}
// FromJSON 从JSON字符串创建JSONArray
func (ja *JSONArray) FromJSON(jsonStr string) error {
var data []interface{}
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
return err
}
ja.data = data
return nil
}
// ToJSON 将JSONArray转换为JSON字符串
func (ja *JSONArray) ToJSON() (string, error) {
bytes, err := json.Marshal(ja.data)
if err != nil {
return "", err
}
return string(bytes), nil
}
// FromString 从字符串创建JSONArray功能同FromJSON
func (ja *JSONArray) FromString(str string) error {
return ja.FromJSON(str)
}
// ToString 将JSONArray转换为字符串功能同ToJSON
func (ja *JSONArray) ToString() (string, error) {
return ja.ToJSON()
}
// Get 获取指定索引的元素
func (ja *JSONArray) Get(index int) (interface{}, bool) {
if index >= 0 && index < len(ja.data) {
return ja.data[index], true
}
return nil, false
}
// Put 在指定索引设置元素
func (ja *JSONArray) Put(index int, value interface{}) error {
if index < 0 || index >= len(ja.data) {
return &InvalidIndexError{index, len(ja.data)}
}
ja.data[index] = extractValueFromJSONBaseObjectForArray(value)
return nil
}
// Append 向数组末尾添加元素
func (ja *JSONArray) Append(value interface{}) {
ja.data = append(ja.data, extractValueFromJSONBaseObjectForArray(value))
}
// Insert 在指定位置插入元素
func (ja *JSONArray) Insert(index int, value interface{}) error {
if index < 0 || index > len(ja.data) {
return &InvalidIndexError{index, len(ja.data)}
}
ja.data = append(ja.data[:index], append([]interface{}{extractValueFromJSONBaseObjectForArray(value)}, ja.data[index:]...)...)
return nil
}
// Remove 删除指定索引的元素
func (ja *JSONArray) Remove(index int) error {
if index < 0 || index >= len(ja.data) {
return &InvalidIndexError{index, len(ja.data)}
}
ja.data = append(ja.data[:index], ja.data[index+1:]...)
return nil
}
// Size 获取数组大小
func (ja *JSONArray) Size() int {
return len(ja.data)
}
// IsEmpty 检查数组是否为空
func (ja *JSONArray) IsEmpty() bool {
return len(ja.data) == 0
}
// GetData 获取内部数据
func (ja *JSONArray) GetData() []interface{} {
return ja.data
}
// InvalidIndexError 索引越界错误
type InvalidIndexError struct {
index int
size int
}
func (e *InvalidIndexError) Error() string {
return "index out of bounds: index " + strconv.Itoa(e.index) + " with size " + strconv.Itoa(e.size)
}
// extractValueFromJSONBaseObjectForArray 从JSONBaseObject中提取原始值用于数组
func extractValueFromJSONBaseObjectForArray(obj interface{}) interface{} {
switch v := obj.(type) {
case JSONBaseObject:
switch t := v.(type) {
case *JSONString:
return t.Value()
case *JSONNumber:
return t.Value()
case *JSONBool:
return t.Value()
case *JSONNull:
return t.Value()
case *JSONObject:
return t.data
case *JSONArray:
return t.data
default:
return v
}
case *JSONObject:
return v.data
case *JSONArray:
return v.data
default:
return v
}
}