jsonKit

package
v1.3.16 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

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{}) ([]byte, error)

Marshal 序列化.

@param obj 可以为nil || ""

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

func MarshalToString

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

MarshalToString 序列化为字符串.

PS: (1) 缺陷: 多次序列化相同的map实例(length >= 2),返回值可能不同,想解决可以使用 MarshalToStringWithJsoniterApi().

@param obj 可以为nil || ""

e.g. (nil) => "null", nil ("") => "\"\"", nil

func MarshalToStringWithIndent

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

func MarshalToStringWithJsoniterApi

func MarshalToStringWithJsoniterApi(api jsoniter.API, obj interface{}) (string, error)

MarshalToStringWithJsoniterApi 可以自定义api

@param api 可以为nil,jsoniter.ConfigDefault || jsoniter.ConfigCompatibleWithStandardLibrary || ...

e.g. 如果希望多次序列化同一map实例,返回的json字符串一直,传参api可以为 jsoniter.ConfigCompatibleWithStandardLibrary.

func MarshalWithIndent

func MarshalWithIndent(obj interface{}) ([]byte, error)

func MarshalWithJsoniterApi

func MarshalWithJsoniterApi(api jsoniter.API, obj interface{}) ([]byte, error)

MarshalWithJsoniterApi 可以自定义api

func MustSetUp added in v1.3.11

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(api jsoniter.API, code, message string, data interface{}, msgArgs ...interface{}) (json string, err error)

func SealWithData

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

func SetMsgHook added in v1.3.11

func SetMsgHook(hook MessageHook)

func SetRespHook added in v1.3.11

func SetRespHook(hook ResponseHook)

func SetUp added in v1.3.11

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(data []byte, ptr interface{}) error

Unmarshal !!!:要注意传参data长度为0的情况,会报错 * @param ptr 类型只能为指针(pointer),且不能为nil

func UnmarshalFromString

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

UnmarshalFromString

@param str !!!: 不能为空字符串(""),否则会报错 @param obj 只能为指针(pointer),且不能为nil

func UnmarshalFromStringToMap

func UnmarshalFromStringToMap(str string) (map[string]interface{}, error)

func UnmarshalToMap

func UnmarshalToMap(data []byte) (map[string]interface{}, error)

Types

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