jsoniterKit

package
v3.0.35 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Unmarshal 反序列化.
	/*
	   @param ptr 	(1) 不能为nil
	   			(2) 指针类型
	   @param data	必要条件: len(data) > 0(包含: 不能为nil)
	*/
	Unmarshal = jsoniter.Unmarshal

	// UnmarshalFromString 反序列化.
	/*
	   @param ptr 	(1) 不能为nil
	   			(2) 指针类型
	   @param str	不能为空字符串("")
	*/
	UnmarshalFromString = jsoniter.UnmarshalFromString
)

Functions

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"
//}

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"

Jump to

Keyboard shortcuts

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