```
feat: 添加 LICENSE 和 README.md 文件 新增项目的许可证文件和详细说明文档,包含 MIT 许可证内容和 GJson 库的完整使用说明、API 文档以及快速开始示例。 ```
This commit is contained in:
parent
7ee90f3a18
commit
d8c339a39a
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023-present, kingecg
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
# GJson
|
||||
|
||||
GJson 是一个功能强大且易于使用的 Go 语言 JSON 处理库。它提供了简洁的 API 来处理 JSON 数据,包括序列化、反序列化和属性访问等功能。
|
||||
|
||||
## 特性
|
||||
|
||||
- **面向对象设计**:提供 JSONBaseObject 接口及其实现(JSONObject、JSONArray、JSONValue 等)
|
||||
- **灵活的属性访问**:支持通过路径访问嵌套 JSON 属性
|
||||
- **完整的 JSON 类型支持**:支持字符串、数字、布尔值、null 等 JSON 数据类型
|
||||
- **格式化输出**:提供类似 JavaScript 的 stringify 函数,支持格式化和紧凑两种输出方式
|
||||
- **类型安全**:强类型系统保证运行时安全性
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
go get git.kingecg.top/kingecg/gjson
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.kingecg.top/kingecg/gjson"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个 JSONObject
|
||||
obj := gjson.NewJSONObject()
|
||||
obj.Put("name", gjson.NewJSONString("张三").Value())
|
||||
obj.Put("age", gjson.NewJSONNumber(25).Value())
|
||||
|
||||
// 创建嵌套对象
|
||||
address := gjson.NewJSONObject()
|
||||
address.Put("city", gjson.NewJSONString("北京").Value())
|
||||
obj.Put("address", address.GetData())
|
||||
|
||||
// 序列化为 JSON 字符串
|
||||
jsonStr, _ := obj.ToJSON()
|
||||
fmt.Printf("JSON对象: %s\n", jsonStr)
|
||||
|
||||
// 使用 GetProperty 访问嵌套属性
|
||||
defaultValue := gjson.NewJSONString("default")
|
||||
city, _ := gjson.GetProperty(obj, "address.city", defaultValue)
|
||||
if str, ok := city.(*gjson.JSONString); ok {
|
||||
fmt.Printf("城市: %s\n", str.Value())
|
||||
}
|
||||
|
||||
// 使用 Stringify 函数
|
||||
stringified, _ := gjson.Stringify(obj, " ")
|
||||
fmt.Printf("格式化输出:\n%s\n", stringified)
|
||||
}
|
||||
```
|
||||
|
||||
## API 文档
|
||||
|
||||
### 基础对象
|
||||
|
||||
- `JSONBaseObject`:所有 JSON 对象的基础接口
|
||||
- `JSONObject`:表示 JSON 对象
|
||||
- `JSONArray`:表示 JSON 数组
|
||||
- `JSONValue`:表示简单值对象,包括 JSONString、JSONNumber、JSONBool、JSONNull
|
||||
|
||||
### 主要功能
|
||||
|
||||
#### 创建对象
|
||||
|
||||
- `NewJSONObject()`:创建新的 JSONObject 实例
|
||||
- `NewJSONArray()`:创建新的 JSONArray 实例
|
||||
- `NewJSONString(value)`:创建新的 JSONString 实例
|
||||
- `NewJSONNumber(value)`:创建新的 JSONNumber 实例
|
||||
- `NewJSONBool(value)`:创建新的 JSONBool 实例
|
||||
- `NewJSONNull()`:创建新的 JSONNull 实例
|
||||
|
||||
#### 序列化和反序列化
|
||||
|
||||
- `ToJSON()` / `FromJSON(jsonStr)`:转换为/从 JSON 字符串
|
||||
- `ToString()` / `FromString(str)`:转换为/从字符串
|
||||
|
||||
#### 属性操作
|
||||
|
||||
- `GetProperty(obj JSONBaseObject, propertyPath string, defaultValue JSONBaseObject)`:获取属性值
|
||||
- `SetProperty(obj JSONBaseObject, propertyPath string, value JSONBaseObject)`:设置属性值
|
||||
|
||||
#### 格式化输出
|
||||
|
||||
- `Stringify(obj JSONBaseObject, space ...string)`:类似 JavaScript 的 stringify 函数
|
||||
- `PrettyPrint(obj JSONBaseObject)`:格式化打印 JSON 对象
|
||||
- `Compact(obj JSONBaseObject)`:将 JSON 对象转换为紧凑格式
|
||||
|
||||
## 贡献
|
||||
|
||||
欢迎提交 Issue 和 Pull Request 来改进这个项目!
|
||||
|
||||
## 许可证
|
||||
|
||||
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件。
|
||||
Loading…
Reference in New Issue