zjson

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

zjson

一个简单、高效的 Go JSON 处理库。

主要特性

1. 并发安全性
  • JsonObjectJsonArray 添加了 sync.RWMutex 互斥锁
  • 所有读写操作都使用适当的锁保护
  • 写操作使用 Lock(),读操作使用 RLock()
  • 添加了并发安全性测试用例
2. 错误处理改进
  • 使用 fmt.Errorf 包装错误,提供更多上下文信息
  • 统一了错误消息格式
  • 添加了更详细的错误描述
  • 改进了错误处理测试用例
3. 类型转换优化
  • 使用 switch 语句替代多个 if 判断,提高代码可读性
  • 增加了对 int64 类型的支持
  • 改进了字符串到数字的转换逻辑
  • 添加了更多类型转换测试用例
4. 测试覆盖
  • 添加了边界条件测试
  • 添加了并发安全性测试
  • 添加了性能测试
  • 添加了错误处理测试
  • 添加了嵌套结构测试

使用示例

并发安全使用
var wg sync.WaitGroup
obj := zjson.NewJsonObject()

wg.Add(2)
go func() {
    defer wg.Done()
    for i := 0; i < 1000; i++ {
        obj.Put("key1", i)
    }
}()

go func() {
    defer wg.Done()
    for i := 0; i < 1000; i++ {
        obj.Put("key2", i)
    }
}()

wg.Wait()
改进的错误处理
obj := zjson.NewJsonObject()
obj.Put("age", "25")

age, err := obj.GetInt("age")
if err != nil {
    // 错误信息会包含更多上下文
    log.Printf("获取年龄失败: %v", err)
    return
}
类型转换
obj := zjson.NewJsonObject()
obj.Put("number", "123.45")

// 支持多种数字类型
val, err := obj.GetInt("number")
if err == nil {
    fmt.Printf("整数: %d\n", val)
}

floatVal, err := obj.GetFloat("number")
if err == nil {
    fmt.Printf("浮点数: %.2f\n", floatVal)
}

安装

go get github.com/codinglz/zjson

版本历史

v0.2.0
  • 添加了并发安全性支持
  • 改进了错误处理机制
  • 优化了类型转换逻辑
  • 添加了全面的测试用例
  • 改进了代码质量
v0.1.0
  • 初始版本发布
  • 基本的 JSON 对象和数组操作
  • 类型转换支持
  • 错误处理

贡献者

许可证

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetParser

func SetParser(parser JsonParser)

Types

type JsonArray

type JsonArray struct {
	// contains filtered or unexported fields
}

func NewJsonArray

func NewJsonArray() *JsonArray

func NewJsonArrayWithVal added in v0.3.0

func NewJsonArrayWithVal(v []any) *JsonArray

func ParseToArray

func ParseToArray(v any) (*JsonArray, error)

func (*JsonArray) Add

func (ja *JsonArray) Add(value any)

func (*JsonArray) AddAll added in v0.3.0

func (ja *JsonArray) AddAll(value []any)

func (*JsonArray) Get

func (ja *JsonArray) Get(index int) any

func (*JsonArray) GetFloat added in v0.1.2

func (ja *JsonArray) GetFloat(index int) (float64, error)

func (*JsonArray) GetFloatIgnoreError added in v0.1.2

func (ja *JsonArray) GetFloatIgnoreError(index int) float64

func (*JsonArray) GetInt

func (ja *JsonArray) GetInt(index int) (int, error)

func (*JsonArray) GetIntIgnoreError added in v0.1.2

func (ja *JsonArray) GetIntIgnoreError(index int) int

func (*JsonArray) GetJsonArray

func (ja *JsonArray) GetJsonArray(index int) (*JsonArray, error)

func (*JsonArray) GetJsonArrayIgnoreError added in v0.1.2

func (ja *JsonArray) GetJsonArrayIgnoreError(index int) *JsonArray

func (*JsonArray) GetJsonObject

func (ja *JsonArray) GetJsonObject(index int) (*JsonObject, error)

func (*JsonArray) GetJsonObjectIgnoreError added in v0.1.2

func (ja *JsonArray) GetJsonObjectIgnoreError(index int) *JsonObject

func (*JsonArray) GetString added in v0.1.2

func (ja *JsonArray) GetString(index int) (string, error)

func (*JsonArray) GetStringIgnoreError added in v0.1.2

func (ja *JsonArray) GetStringIgnoreError(index int) string

func (*JsonArray) Length

func (ja *JsonArray) Length() int

func (*JsonArray) Remove

func (ja *JsonArray) Remove(index int)

func (*JsonArray) ToJsonStr

func (ja *JsonArray) ToJsonStr() string

func (*JsonArray) ToStruct

func (ja *JsonArray) ToStruct(s any) error

func (*JsonArray) UnmarshalJSON added in v0.4.0

func (ja *JsonArray) UnmarshalJSON(data []byte) error

type JsonObject

type JsonObject struct {
	// contains filtered or unexported fields
}

func NewJsonObject

func NewJsonObject() *JsonObject

func NewJsonObjectWithVal added in v0.3.0

func NewJsonObjectWithVal(v map[string]any) *JsonObject

func ParseToJsonObject

func ParseToJsonObject(v any) (*JsonObject, error)

func (*JsonObject) ContainsKey

func (jo *JsonObject) ContainsKey(key string) bool

func (*JsonObject) ForEach added in v0.5.0

func (jo *JsonObject) ForEach(f func(key string))

func (*JsonObject) Get

func (jo *JsonObject) Get(key string) any

func (*JsonObject) GetBool

func (jo *JsonObject) GetBool(key string) (bool, error)

func (*JsonObject) GetBoolIgnoreError added in v0.1.2

func (jo *JsonObject) GetBoolIgnoreError(key string) bool

func (*JsonObject) GetFloat

func (jo *JsonObject) GetFloat(key string) (float64, error)

func (*JsonObject) GetFloatIgnoreError added in v0.1.2

func (jo *JsonObject) GetFloatIgnoreError(key string) float64

func (*JsonObject) GetInt

func (jo *JsonObject) GetInt(key string) (int, error)

func (*JsonObject) GetIntIgnoreError added in v0.1.2

func (jo *JsonObject) GetIntIgnoreError(key string) int

func (*JsonObject) GetJsonArray

func (jo *JsonObject) GetJsonArray(key string) (*JsonArray, error)

func (*JsonObject) GetJsonArrayIgnoreError added in v0.1.2

func (jo *JsonObject) GetJsonArrayIgnoreError(key string) *JsonArray

func (*JsonObject) GetJsonObject

func (jo *JsonObject) GetJsonObject(key string) (*JsonObject, error)

func (*JsonObject) GetJsonObjectIgnoreError added in v0.1.2

func (jo *JsonObject) GetJsonObjectIgnoreError(key string) *JsonObject

func (*JsonObject) GetString

func (jo *JsonObject) GetString(key string) (string, error)

func (*JsonObject) GetStringIgnoreError added in v0.1.2

func (jo *JsonObject) GetStringIgnoreError(key string) string

func (*JsonObject) Keys added in v0.5.0

func (jo *JsonObject) Keys() []string

func (*JsonObject) Length

func (jo *JsonObject) Length() int

func (*JsonObject) Put

func (jo *JsonObject) Put(key string, value any)

func (*JsonObject) PutAll added in v0.3.0

func (jo *JsonObject) PutAll(val map[string]any)

func (*JsonObject) Remove

func (jo *JsonObject) Remove(key string)

func (*JsonObject) ToJsonStr

func (jo *JsonObject) ToJsonStr() string

func (*JsonObject) ToStruct

func (jo *JsonObject) ToStruct(s any) error

func (*JsonObject) UnmarshalJSON added in v0.4.0

func (jo *JsonObject) UnmarshalJSON(data []byte) error

type JsonParser

type JsonParser interface {
	AnyToJsonString(v any) ([]byte, error)
	JsonStringToAny(jsonStr []byte, v any) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL