zdpgo_json

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 3 Imported by: 1

README

zdpgo_json

在 Golang 中便捷从处理 Json 文件,比如动态配置,增删改查

项目地址:https://github.com/zhangdapeng520/zdpgo_json

功能清单

  • 与 Python 保持一致的 API 接口,dump 对应 Dump,load 对应 Load,dumps 对应 Dumps,loads 对应 Loads
  • 支持 json 字符串的查询

版本历史

  • v0.1.0 2022/02/16 基本功能
  • v0.1.1 2022/03/30 读取配置
  • v0.1.2 2022/04/02 项目结构优化
  • v0.1.3 2022/06/16 优化:读取 json 字符串优化
  • v0.1.4 2022/06/22 新增:整合 jsoniter,序列化性能极大提升
  • v0.1.5 2022/07/14 优化:代码优化

使用示例

读写 json 文件
package main

import (
	"fmt"

	"github.com/zhangdapeng520/zdpgo_json"
)

type account struct {
	Email    string  `json:"email"`
	password string  `json:"password"` // 不会处理私有变量
	Money    float64 `json:"money"`
}

type user struct {
	Name    string
	Age     int
	Roles   []string
	Skill   map[string]float64
	Account account
}

func main() {
	a := account{
		Email:    "张大鹏",
		password: "123456",
		Money:    100.5,
	}
	u := user{
		Name:    "张大鹏",
		Age:     27,
		Roles:   []string{"Owner", "Master"}, // 处理切片
		Account: a,
	}

	// 序列化
	jsonData, err := zdpgo_json.Dumps(u)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(jsonData)

	// 反序列化
	err = zdpgo_json.Loads(jsonData, &u)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}
序列化和反序列化
package main

import (
	"fmt"

	"github.com/zhangdapeng520/zdpgo_json"
)

type account struct {
	Email    string  `json:"email"`
	password string  `json:"password"` // 不会处理私有变量
	Money    float64 `json:"money"`
}

type user struct {
	Name    string
	Age     int
	Roles   []string
	Skill   map[string]float64
	Account account
}

func main() {
	a := account{
		Email:    "张大鹏",
		password: "123456",
		Money:    100.5,
	}
	u := user{
		Name:    "张大鹏",
		Age:     27,
		Roles:   []string{"Owner", "Master"}, // 处理切片
		Account: a,
	}

	// 写入文件
	err := zdpgo_json.Dump("user.json", u)
	if err != nil {
		fmt.Println(err)
	}

	// 读取文件
	err = zdpgo_json.Load("user.json", &u)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(u)
}
从 json 字符串中查询数据
package main

import (
	"fmt"

	"github.com/zhangdapeng520/zdpgo_json"
)

type account struct {
	Email    string  `json:"email"`
	password string  `json:"password"` // 不会处理私有变量
	Money    float64 `json:"money"`
}

type user struct {
	Name    string
	Age     int
	Roles   []string
	Skill   map[string]float64
	Account account
}

func main() {
	a := account{
		Email:    "张大鹏",
		password: "123456",
		Money:    100.5,
	}
	u := user{
		Name:    "张大鹏",
		Age:     27,
		Roles:   []string{"Owner", "Master"}, // 处理切片
		Account: a,
	}

	j := zdpgo_json.New()

	// 序列化
	jsonData, err := j.Dumps(u)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(jsonData)
	fmt.Println("the json string :", jsonData)

	// Get查询
	money := j.Query.Get(jsonData, "Account.money")
	fmt.Println("Get查询:", money, money.Float())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(filePath string, obj interface{}) error

Dump 将Golang对象写入到json文件

func Dumps

func Dumps(obj interface{}) (string, error)

Dumps 将Golang对象转换为json字符串

func Load

func Load(filePath string, obj interface{}) error

Load 将json文件读取并转换为Golang对象

func Loads

func Loads(str string, obj interface{}) error

Loads 将字符串转换为Golang对象

func ReadConfig added in v0.1.1

func ReadConfig(configObj interface{}, configFileList ...string) error

ReadConfig 读取配置,支持同时读取多个

func ReadDefaultConfig added in v0.1.1

func ReadDefaultConfig(configObj interface{}) error

ReadDefaultConfig 读取默认配置。默认公共配置config/config.json,默认私密配置config/secret/.config.json

Types

type Json added in v0.1.2

type Json struct {
	Query *query.Query // 查询核心对象

	// 方法列表
	Dump  func(filePath string, obj interface{}) error
	Load  func(filePath string, obj interface{}) error
	Dumps func(obj interface{}) (string, error)
	Loads func(str string, obj interface{}) error
}

Json 处理json的核心对象

func New added in v0.1.2

func New() *Json

New 创建新的处理json的对象示例

Directories

Path Synopsis
examples
Package jsoniter implements encoding and decoding of JSON as defined in RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
Package jsoniter implements encoding and decoding of JSON as defined in RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
Package match provides a simple pattern matcher with unicode support.
Package match provides a simple pattern matcher with unicode support.

Jump to

Keyboard shortcuts

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