jsonKit

package
v2.1.13 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearMsgProcessor

func ClearMsgProcessor()

func ClearRespProcessor

func ClearRespProcessor()

func Marshal

func Marshal(obj interface{}, options ...JsonOption) ([]byte, error)

Marshal 序列化(可选配置: api && indent).

@param obj 可以为nil || ""

e.g.

(nil) 	=> []byte("null"), nil
("") 	=> []byte("\"\""), nil

func MarshalToString

func MarshalToString(obj interface{}, options ...JsonOption) (string, error)

MarshalToString 序列化为json字符串(可选配置: api && indent).

@param obj 可以为nil || ""

e.g.

(nil) 	=> "null", nil
("") 	=> "\"\"", nil

e.g.1

m := map[string]interface{}{
	"a": "0",
	"b": "1",
}

str, _ := jsonKit.MarshalToString(m)
fmt.Println(str)
// {"a":"0","b":"1"}

str, _ = jsonKit.MarshalToString(m, jsonKit.WithIndent("    "))
fmt.Println(str)
//{
//	"a": "0",
//	"b": "1"
//}

func MustSetUp

func MustSetUp(messageHook MessageHook, responseHook ResponseHook, messageFiles ...string)

func Seal

func Seal(code string, msgArgs ...interface{}) string

Seal

@param args 不传参的情况,值为nil

func SealFully

func SealFully(code, message string, data interface{}, msgArgs ...interface{}) (json string, err error)

func SealWithData

func SealWithData(code string, data interface{}, msgArgs ...interface{}) string

func SetMsgHook

func SetMsgHook(hook MessageHook)

func SetRespHook

func SetRespHook(hook ResponseHook)

func SetUp

func SetUp(msgHook MessageHook, respHook ResponseHook, messageFiles ...string) (err error)

SetUp

@param msgProcessor [可以为nil] 对响应结构体中的message进行二开,比如可以加上: 是哪台服务响应的 @param respProcess [可以为nil] 对响应结构体进行二开,以修改序列化为json字符串时的key @param messageFiles [.properties文件] (存储code和msg映射关系的)文件的路径(相对 || 绝对),如果为空则不读取message文件

func Unmarshal

func Unmarshal(ptr interface{}, data []byte) error

Unmarshal 反序列化.

@param ptr (1) 不能为nil

(2) 指针类型

@param data 必要条件: len(data) > 0(包含: 不能为nil)

func UnmarshalFromString

func UnmarshalFromString(ptr interface{}, str string) error

UnmarshalFromString 反序列化.

@param ptr (1) 不能为nil

(2) 指针类型

@param str 不能为空字符串("")

Types

type JsonOption

type JsonOption func(opts *jsonOptions)

func WithApi

func WithApi(api jsoniter.API) JsonOption

WithApi

@param api jsoniter.ConfigDefault || jsoniter.ConfigCompatibleWithStandardLibrary || jsoniter.ConfigFastest

func WithIndent

func WithIndent(indent string) JsonOption

WithIndent

@param indent (0) 建议使用 " "(4个空格)

   			  (1) 目前只能是 "" 或 多个空格(不能有其他字符)
			  (2) encoding/json标准库 可以用"\t"

type MessageHook

type MessageHook func(code string, message string, data interface{}) string

type Response

type Response struct {
	Code    string      `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

Response 响应给前端的json对象.

type ResponseHook

type ResponseHook func(resp *Response) any

Jump to

Keyboard shortcuts

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