ajson

package
v1.8.7 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

antgo/encoding/ajson - JSON 数据处理库 / JSON Data Processing Library

中文 | English


中文

📖 简介

antgo/encoding/ajson 是一个高性能JSON数据处理库,提供JSON解析、编码、路径查询及类型安全转换等功能。
适用于配置管理、API数据处理、结构化数据转换等场景。

GitHub地址: github.com/small-ek/antgo/encoding/ajson

📦 安装
go get github.com/small-ek/antgo/encoding/ajson
🚀 快速开始
基本解析
package main

import (
	"fmt"
	"github.com/small-ek/antgo/encoding/ajson"
)

func main() {
	// 示例JSON内容
	jsonContent := `{
		"user": {
			"name": "John",
			"age": 30,
			"skills": ["Go", "Python"]
		},
		"server": {
			"host": "0.0.0.0",
			"ports": [8080, 8081]
		}
	}`

	// 解析JSON内容
	j := ajson.Decode([]byte(jsonContent))
	
	// 获取嵌套值
	name := j.Get("user.name").String()
	age := j.Get("user.age").Int()
	skill := j.Get("user.skills.0").String()
	
	fmt.Println(name)  // 输出: John
	fmt.Println(age)   // 输出: 30
	fmt.Println(skill) // 输出: Go
}
编码生成
func main() {
	// 构建数据
	data := map[string]interface{}{
		"database": map[string]interface{}{
			"host": "127.0.0.1",
			"port": 3306,
		},
	}

	// 编码为JSON字符串
	jsonStr := ajson.Encode(data)
	fmt.Println(jsonStr) // 输出: {"database":{"host":"127.0.0.1","port":3306}}
}
类型转换
func main() {
	jsonContent := `{"count": "100", "active": true}`

	j := ajson.Decode([]byte(jsonContent))
	
	// 自动类型转换
	count := j.Get("count").Int()     // 字符串转数字
	active := j.Get("active").Bool()  // 布尔值转换
	
	fmt.Println(count)  // 输出: 100
	fmt.Println(active) // 输出: true
}
✨ 核心特性
特性 描述
高效解析 基于标准库的高性能解析,支持复杂数据结构
链式操作 支持 Get("path.to.value").Int() 链式调用
类型安全 提供20+种安全类型转换方法(String/Int/Float64/Map/Array等)
零拷贝处理 解析过程最小化内存分配
并发安全 所有操作线程安全
⚠️ 注意事项
  1. 使用Get()方法时路径不存在会返回零值
  2. 类型转换失败时返回对应类型的零值(如字符串转数字失败返回0)
  3. 复杂JSON建议先进行Get()路径验证
  4. 解析失败会触发panic,生产环境建议配合recover使用
🤝 参与贡献

贡献指南 | 提交Issue


English

📖 Introduction

antgo/encoding/ajson is a high-performance JSON processing library providing parsing, encoding, path querying and type-safe conversions.
Ideal for configuration management, API data processing and structured data transformation.

GitHub URL: github.com/small-ek/antgo/encoding/ajson

📦 Installation
go get github.com/small-ek/antgo/encoding/ajson
🚀 Quick Start
Basic Parsing
package main

import (
	"fmt"
	"github.com/small-ek/antgo/encoding/ajson"
)

func main() {
	jsonContent := `{
		"user": {
			"name": "John",
			"age": 30,
			"skills": ["Go", "Python"]
		},
		"server": {
			"host": "0.0.0.0",
			"ports": [8080, 8081]
		}
	}`

	j := ajson.Decode([]byte(jsonContent))
	
	name := j.Get("user.name").String()
	age := j.Get("user.age").Int()
	skill := j.Get("user.skills.0").String()
	
	fmt.Println(name)  // Output: John
	fmt.Println(age)   // Output: 30
	fmt.Println(skill) // Output: Go
}
JSON Generation
func main() {
	data := map[string]interface{}{
		"database": map[string]interface{}{
			"host": "127.0.0.1",
			"port": 3306,
		},
	}

	jsonStr := ajson.Encode(data)
	fmt.Println(jsonStr) // Output: {"database":{"host":"127.0.0.1","port":3306}}
}
Type Conversion
func main() {
	jsonContent := `{"count": "100", "active": true}`

	j := ajson.Decode([]byte(jsonContent))
	
	count := j.Get("count").Int()     // String to int
	active := j.Get("active").Bool()  // Boolean conversion
	
	fmt.Println(count)  // Output: 100
	fmt.Println(active) // Output: true
}
✨ Key Features
Feature Description
High Performance Standard library-based parsing with complex data support
Chained API Method chaining like Get("path.to.value").Int()
Type Safety 20+ type conversion methods (String/Int/Float64/Map/Array etc.)
Zero-Copy Minimal memory allocation during parsing
Concurrency Safe All operations are thread-safe
⚠️ Important Notes
  1. Get() returns zero-value for non-existent paths
  2. Type conversion failures return type's zero-value
  3. Validate paths with Get() for complex JSON
  4. Parse errors trigger panic, use with recover in production
🤝 Contributing

Contribution Guide | Open an Issue

⬆ Back to Top

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(data interface{}) string

Encode converts data to JSON string Encode 将数据转换为JSON字符串

func Open

func Open(file string) []byte

Open reads a JSON file and returns its content as bytes Open 读取JSON文件并返回其内容的字节切片

Types

type Json

type Json struct {
	Child interface{} // Current level JSON data (当前层级的JSON数据)
}

Json is a wrapper for JSON data parsing and manipulation Json 是用于JSON数据解析和操作的封装结构

func Decode

func Decode(data []byte) *Json

Decode parses JSON bytes into Json structure Decode 将JSON字节解析为Json结构

func (*Json) Array

func (j *Json) Array() []interface{}

Array converts current value to []interface{} Array 将当前值转换为切片类型

func (*Json) Bool

func (j *Json) Bool() bool

Bool converts current value to boolean Bool 将当前值转换为布尔值

func (*Json) Float64

func (j *Json) Float64() float64

Float64 converts current value to float64 Float64 将当前值转换为64位浮点数

func (*Json) Get

func (j *Json) Get(path string) *Json

Get navigates through multi-level JSON path Get 通过多级路径导航JSON数据

func (*Json) Int

func (j *Json) Int() int

Int converts current value to int Int 将当前值转换为整型

func (*Json) Int64

func (j *Json) Int64() int64

Int64 converts current value to int64 Int64 将当前值转换为64位整型

func (*Json) Interface

func (j *Json) Interface() interface{}

Interface returns raw interface value Interface 返回原始接口值

func (*Json) Ints

func (j *Json) Ints() []int

Ints converts current value to int slice Ints 将当前值转换为整型切片

func (*Json) Map

func (j *Json) Map() map[string]interface{}

Map converts current value to map[string]interface{} Map 将当前值转换为字典类型

func (*Json) Next

func (j *Json) Next(name interface{}) *Json

Next navigates to next level JSON element Next 导航到下一级JSON元素

func (*Json) String

func (j *Json) String() string

String converts current value to string String 将当前值转换为字符串

func (*Json) Strings

func (j *Json) Strings() []string

Strings converts current value to string slice Strings 将当前值转换为字符串切片

Jump to

Keyboard shortcuts

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